<?php

namespace Admin\Controller;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class SupportController extends ThinkController {
    

    public function lists($p=1){
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row=10;
        if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
        if(I('promote_id')){
            $map['promote_id']=I('promote_id');
        }
        if(I('game_id')){
            $map['game_id']=I('game_id');
        }
        if(!empty(I('apply_type')) || I('apply_type')==='0'){
            $map['apply_type']=I('apply_type');
        }
        //
        if(!empty(I('apply_status')) || I('apply_status')==='0'){
            $map['apply_status']=I('apply_status');
        }
        //为数据权限添加
        setPowerPromoteIds($map);
        /* 获取频道列表 */
        $list = M('test_resource','tab_')
              ->field('*')
              ->where($map)
              ->order('id desc')
              ->page($page,$row)
              ->select();
        $count = M('test_resource','tab_')->where($map)->count();
        //分页
        $page = set_pagination($count,$row);
        if($page) {$this->assign('_page', $page);}

        $this->checkListOrCountAuthRestMap($map,[]);
        
        $this->assign('list_data', $list);
        $show_data_power = (is_administrator()|| session('user_auth')['show_data']);
        $this->assign('show_data_power', $show_data_power);
        $this->meta_title = '资源发放';
        $this->display();
    }
    //改变审核状态
    // public function change(){
    //     if (empty(I('id'))) {
    //         $this->error('请选择要操作的数据!');
    //     }
    //     $map['id']=I('id');
    //     $data['status']=I('status');
    //     $data['check_time']=time();
    //     $res=M('support','tab_')->where($map)->save($data);
    //     if($res!==false){
    //         $this->success('操作成功');
    //     }else{
    //         $this->error('操作失败');
    //     }
    // }
    //通知渠道
     public function notice_pro(){
        if (empty(I('id'))) {
            $this->ajaxReturn(['status'=>0,'msg'=>'请选择要操作的数据!']);
        }
        if( I('verify_resource') < 0){
            $this->ajaxReturn(['status'=>0,'msg'=>'实际发放数量不正确!']);
        }
        $map['id']=I('id');

        $support=M('test_resource','tab_')->field('apply_resource,apply_status')->find($map['id']);
         if($support['apply_status'] !=0 ){
            $this->ajaxReturn(['status'=>0,'msg'=>'请勿重复提交!']);
        }
        if($support['apply_resource'] < I('verify_resource') ){
            $this->ajaxReturn(['status'=>0,'msg'=>'实际发放数量不能大于申请额度!']);
        }

        $data['verify_remark']=I('verify_remark');
        $data['verify_resource']=I('verify_resource');
        $data['verify_time']=time();
        $data['apply_status']=2;

        $res=M('test_resource','tab_')->where($map)->save($data);

        if($res!==false){

            $resourceData = M('test_resource','tab_')
                ->join('left join tab_user on tab_test_resource.user_id = tab_user.id')
                ->field('user_account,tab_user.promote_account')
                ->where(['tab_test_resource.id'=>$map['id']])
                ->find();

            addOperationLog(['op_type'=>1,'key'=>$resourceData['promote_account'].'/'.$resourceData['user_account'],'op_name'=>'资源发放审核','url'=>U('Support/lists'),'menu'=>'推广员-推广员管理-推广补链-资源发放审核']);

            $this->ajaxReturn(['status'=>1,'msg'=>'操作成功!']);
        }else{
            $this->ajaxReturn(['status'=>0,'msg'=>'操作失败!']);
        }
    }
    //填写拒绝理由
    public function refuse_notice(){
        if (empty(I('id'))) {
            $this->ajaxReturn(['status'=>0,'msg'=>'请选择要操作的数据!']);
        }
        $map['id']=I('id');
        $support=M('test_resource','tab_')->field('verify_resource,apply_status')->find($map['id']);
        if($support['apply_status'] !=0 ){
            $this->ajaxReturn(['status'=>0,'msg'=>'请勿重复提交!']);
        }
        $data['verify_remark']=I('verify_remark');
        $data['verify_time']=time();
        $data['apply_status']=1;
        $res=M('test_resource','tab_')->where($map)->save($data);
        if($res!==false){

            $resourceData = M('test_resource','tab_')
                ->join('left join tab_user on tab_test_resource.user_id = tab_user.id')
                ->field('user_account,tab_user.promote_account')
                ->where(['tab_test_resource.id'=>$map['id']])
                ->find();

            addOperationLog(['op_type'=>1,'key'=>$resourceData['promote_account'].'/'.$resourceData['user_account'],'op_name'=>'资源发放拒绝','url'=>U('Support/lists'),'menu'=>'推广员-推广员管理-推广补链-资源发放拒绝']);

            $this->ajaxReturn(['status'=>1,'msg'=>'操作成功!']);
        }else{
            $this->ajaxReturn(['status'=>0,'msg'=>'操作失败!']);
        }
    }
    // //设置自动审核
    // public function set_config_auto_audit($val='',$config_key='')
    // {
    //     $config['value'] = $val;
    //     $res = M('config')->where(array('name'=>$config_key))->save($config);
    //     S('DB_CONFIG_DATA',null);
    //     if($res !== false){
    //         $this->success('操作成功');
    //     }else{
    //         $this->error('操作失败');
    //     }
    // }

}