<?php

/**
 * 建筑激活控制器
 *
 * User: smartcost
 * Date: 2018/9/26
 * Time: 11:03
 */
class BuildingController extends DooController {

    public $staff;

    public function beforeRun($resource, $action) {
        Doo::loadModel ( 'staff' );
        $staff = new staff ();
        $detail = $staff->getStaffBySid ( $_COOKIE ["staff"] );

        $accessModular = 'BUILD';
        if (empty ( $detail ['cldAccessArray'] ))
            die ( 'illegal request' );
        else {

            if (in_array ( $accessModular, $detail ['cldAccessArray'] )) {
                if (Doo::acl ()->isAllowed ( $accessModular, $resource, $action )) {
                    $flag = true;
                } else {
                    $flag = false;
                }
            } else {
                die ( 'illegal request' );
            }
        }
        if (! $flag)
            die ( 'illegal request' );
    }

    function __construct() {
        if(isset($_COOKIE["staff"])){
            if(!empty($_COOKIE["staff"])){
                Doo::loadModel ( 'staff' );
                $staff = new staff ();
                $this->staff=$staff->getUserByIdList($_COOKIE["staff"]);

                return "/";
            }
        }

        Doo::loadCore ( 'uri/DooUriRouter' );
        $router = new DooUriRouter ();
        $routeRs = $router->execute ( Doo::app ()->route, Doo::conf ()->SUBFOLDER );

        if($routeRs['1']!="login"){
            header ( 'Content-Type:text/html;charset=utf-8' );
            @header ( "Location: /login"  );
        }
    }

    public function index() {
        Doo::loadModel('buildActivation');

        $buildActivation = new BuildActivation();

        Doo::loadModel('L_category');

        $category = new L_category();

        Doo::loadModel('staff');

        $staff = new staff();

        Doo::loadModel('client');

        $client = new client();

        $thisyear = date('Y',time());
        $num = intval($thisyear)-2018;
        $html = '<option value="0" selected>所有年份</option>';
        for($i = $num; $i >= 0; $i--){
            $html .= '<option value="'.(2018+$i).'">'.(2018+$i).'</option>';
        }

        $data['year_option'] = $html;

        // 获取建筑版本
        $data['compilationList'] = $buildActivation->getCompilationList();

        $sql = $this->staff[0]['cid'] == 12 ? ' 1' : 'cid='. $this->staff[0]['cid'];

        $blist = $buildActivation->getList($sql);

        if (!empty($blist)) {
            foreach ($blist as $k => $v) {
                $blist[$k]['categoryname'] = $category->getCategoryById($v['cid'])[0]['title'];
                $blist[$k]['staffname'] = $staff->getStaffBySid($v['sid'])['username'];
                if ($v['client_id'] != 0) {
                    $blist[$k]['clientname'] = $client->getClientByID($v['client_id'])['clientname'];
                }
                $blist[$k]['addtime'] = date('Y-m-d', $v['addtime']);
            }
        }

        //办事处列表
        $data['categoryList'] = $category->getCategory();

        //办事处人员列表
        $data['staffList'] = $staff->getStaffByCid($this->staff[0]['cid']);

        $data['blist'] = $blist;

        $data ['memu'] = 'building';
        $data ['staff'] = $this->staff;
        $this->render ( "/building-index", $data );
    }

    /**
     * 获取办事处员工信息json
     */
    public function getStaffMsg() {
        if (isset($_GET['sid']) && is_numeric($_GET['sid'])) {
            Doo::loadModel('staff');

            $staff = new staff();

            $sid = $_GET['sid'];

            $staffInfo = $staff->getStaffBySid($sid);

            if (!empty($staffInfo)) {
                echo json_encode(array('code' => 200, 'msg' => '', 'data' => $staffInfo));
                exit;
            }

        }
        echo json_encode(array('code' => 404, 'msg' => '参数有误'));
        exit;
    }

    /**
     * 获取建筑用户信息json
     */
    public function getBuildMsg() {
        if (isset($_GET['id']) && is_numeric($_GET['id'])) {
//            Doo::loadModel('staff');
//
//            $staff = new staff();
//
//            $id = $_GET['id'];
//
//            $staffInfo = $staff->getStaffBySid($id);

            Doo::loadClass('building');

            $building = new Building();

            $data = $building->getUsersAndCompilationByID($_GET['id']);
//            $data = $building->getUsersAndCompilationByMobile('13726259839');
            $clientInfo = '';
            if (isset($_GET['cid']) && is_numeric($_GET['cid'])) {
                Doo::loadModel('client');

                $client = new client();

                $clientInfo = $client->getClientByID($_GET['cid']);
            }
            $data['clientInfo'] = $clientInfo;

            Doo::loadModel('buildActivation');

            $buildActivation = new BuildActivation();

            Doo::loadModel('L_category');

            $category = new L_category();

            Doo::loadModel('staff');

            $staff = new staff();

            $buildActivationList = $buildActivation->getListByssoID($_GET['id']);

            if (!empty($buildActivationList)) {
                foreach ($buildActivationList as $bk => $bv) {
                    $buildActivationList[$bk]['year'] = date('Y', $bv['addtime']);
                    $buildActivationList[$bk]['month'] = date('m', $bv['addtime']);
                    $buildActivationList[$bk]['day'] = date('d', $bv['addtime']);
                    $buildActivationList[$bk]['categoryname'] = $category->getCategoryById($bv['cid'])[0]['title'];
                    $buildActivationList[$bk]['staffname'] = $staff->getStaffBySid($bv['sid'])['username'];
                }
            }

            $data['buildActivationList'] = $buildActivationList;

            if (!empty($data)) {
                echo json_encode(array('code' => 200, 'msg' => '', 'data' => $data));
                exit;
            } else {
                echo json_encode(array('code' => 404, 'msg' => '建筑不存在该用户'));
                exit;
            }
        }
        echo json_encode(array('code' => 404, 'msg' => '参数有误'));
        exit;
    }

    /**
     * 获取cld客户信息
     */
    public function getClientMsg() {
        if (isset($_GET['cid']) && is_numeric($_GET['cid'])) {
            Doo::loadModel('client');

            $client = new client();

            $cid = $_GET['cid'];

            $clientInfo = $client->getClientByID($cid);

            if (!empty($clientInfo)) {

                Doo::loadModel ( 'client_staff' );

                $client_staff = new client_staff ();

                Doo::loadModel ( 'company' );

                $company = new company ();

                Doo::loadModel ( "district" );

                $district = new district ();

                $companyInfo = $company->getCompanyByCid ( $clientInfo ['companyid'] )[0];

                $staffInfo = $client_staff->getClientByCid ( $clientInfo ['cid'] );

                $local = $district->getbyidlist ( $clientInfo ['district'] );

                if (! isset ( $local [1] ))
                    $local [1] ['name'] = "";
                if (! isset ( $local [2] ))
                    $local [2] ['name'] = "";

                echo json_encode(array('code' => 200, 'msg' => '', 'data' => array('clientInfo' => $clientInfo, 'companyInfo' => $companyInfo, 'staffInfo' => $staffInfo, 'local' => $local)));
                exit;
            }
        }
        echo json_encode(array('code' => 404, 'msg' => '参数有误'));
        exit;
    }

    /**
     * 搜索手机号，防止由于改号码导致数据不全问题
     */
    public function checkMobile() {
        if (isset($_GET['mobile'])) {
            Doo::loadClass('building');

            $building = new Building();

            $data = $building->getUsersAndCompilationByMobile($_GET['mobile']);

            if ($data) {
                Doo::loadModel('buildActivation');

                $buildActivation = new BuildActivation();

                $buildActivationList = $buildActivation->getOneByssoID($data['userInfo']['ssoId']);

                $cid = !empty($buildActivationList) && $buildActivationList['client_id'] != 0 ? $buildActivationList['client_id'] : '';

                echo json_encode(array('code' => 200, 'msg' => '', 'data' => array('id' => $data['userInfo']['ssoId'], 'cid' => $cid)));
                exit;
            } else {
                echo json_encode(array('code' => 404, 'msg' => '该号码未注册建筑项目'));
                exit;
            }
        }
        echo json_encode(array('code' => 404, 'msg' => '参数有误'));
        exit;
    }

    /**
     * 修改编办信息
     */
    public function setUserCompilation() {
        if (isset($_POST['id']) && is_numeric($_POST['id']) && isset($_POST['cid'])) {
            Doo::loadClass('building');

            $building = new Building();

            $result = $building->setUserCompilation($_POST['id'], $_POST['cid']);

            if($result) {
                Doo::loadModel('buildActivation');

                $buildActivation = new BuildActivation();

                $buildActivation->insertActivation($_POST,$this->staff[0]);
                echo json_encode(array('code' => 200, 'msg' => ''));
                exit;
            } else {
                echo json_encode(array('code' => 404, 'msg' => '添加失败'));
                exit;
            }
        }
        echo json_encode(array('code' => 404, 'msg' => '参数有误'));
        exit;
    }

    /**
     * 绑定cld客户
     */
    public function setUserClient() {
        if (isset($_POST['id']) && is_numeric($_POST['id']) && isset($_POST['cid']) && is_numeric($_POST['cid'])) {
            Doo::loadModel('buildActivation');

            $buildActivation = new BuildActivation();

            $result = $buildActivation->setClientID($_POST['id'], $_POST['cid']);

            if ($result) {
                echo json_encode(array('code' => 200, 'msg' => ''));
                exit;
            } else {
                echo json_encode(array('code' => 404, 'msg' => '绑定失败'));
                exit;
            }
        }
        echo json_encode(array('code' => 404, 'msg' => '参数有误'));
        exit;
    }

    /**
     * 获取列表
     */
    public function getBuildList() {

        Doo::loadModel('buildActivation');

        $buildActivation = new BuildActivation();

        $sql = ' 1';

        if (isset($_POST['sso_id']) && is_numeric($_POST['sso_id'])) {
            $sql .= ' and sso_id='.$_POST['sso_id'];
        }

        if (isset($_POST['compilation_id']) && $_POST['compilation_id'] != 0) {
            $sql .= ' and compilation_id="'.$_POST['compilation_id'].'"';
        }

        if (isset($_POST['staff_id']) && is_numeric($_POST['staff_id']) && $_POST['staff_id'] != 0) {
            $sql .= ' and sid='.$_POST['staff_id'];
        } else if (isset($_POST['category']) && is_numeric($_POST['category']) && $_POST['category'] != 12) {
            $sql .= ' and cid='.$_POST['category'];
        }

        if (isset($_POST['year']) && is_numeric($_POST['year']) && $_POST['year'] != 0) {
            $year = intval($_POST['year']);
            if (isset($_POST['month']) && is_numeric($_POST['month']) && $_POST['month'] != 0) {
                $month = intval($_POST['month']);
                $startday = $year.'/'.$month.'/1';
                $endday = $month == 12 ? ($year+1).'/1/1' : $year.'/'.($month+1).'/1';
            } else {
                $startday = $year.'/1/1';
                $endday = ($year+1).'/1/1';
            }
            $sql .= ' and addtime>=UNIX_TIMESTAMP("'.$startday.'") and addtime<UNIX_TIMESTAMP("'.$endday.'")';
        }

        $blist = $buildActivation->getList($sql);
        if (!empty($blist)) {
            Doo::loadModel('L_category');

            $category = new L_category();

            Doo::loadModel('staff');

            $staff = new staff();

            Doo::loadModel('client');

            $client = new client();

            foreach ($blist as $k => $v) {
                $blist[$k]['categoryname'] = $category->getCategoryById($v['cid'])[0]['title'];
                $blist[$k]['staffname'] = $staff->getStaffBySid($v['sid'])['username'];
                if ($v['client_id'] != 0) {
                    $blist[$k]['clientname'] = $client->getClientByID($v['client_id'])['clientname'];
                }
                $blist[$k]['addtime'] = date('Y-m-d', $v['addtime']);
            }
        }
        echo json_encode(array('code' => 200, 'msg' => '', 'data' => $blist));
        exit;
    }
}