<?php

namespace Home\Controller;

use Think\Controller;


class AuthorityManagementController extends ComController
{

    //权限手机列表查询
    public function getPhoneList()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
//        if (!empty($Data['phone'])) {
//            $where = " and phone like '%" . $Data['phone'] . "%'";
//        }
//        $data = M()->query("select phone from wc_user where enable=1 group by phone");
        $data = M()->query("SELECT * FROM (SELECT d.phone,IFNULL(c.personnel_function,0) AS personnel_function  from wc_user as d LEFT JOIN (select a.phone,b.personnel_function from wc_user as a  join personnel as b on a.phone=b.personnel_phone WHERE a.`enable`=1 group by a.phone) as c ON d.phone=c.phone ) AS e WHERE e.personnel_function!=1  GROUP BY e.phone");
//        $data = M()->query("select a.phone from wj_user_management as a join wj_rights_management as b on a.phone=b.phone where  permissionLabel!=1 group by a.phone");
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $data;
        echo json_encode($res);
    }

    //权限列表查询
    public function getAuthorityList()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!empty($Data['phone'])) {
            $where = " and personnel_phone like '%" . $Data['phone'] . "%'";
        }

        if(!empty($Data['currentPage'])&&!empty($Data['size'])){
            $start_number = ($Data['currentPage']-1)*$Data['size'];
            $limit = " limit ".$start_number.",".$Data['size'];
        }
        $R = M()->query("select * from personnel where enable=1 and personnel_function!=1 " . $where . " group by personnel_phone");
        $data = M()->query("select personnel_name as userName,personnel_phone as phone,personnel_function as permissionLabel,site_id as siteId,alarm_setting as alarmSetting,create_time,creater,enable from personnel where enable=1 and personnel_function!=1 " . $where . " group by personnel_phone ".$limit);
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $data;
        $res['total'] =  count($R);
        $res['currentPage'] = $Data['currentPage'];
        $res['size'] = $Data['size'];
        echo json_encode($res);
    }

    public function setaddAuthority()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (!isset($Data['phone']) || !isset($Data['siteId']) || !isset($Data['permissionLabel'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $arr = explode(',', $Data['siteId']);
        foreach ($arr as $key => $value) {
//            print_r($arr[$key]);
            $arr1['personnel_name'] = $Data['userName'];
            $arr1['personnel_phone'] = $Data['phone'];
            $arr1['site_id'] = $arr[$key];
            $arr1['personnel_function'] = $Data['permissionLabel'];
            $arr1['alarm_setting'] = "";
            $arr1['create_time'] =  date('Y-m-d H:i:s');
            $arr1['enable'] = 1;
            $arr1['creater'] = $_SESSION['phone'];
            $R = M("personnel")->add($arr1);
        }

        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    //权限修改回显
    public function getAuthority()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!isset($Data['phone'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $where = " and a.personnel_phone = '" . $Data['phone'] . "'";
        $data = M()->query("select a.id,a.personnel_name as userName,a.personnel_phone as phone,a.personnel_function as permissionLabel,a.alarm_setting as alarmSetting,a.site_id as siteId,b.site_name as siteName from personnel as a join site as b on a.site_id=b.id where 1=1 " . $where . " group by a.site_id");
        $arr = null;
        foreach ($data as $key => $value) {
            $arr[$key]['id'] = (int)$data[$key]['siteId'];
            $arr[$key]['siteName'] = $data[$key]['siteName'];
        }
        $arr1[0]['id'] = $data[0]['id'];
        $arr1[0]['userName'] = $data[0]['userName'];
        $arr1[0]['phone'] = $data[0]['phone'];
        $arr1[0]['permissionLabel'] = $data[0]['permissionLabel'];
        $arr1[0]['alarmSetting'] = $data[0]['alarmSetting'];
        $arr1[0]['siteIdData'] = $arr;
//        print_r($arr1);
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $arr1;
        echo json_encode($res);
    }


    //权限修改
    public function setEditAuthority()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (!isset($Data['phone']) || !isset($Data['phone']) || !isset($Data['siteId']) || !isset($Data['permissionLabel'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where['personnel_phone'] = $Data['phone'];
        $R = M("personnel")->where($where)->delete();
        if ($R) {
            $arr = explode(',', $Data['siteId']);
            foreach ($arr as $key => $value) {
//                print_r($arr[$key]);
//                $arr1['userName'] = $Data['phone'];
                $arr1['personnel_phone'] = $Data['phone'];
                $arr1['site_id'] = $arr[$key];
                $arr1['personnel_function'] = $Data['permissionLabel'];
                $R1 = M("personnel")->add($arr1);
            }
            if ($R1) {
                $res['msg'] = '操作成功';
                $res['flag'] = true;
                echo json_encode($res);
            } else {
                $res['msg'] = '操作失败';
                $res['flag'] = false;
                echo json_encode($res);
            }

        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    //

    //权限删除
    public function setDelAuthority()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (!isset($Data['phone'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if ($Data['phone'] == '17621295894') {
            $res['msg'] = '操作失败，最高权限用户不可删除';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $where['personnel_phone'] = $Data['phone'];
        $R = M("personnel")->where($where)->delete();
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }
}