<?php

    /***************************************************************\
    |                                                               |
    |   开单大师 - 专业的房产ERP管理                                  |
    |                                                               |
    |   © 2019 http://www.kaidandashi.com All rights reserved.      |
    |                                                               |
    |   开单大师由淮南市银泰软件科技有限公司提供技术                    |
    |                                                               |
    |   QQ:984784483                                                |
    |                                                               |
    \***************************************************************/

    namespace Admin\Controller;
    use Think\Controller;
    class WeibanController extends CommonController {
        public function index(){
            if (I('is_ajax')) {
                $startUnix = strtotime(I('datetime1'));
                $endUnix = strtotime(I('datetime2'))+86399; 
                $p = I('pageSize');
                $sn = I('sortName');
                $so = I('sortOrder');
                $sousuo=" where a.id>0";
                if (I('datetime1') and I('datetime2')) {
                    $sousuo.=" and addtime >= '".$startUnix."' and addtime<'".$endUnix."'";
                }elseif (I('datetime1')) {
                    $sousuo.=" and addtime >= '".$startUnix."'";
                }elseif (I('datetime2')) {
                    $sousuo.=" and addtime<'".$endUnix."'";
                }
                /*权限控制开始*/
                $map = "gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(744,0);
                if ($x && $x != "all" && $x != "me") {
                    $map .= " and bumen in ({$x})";
                }elseif ($x == "me") {
                    $map .= " and id=".session('uid');
                }elseif ($x == false) {
                    $map .= " and id=0";
                }
                /*权限控制结束*/
                $Model = new \Think\Model();
                $sqlcount = "select count(*) FROM  __WEIBAN__ as a join (select * from __YONGHU__ where $map) as b on a.uid=b.id left join __FANGYUAN__ as c on a.fybianhao=c.bianhao".$sousuo;
                $countb=$Model->query($sqlcount);
                $count=$countb[0]['count(*)'];
                $Page = new \Think\Page($count,$p); //实例化分页类 传入总记录数和每页显示的记录数(15)
                $show = $Page->show();  //分页显示输出    
                $sql="select a.*,b.ygmingcheng,b.id as y_id,b.bumen,c.xiaoqum,c.zuodong,c.danyuan,c.louceng,c.fanghao,c.weihurenid,c.zhuangtai FROM __WEIBAN__ as a inner join (select * from __YONGHU__ where $map) as b on a.uid=b.id left join __FANGYUAN__ as c on a.fybianhao=c.bianhao ".$sousuo." order by id DESC limit ".$Page->firstRow.','.$Page->listRows;
                $arr=$Model->query($sql);
                foreach ($arr as $key => $value) {
                    $arr[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
                    $arr[$key]['status'] = $value['status'] ? '有效' : '待审核';
                    $arr[$key]['bumen'] = zym($value['bumen'],5);
                    $arr[$key]['dizhi'] = $value['zuodong'].'号楼'.$value['danyuan'].'单元'.$value['louceng'].'层'.$value['fanghao'];
                    $arr[$key]['shijian'] = date('Y-m-d',$value['startunix']).'至'.date('Y-m-d',$value['endunix']);
                    $arr[$key]['jiage'] = $value['jiage'].'万元';
                    $arr[$key]['read'] = _rbac_btn_show(745,0,$value['uid']) ? '1' : '0';
                    $arr[$key]['edit'] = _rbac_btn_show(746,0,$value['uid']) ? '1' : '0';
                    $arr[$key]['del'] = _rbac_btn_show(751,0,$value['uid']) ? '1' : '0';
                    $arr[$key]['fyxq'] = _rbac_btn_show(46,$value['zhuangtai'],$value['weihurenid']) ? '1' : '0';
                }
                $data=array('data'=>$arr,'total'=>$count);
                header("Content-type: application/json");
                exit(json_encode($data));
            }else{
                $this->display();
            }
        }
        public function editweiban(){
            $id = I('id',0,'int');
            $uid = M('weiban')->where(array('id'=>$id))->getField('uid');
            $is_rbac = _rbac_btn_show(746,0,$uid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            if (IS_POST) {
                $wbtps=M('photos')->where(array('data_table'=>'weiban','data_id'=>$id))->count();
                $file_arr = I('file_path');
                if (count($file_arr) > (6-$wbtps)) {
                    $this->error('最多上传6张！');
                }
                $data['wbtime'] = strtotime(I('wbtime'));
                $time_s = explode(' - ',I('shijian'));
                $wb_uid = M('yonghu')->where(array('ygmingcheng'=>I('wb_user')))->getField('id');
                if (!$wb_uid) {
                    $this->error('查找不到该用户！');
                }
                $data['uid'] = $wb_uid;
                $data['xieyibh'] = I('xieyibh');
                $data['jiage'] = I('jiage');
                $data['baozhengjin'] = I('baozhengjin');
                $data['startunix'] = strtotime($time_s[0]);
                $data['endunix'] = strtotime($time_s[1]);
                $data['beizhu'] = I('beizhu');
                $isok = M('weiban')->where(array('id'=>$id))->save($data);
                if ($isok) {
                    foreach ($file_arr as $value) {
                        $file_img_arr[] = array(
                            'data_id' => $id,
                            'img_path' => $value,
                            'addtime' => time(),
                            'adduid' => session('uid'),
                            'gongsiid' => session('gongsiid'),
                            'data_table' => 'weiban'
                        );
                    }
                    M('photos')->addAll($file_img_arr);
                    $this->success('保存成功!');
                } else {
                    $this->error('保存失败!');
                }
            }else{
                $weiban = M('weiban')->where(array('id'=>$id))->find();
                $weiban['wb_user'] = M('yonghu')->where(array('id'=>$weiban['uid']))->getField('ygmingcheng');
                $this->weiban = $weiban;
                $this->display();
            }
        }
        public function content(){
            $id = I('get.id',0 ,'int');
            $find = M('weiban')->where(array('id'=>$id))->find();
            if (!$find) {
                echo "没找到资源！ -400";
                die;
            }
            $is_rbac = _rbac_btn_show(745,0,$find['uid']);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $imgs = M('photos')->where(array('data_id'=>$id,'data_table'=>'weiban','status'=>1))->select();
            $fangyuan = M('fangyuan')->where(array('bianhao'=>$find['fybianhao']))->find();
            $user = M('yonghu')->where(array('id'=>$find['uid']))->field('ygmingcheng,bumen')->find();
            $this->user = $user;
            $this->fangyuan = $fangyuan;
            $this->find = $find;
            $this->imgs = $imgs;
            $this->display();
        }
        public function delweiban(){
            $id = I('post.id',0 ,'int');
            $uid = M('weiban')->where(array('id'=>$id))->getField('uid');
            $is_rbac = _rbac_btn_show(751,0,$uid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $isok = M('weiban')->where(array('id'=>$id))->delete();
            if ($isok) {
                $this->success('删除成功！');
            }else{
                $this->error('删除失败！');
            }
        }
    }
?>