<?php
// 公司关系绑定
namespace Admin\Controller;

class CompanyRelationController extends AdminController
{
    /**
     * @var 默认甲方公司类型
     */
    const COMPANY_TYPE_DEFAULT_A = '0';

     /**
     * @var 默认甲方公司类型
     */
    const COMPANY_TYPE_DEFAULT_B = '2';

    public $admininfo;
    public $DBModel;
    public $DBlogModel;
    public $OpAuthList=[];
    public $Status = [
        "-2"=>"管理员拒绝",
        "-1"=>"市场部拒绝",
        "0"=>"待审核",
        "1"=>"市场部通过",
        "2"=>"管理员通过"
    ];
    public $CompanyType = [
        "0"=>"己方公司",
        "1"=>"上游cp公司",
        "2"=>"联运下游渠道公司"
    ];
    public $SettlementType=[
        "0"=>"无",
        "1"=>"周结",
        "2"=>"月结"
    ];
    public $InvoiceType=[
        "0"=>"无",
        "1"=>"专票",
        "2"=>"普票"
    ];
    public $IsPayment=[
        "1"=>"是",
        "2"=>"否"
    ];
    public $Collection=[
        "1"=>"甲方",
        "2"=>"乙方"
    ];
    public function _initialize()
    {
        $this->admininfo = $_SESSION['onethink_admin']['user_auth'];
        $this->DBlogModel = M("company_relation_log","tab_");
        $this->DBModel = M("company_relation","tab_");
        parent::_initialize();
    }
    //生效列表
    public function index()
    {
        $params = I('get.');
        $page = $params['p'] ? intval($params['p']) : 1;
        $row = $params['row'] ? intval($params['row']) : 10;

        //权限分配
        if(!IS_ROOT){
            $this->OpAuthList= getModuleControllerAuth();
        }

        $where['_string'] = '1 = 1';
        if(isset($params['company_type'])){
            $where['_string'] .= " AND (first_company_type='{$params['company_type']}' OR second_company_type='{$params['company_type']}')";
        }
        if(isset($params['company_name'])){
            $where['_string'] .= " AND (first_company_name LIKE '%{$params['company_name']}%' OR second_company_name LIKE '%{$params['company_name']}%')";
        }

        if(isset($params['settlement_type'])){
            $where['settlement_type'] = $params['settlement_type'];
        }
        if(isset($params['invoice_type'])){
            $where['invoice_type'] = $params['invoice_type'];
        }
        if(isset($params['is_payment'])){
            $where['is_payment'] = $params['is_payment'];
        }

        $dbres = $this->DBModel->where($where)->order("id desc");;
        if(isset($params['export'])){
            $dbres = $dbres->select();
        }else{
            $dbres = $dbres->page($page, $row)->select();
        }
        foreach($dbres as $k=>&$v){
            $v['settlement_type'] =$this->SettlementType[$v['settlement_type']];
            $v['first_company_type'] =$this->CompanyType[$v['first_company_type']];
            $v['second_company_type'] =$this->CompanyType[$v['second_company_type']];
            $v['invoice_type'] =$this->InvoiceType[$v['invoice_type']];
            $v['is_payment'] =$this->IsPayment[$v['is_payment']];
            $v['collection'] =$this->Collection[$v['collection']];
            $v['oplist'] = $this->listOpAuth($v);
        }

        if(isset($_REQUEST['export'])){
            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),"op_name"=>"导出",'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData)]);
            data2csv($dbres,'公司绑定生效列表',array(
                "first_company_name"=>"甲方公司类型",
                "first_company_type"=>"甲方公司",
                "second_company_name"=>"乙方公司",
                "second_company_type"=>"乙方公司类型",
                "settlement_type"=>"结算周期",
                "invoice_type"=>"开票类型",
                "invoice_content"=>"开票内容",
                "is_payment"=>"打款流程",
                "collection"=>"收款方"
            ));
        }

        $count = $this->DBModel->field("count(id) count")->where($where)->find()['count'];
        $this->assign('data', $dbres);
        $page = set_pagination($count, $row,$params);
        if($page) {
            $this->assign('_page', $page);
        }
        $this->assign('Status',$this->Status);
        $this->assign('SettlementType',$this->SettlementType);
        $this->assign('CompanyType',$this->CompanyType);
        $this->assign('InvoiceType',$this->InvoiceType);
        $this->assign('IsPayment',$this->IsPayment);
        $this->assign('menubtn',$this->listMenuAuth());
        $this->display();   
    }
    //审核列表
    public function lists()
    {
        $params = I('get.');
        $page = $params['p'] ? intval($params['p']) : 1;
        $row = $params['row'] ? intval($params['row']) : 10;
        //权限分配
        if(!IS_ROOT){
            $this->OpAuthList= getModuleControllerAuth();
        }
        $where['_string'] = '1 = 1';
        if(isset($params['company_type'])){
            $where['_string'] .= " AND (first_company_type='{$params['company_type']}' OR second_company_type='{$params['company_type']}')";
        }
        if(isset($params['company_name'])){
            $where['_string'] .= " AND (first_company_name LIKE '%{$params['company_name']}%' OR second_company_name LIKE '%{$params['company_name']}%')";
        }

        if(isset($params['settlement_type'])){
            $where['settlement_type'] = $params['settlement_type'];
        }
        if(isset($params['invoice_type'])){
            $where['invoice_type'] = $params['invoice_type'];
        }
        if(isset($params['is_payment'])){
            $where['is_payment'] = $params['is_payment'];
        }
        if(isset($params['status'])){
            $where['status'] = $params['status'];
        }
        // $this->checkListOrCountAuthRestMap($where);//导出权限
        $dbres = $this->DBlogModel->where($where)->order("FIELD(status,0,1,-1,-2,2),id desc");;
        if(isset($params['export'])){
            $dbres = $dbres->select();
        }else{
            $dbres = $dbres->page($page, $row)->select();
        }
        foreach($dbres as $k=>&$v){
            $v['settlement_type'] =$this->SettlementType[$v['settlement_type']];
            $v['first_company_type'] =$this->CompanyType[$v['first_company_type']];
            $v['second_company_type'] =$this->CompanyType[$v['second_company_type']];
            $v['invoice_type'] =$this->InvoiceType[$v['invoice_type']];
            $v['is_payment'] =$this->IsPayment[$v['is_payment']];
            $v['collection'] =$this->Collection[$v['collection']];
            $v['verify_log'] = json_decode($v['verify_log'], true);
            if (isset($params['export'])) {
                $symbol = "\n";
            } else {
                $symbol = "<br>";
            }

            $v["create"]= "{$v['verify_log']['create_user']} {$symbol} {$v['verify_log']['create_time']}";
            if(isset($v['verify_log']['market_user'])){
                if($v['status'] == -1){
                    $ts = "审核拒绝";
                }else{
                    $ts = "审核通过";
                }
                $v["market"]= "{$ts}({$v['verify_log']['market_user']}) {$symbol} {$v['verify_log']['market_time']}";
            }else{
                $v["market"] = '--';
            }

            if(isset($v['verify_log']['admin_user'])){
                if($v['status'] == -2){
                    $ts = "审核拒绝";
                }else{
                    $ts = "审核通过";
                }
                $v["admin"]= "{$ts}({$v['verify_log']['admin_user']}) {$symbol} {$v['verify_log']['admin_time']}";
            }else{
                $v["admin"] = '--';
            }
            $v['oplist'] = $this->OpAuth($v);

        }

        if(isset($_REQUEST['export'])){
            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),"op_name"=>"导出",'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData)]);
            data2csv($dbres,'公司绑定审核列表',array(
                "first_company_name"=>"甲方公司类型",
                "first_company_type"=>"甲方公司",
                "second_company_name"=>"乙方公司",
                "second_company_type"=>"乙方公司类型",
                "settlement_type"=>"结算周期",
                "invoice_type"=>"开票类型",
                "invoice_content"=>"开票内容",
                "is_payment"=>"打款流程",
                "collection"=>"收款方",
                "create"=>"创建记录",
                "market"=>"市场部审批",
                "admin"=>"管理员审批",
                "remark"=>"备注"
            ));
        }

        $count = $this->DBlogModel->field("count(id) count")->where($where)->find()['count'];
        $this->assign('data', $dbres);
        $page = set_pagination($count, $row,$params);
        if($page) {
            $this->assign('_page', $page);
        }
        $this->assign('Status',$this->Status);
        $this->assign('SettlementType',$this->SettlementType);
        $this->assign('CompanyType',$this->CompanyType);
        $this->assign('InvoiceType',$this->InvoiceType);
        $this->assign('IsPayment',$this->IsPayment);
        $this->assign('menubtn',$this->menuAuth());
        $this->display();   
    }

    public function marketAgree(){
        if(!isset($_REQUEST['ids'])) $this->error("参数错误");
        $ids = $_REQUEST['ids'];
        $dbres = $this->DBlogModel->field("id,status,verify_log")->where("id in ({$ids})")->select();
        foreach($dbres as $k=>&$v){
            if($v['status'] != 0) continue;
            $v['verify_log'] = json_decode($v['verify_log'],true);
            $v['verify_log']['market_user']=$this->admininfo["username"];
            $v['verify_log']['market_time']=date("Y-m-d H:i:s");
            $v['verify_log'] = json_encode($v['verify_log']);
            $v['status']=1;
            $this->DBlogModel->save($v);
            addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"市场部审核",'url'=>U('index')]);
        }
        $this->ajaxReturn(array(
            'status' => 1,
            "info"=>"市场部审核通过成功"
        ));
    }
    public function marketRefuse(){
        if(!isset($_REQUEST['ids'])) $this->error("参数错误");
        $ids = $_REQUEST['ids'];
        $dbres = $this->DBlogModel->field("id,status,verify_log")->where("id in ({$ids})")->select();
        foreach($dbres as $k=>&$v){
            if($v['status'] != 0) continue;
            $v['verify_log'] = json_decode($v['verify_log'],true);
            $v['verify_log']['market_user']=$this->admininfo["username"];
            $v['verify_log']['market_time']=date("Y-m-d H:i:s");
            $v['verify_log'] = json_encode($v['verify_log']);
            $v['status']=-1;
            $this->DBlogModel->save($v);
            addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"市场部审核",'url'=>U('index')]);
        }
        $this->ajaxReturn(array(
            'status' => 1,
            "info"=>"市场部审核拒绝成功"
        ));
    }
    public function adminRefuse(){
        if(!isset($_REQUEST['ids'])) $this->error("参数错误");
        $ids = $_REQUEST['ids'];
        $dbres = $this->DBlogModel->field("id,status,verify_log")->where("id in ({$ids})")->select();
        foreach($dbres as $k=>&$v){
            if($v['status'] != 1) continue;
            $v['verify_log'] = json_decode($v['verify_log'],true);
            $v['verify_log']['admin_user']=$this->admininfo["username"];
            $v['verify_log']['admin_time']=date("Y-m-d H:i:s");
            $v['verify_log'] = json_encode($v['verify_log']);
            $v['status']=-2;
            $this->DBlogModel->save($v);
            addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"管理员审核",'url'=>U('index')]);
        }
        $this->ajaxReturn(array(
            'status' => 1,
            "info"=>"管理员审核拒绝成功"
        ));
    }
    //管理员成功
    public function adminAgree(){
        if(!isset($_REQUEST['ids'])) $this->error("参数错误");
        $ids = $_REQUEST['ids'];
        $dbres = $this->DBlogModel->field("*")->where("id in ({$ids})")->select();
        foreach($dbres as $k=>&$v){
            if($v['status'] != 1) continue;
            $v['verify_log'] = json_decode($v['verify_log'],true);
            $v['verify_log']['admin_user']=$this->admininfo["username"];
            $v['verify_log']['admin_time']=date("Y-m-d H:i:s");
            $v['verify_log'] = json_encode($v['verify_log']);
            $v['status']=2;
            $this->DBlogModel->save($v);
            $id = $v['id'];
            //保存到其他表
            //查找是否存在
            unset($v['id']);
            unset($v['remark']);
            unset($v['status']);
            unset($v['verify_log']);
            //判断公司id及类型
            if($v['first_company_type'] == 0){
                $t_company_id = $v['second_company_id'];
                $t_company_type = $v['second_company_type'];
            }else{
                $t_company_id = $v['first_company_id'];
                $t_company_type = $v['first_company_type'];
            }
            $where = "
                (first_company_type ='{$t_company_type}' and first_company_id = '{$t_company_id}')
                OR 
                (second_company_type ='{$t_company_type}' and second_company_id = '{$t_company_id}')
             ";
            $hasdb =  $this->DBModel->where($where)->find();
            if(isset($hasdb['id'])){
                $v['id'] = $hasdb['id'];
                $this->DBModel->save($v);
            }else{
                $this->DBModel->add($v);
            }
            addOperationLog(['op_type'=>1,'key'=>$id,"op_name"=>"管理员审核",'url'=>U('index')]);
        }
        $this->ajaxReturn(array(
            'status' => 1,
            "info"=>"管理员审核通过成功"
        ));
    }

    public function addRelation()
    {
        if ($_POST) {
            $params = I('post.');
            if ($params['first_company_type'] == $params['second_company_type']) {
                $this->error('合作甲乙双方不能是同类型公司');
            }
            if ($params['first_company_type']!=0 && $params['second_company_type']!=0) {
                $this->error('合作甲乙双方必须有个是己方公司');
            }
            $params['remark'] = $params['remark'] ?? '';
            $params['status'] = 0;

            $verify_log = [
                "create_user"=>$this->admininfo["username"],
                "create_time"=>date("Y-m-d H:i:s")
            ];
            if($params['first_company_type'] == 1 || $params['second_company_type'] == 1){
                //上游公司
                $verify_log['market_user']= "AUTO";
                $verify_log['market_time']= date("Y-m-d H:i:s");
                $params['status'] = 1;
            }

            $params['verify_log']=json_encode($verify_log);
            //判断哪方是非官方
            if($params['first_company_type'] > 0){
                $where = "
                (first_company_type ='{$params['first_company_type']}' and first_company_id = '{$params['first_company_id']}') 
                OR
                (second_company_type ='{$params['first_company_type']}' and second_company_id = '{$params['first_company_id']}')
                ";
            }else{
                $where = "
                (first_company_type ='{$params['second_company_type']}' and first_company_id = '{$params['second_company_id']}') 
                OR 
                (second_company_type ='{$params['second_company_type']}' and second_company_id = '{$params['second_company_id']}')
                ";
            }
            $r_res =  $this->DBModel->where($where)->find();
            if(!empty($r_res)){
                $this->error('当前合作方已有绑定关系');
            }

            $hasdb =  $this->DBlogModel->where($where)->find();
            if(!empty($hasdb)){
                //覆盖
                $params['id'] = $hasdb['id'];
                $this->DBlogModel->save($params);
                $id = $hasdb['id'];
            }else{
                //新增
                $id = $this->DBlogModel->add($params);
            }
            addOperationLog(['op_type'=>0,'key'=>$id,'op_name'=>'新增','url'=>U('index')]);
            $this->ajaxReturn(["msg"=>"添加成功,请联系管理员尽快审核","code"=>1,"url"=>U("index")]);

        } else {
            $this->assign('defaultCompanyTypeA', self::COMPANY_TYPE_DEFAULT_A);
            $this->assign('defaultCompanyTypeB', self::COMPANY_TYPE_DEFAULT_B);
            $this->assign('companyType', $this->CompanyType);
            $this->display();
        }
    }
    // public function editRelation()
    // {
    //     if ($_POST) {
    //         $p= I('post.');
    //         if(!isset($p['id'])){
    //             $this->error('参数错误');
    //         }
    //         //查询
    //         $y = $this->DBlogModel->where("id='{$p['id']}'")->find();
    //         $p['remark'] = $p['remark'] ?? '';
    //         if($y['settlement_type'] != $p['settlement_type'] || $y['invoice_type'] != $p['invoice_type'] || $y['invoice_content'] != $p['invoice_content'] || $y['is_payment'] != $p['is_payment'] || $y['collection'] != $p['collection']){
    //             $p['status'] = 0;
    //             $p['verify_log'] = json_encode(["create_user"=>$this->admininfo["username"],"create_time"=>date("Y-m-d H:i:s")]);
    //         }
    //         $this->DBlogModel->save($p);
    //         addOperationLog(['op_type'=>1,'key'=>$p['id'],'op_name'=>'修改','url'=>U('lists')]);
    //         $this->ajaxReturn(["msg"=>"修改成功","code"=>1,"url"=>U("lists")]);
    //     } else {
    //         $params = I('get.');
    //         $id = $params['id'] ?? 0;
    //         $id = intval($id);
    //         $map['id'] = $id;
    //         $dbres = $this->DBlogModel->where($map)->find();
    //         $this->assign('first_company_info',$this->getCompanyInfo($dbres['first_company_type'],$dbres['first_company_id']));
    //         $this->assign('second_company_info',$this->getCompanyInfo($dbres['second_company_type'],$dbres['second_company_id']));

    //         $dbres['first_company_type'] =$this->CompanyType[$dbres['first_company_type']];
    //         $dbres['second_company_type'] =$this->CompanyType[$dbres['second_company_type']];

    //         $this->assign('data', $dbres);
    //         $this->display();
    //     }
    // }
    public function edit()
    {
        if ($_POST) {
            $p= I('post.');
            if(!isset($p['id'])){
                $this->error('参数错误');
            }
            
            //查询
            $y = $this->DBModel->where("id='{$p['id']}'")->find();
            if($y['settlement_type'] != $p['settlement_type'] || $y['invoice_type'] != $p['invoice_type'] || $y['invoice_content'] != $p['invoice_content'] || $y['is_payment'] != $p['is_payment'] || $y['collection'] != $p['collection']){
                //修改了进行审核
                $p['status'] = 0;
                $verify_log = [
                    "create_user"=>$this->admininfo["username"],
                    "create_time"=>date("Y-m-d H:i:s")
                ];
                if($y['first_company_type'] == 1 || $y['second_company_type'] == 1){
                    //上游公司
                    $verify_log['market_user']= "AUTO";
                    $verify_log['market_time']= date("Y-m-d H:i:s");
                    $p['status'] = 1;
                }
                $p['verify_log'] = json_encode($verify_log);
                //其他信息
                $p['first_company_id'] = $y['first_company_id'];
                $p['first_company_name'] = $y['first_company_name'];
                $p['first_company_type'] = $y['first_company_type'];
                $p['second_company_id'] = $y['second_company_id'];
                $p['second_company_name'] = $y['second_company_name'];
                $p['second_company_type'] = $y['second_company_type'];
            }
            if($p['first_company_type'] > 0){
                $where = "
                (first_company_type ='{$p['first_company_type']}' and first_company_id = '{$p['first_company_id']}') 
                OR
                (second_company_type ='{$p['first_company_type']}' and second_company_id = '{$p['first_company_id']}')
                ";
            }else{
                $where = "
                (first_company_type ='{$p['second_company_type']}' and first_company_id = '{$p['second_company_id']}') 
                OR 
                (second_company_type ='{$p['second_company_type']}' and second_company_id = '{$p['second_company_id']}')
                ";
            }
            $hasdb =  $this->DBlogModel->where($where)->find();
            if(!empty($hasdb)){
                //覆盖
                $p['id'] = $hasdb['id'];
                $this->DBlogModel->save($p);
                $id = $hasdb['id'];
            }else{
                //新增
                $id = $this->DBlogModel->add($p);
            }
            addOperationLog(['op_type'=>1,'key'=>$p['id'],'op_name'=>'修改','url'=>U('index')]);
            $this->ajaxReturn(["msg"=>"修改成功,请联系管理员尽快审核","code"=>1,"url"=>U("index")]);

        } else {
            $params = I('get.');
            $id = $params['id'] ?? 0;
            $id = intval($id);
            $map['id'] = $id;
            $dbres = $this->DBModel->where($map)->find();
            $this->assign('first_company_info',$this->getCompanyInfo($dbres['first_company_type'],$dbres['first_company_id']));
            $this->assign('second_company_info',$this->getCompanyInfo($dbres['second_company_type'],$dbres['second_company_id']));

            $dbres['first_company_type'] =$this->CompanyType[$dbres['first_company_type']];
            $dbres['second_company_type'] =$this->CompanyType[$dbres['second_company_type']];

            $this->assign('data', $dbres);
            $this->display();
        }
    }

    public function del()
    {
        if(!isset($_REQUEST['id'])){
            $this->error('参数错误');
        }
        $id = $_REQUEST['id'];
        $res =  $this->DBModel->where("id='{$id}'")->delete();
        if($res !== false){
            addOperationLog(['op_type'=>2,'key'=>$id,'op_name'=>'删除','url'=>U('index')]);
            $this->ajaxReturn(["msg"=>"删除成功","code"=>1,"url"=>U("index")]);
        }else{
            $this->error('删除错误');
        }   
    }

    public function delRelation()
    {
        if(!isset($_REQUEST['id'])){
            $this->error('参数错误');
        }
        $id = $_REQUEST['id'];
        $res =  $this->DBlogModel->where("id='{$id}'")->delete();
        if($res !== false){
            addOperationLog(['op_type'=>2,'key'=>$id,'op_name'=>'删除','url'=>U('lists')]);
            $this->ajaxReturn(["msg"=>"删除成功","code"=>1,"url"=>U("lists")]);
        }else{
            $this->error('删除错误');
        }   
    }
    public function getCompanyList($type)
    {
        if($type ==''){
            $this->success([],'',true);
        }
        $companyInfo = [];
        if($type == 0){
            //己方公司
            $companyInfo = M("CompanyInfo","tab_")->field("id,partner company_name,link_man,link_phone")->where("status='1'")->select();
        }
        $b = $this->getBindCompanyId($type);
        if($b){
            $map = [
                "id"=>["NOT IN",$b]
            ];
        }else{
            $map = false;
        }

        if($type == 1){
            //上游
            $companyInfo = M("Partner","tab_")->where($map)->field("id,partner company_name,company_type,link_man,link_phone")->where("status='1'")->select();
        }
        if($type == 2){
            //下游游
            $companyInfo = M("PromoteCompany","tab_")->where($map)->field("id,company_belong,company_name,company_type,settlement_contact link_man,contact_phone link_phone")->where("status='1'")->select();
        }
        foreach($companyInfo as $k=>&$v){
            if(isset($v['company_belong'])){
                $v['company_belong'] = getCompanyBlong($v['company_belong']);
            }
            if(isset($v['company_type'])){
                $v['company_type'] = ($v['company_type'] == 1 ?"公司" :'个人');
            }
            if(isset($v['link_phone']) && $v['link_phone']==0){
                $v['link_phone'] = '';
            }
        }
        $this->success($companyInfo,'',true);
    }
    public function getCompanyInfo($type,$id)
    {
        $companyInfo = [];
        if($type == 0){
            //己方公司
            $companyInfo = M("CompanyInfo","tab_")->field("id,partner company_name,link_man,link_phone")->where("status='1' AND id='{$id}'")->find();
        }
        if($type == 1){
            //上游
            $companyInfo = M("Partner","tab_")->field("id,partner company_name,company_type,link_man,link_phone")->where("status='1' AND id='{$id}'")->find();
        }
        if($type == 2){
            //下游游
            $companyInfo = M("PromoteCompany","tab_")->field("id,company_belong,company_name,company_type,settlement_contact link_man,contact_phone link_phone")->where("status='1' AND id='{$id}'")->find();
        }
        if(isset($companyInfo['company_belong'])){
            $companyInfo['company_belong'] = getCompanyBlong($companyInfo['company_belong']);
        }
        if(isset($companyInfo['company_type'])){
            $companyInfo['company_type'] = ($companyInfo['company_type'] == 1 ? "公司" :'个人');
        }
        if(isset($companyInfo['link_phone']) && $companyInfo['link_phone']==0){
            $companyInfo['link_phone'] = '';
        }
        return $companyInfo;
    }
    /**
     * 获取已绑定公司id
     * @param [type] $type 1 上游 /2下游
     * @return void
     */
    protected function getBindCompanyId($type){
        $dbres =  M("CompanyRelation","tab_")->where("first_company_type = {$type} OR second_company_type = {$type}")->field("first_company_id,second_company_id")->select();
        if(empty($dbres)){
            return false;
        }else{
            $sendid = [];
            foreach ($dbres as $k => $v) {
                if($v['first_company_type'] == $type){
                    $sendid[] = $v['first_company_id'];
                }else{
                    $sendid[] = $v['second_company_id'];
                }
            }
            return implode(",",$sendid);
        }
    }
    
    protected function listMenuAuth(){
        $addurl = U("addRelation");
        $mentBtn = [
            "addRelation"=>"<a class='butn' href='{$addurl}'>新增公司绑定</a>"
        ];
        $resarr = [];
        foreach ($mentBtn as $k => $v) {
            if(IS_ROOT){
                $resarr[] = $v;
            }else{
                if(in_array($k,$this->OpAuthList)){
                    $resarr[] = $v;
                }
            }
        }
        return $resarr;
    }
    protected function listOpAuth($info){
        $id = $info['id'];
        //原始列表
        $opBtn = [
            "edit"=>"<a class='confirm edit' data-id='{$id}'>编辑</a>",
            "del"=>"<a class='confirm del' data-id='{$id}' style='color: red;'>删除</a>",
        ];
        //操作对应菜单
        $optist = ["edit","del"];
        $resarr = [];
        foreach ($optist as $k => $v) {
            if(IS_ROOT){
                $resarr[] = $opBtn[$v];
            }else{
                if(in_array($v,$this->OpAuthList)){
                    $resarr[] = $opBtn[$v];
                }
            }
        }
        return $resarr;
    }

    protected function menuAuth()
    {
        
        $mentBtn = [
            "marketAgree"=>"<a class='butn' id='marketAgree'>市场部审批通过</a>",
            "marketRefuse"=>"<a class='butn' id='marketRefuse' style='background-color: red;'>市场部审核拒绝</a>",
            "adminAgree"=>"<a class='butn' id='adminAgree'>管理员审核通过</a>",
            "adminRefuse"=>"<a class='butn' id='adminRefuse' style='background-color: red;'>管理员审核拒绝</a>"
        ];
        $resarr = [];
        foreach ($mentBtn as $k => $v) {
            if(IS_ROOT){
                $resarr[] = $v;
            }else{
                if(in_array($k,$this->OpAuthList)){
                    $resarr[] = $v;
                }
            }
        }
        return $resarr;
    }
    protected function OpAuth($info)
    {
        $id = $info['id'];
        //原始列表
        $opBtn = [
            "editRelation"=>"<a class='confirm editRelation' data-id='{$id}'>编辑</a>",
            "delRelation"=>"<a class='confirm delRelation' data-id='{$id}' style='color: red;'>删除</a>",
        ];
        //操作对应菜单
        
        if($info['status'] == 2){
            $optist = [];
        }else{
            $optist = ["delRelation"];
        }
        //
        $resarr = [];
        foreach ($optist as $k => $v) {
            if(IS_ROOT){
                $resarr[] = $opBtn[$v];
            }else{
                if(in_array($v,$this->OpAuthList)){
                    $resarr[] = $opBtn[$v];
                }
            }
        }
        return $resarr;
    }

    protected function error($data)
    {
        header('Content-Type:application/json; charset=utf-8');
        $data =json_encode(['msg'=>$data,"code"=>4000],JSON_UNESCAPED_UNICODE);
        exit($data);
    }

    
}