<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use function GuzzleHttp\Psr7\str;
use think\Db;
class SalesmanController extends AdminBaseController{

    /**
     * 业务员列表
     */
    public function salesman_list(){
        $param = $this->request->param();
        $where = [
            'a.status' => 2
        ];
//        if(!empty($param['start_time']) && !empty($param['end_time'])){
//            $start_time = strtotime($param['start_time']);
//            $end_time = strtotime($param['end_time']);
//            $where['audit_time'] = [['>=',$start_time],['<=',$end_time]];
//        }
        if(!empty($param['keyword'])){
            $keyword = trim($param['keyword']);
            $where['b.phone|u.user_nickname'] = ['like','%'.$keyword.'%'];
        }
        if(!empty($param['id'])){
            $where['a.id'] = ['eq',$param['id']];
        }
        $data = Db::name('my_user')->alias('a')
            ->field("a.uid,a.id,a.balance,a.audit_time,a.is_pro,b.phone,u.user_nickname")
            ->join('user u','a.uid = u.id','LEFT')
            ->join('sale_audit b','a.uid = b.uid','LEFT')
            ->where($where)
            ->group('a.id')
            ->order(['a.balance'=>'DESC','a.create_time'=>'DESC'])
            ->paginate(12);
        $data->appends($param);
        $data_arr = $data -> toArray();
        $data_money = Db::name('money_ratio') -> find();
        foreach ($data_arr['data'] as $key => $val){
            $data_user = Db::name('user') -> where('id',$val['uid']) -> find();
            $data_arr['data'][$key]['name'] = $data_user['user_nickname'];
            $data_arr['data'][$key]['money'] = $data_money['platform_ratio'];
            // 获取提现数据
            $ti_count = Db::name('money_expend')->where('uid',$val['uid'])->where('state',1)->sum('money');// 已提金额
            $data_arr['data'][$key]['ti_count'] = $ti_count;
            $data_arr['data'][$key]['all_count'] = Db::name('money_income')->where('uid',$val['uid'])->sum('money');
        }
        $this -> assign('data_arr',$data_arr['data']);
        $this -> assign('data',$data);
        $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
        $this->assign('id',!empty($param['id']) ? $param['id'] : '');
        return $this -> fetch();

    }

    /**
     * 业务员编辑查看
     */
    public function salesman_edit(){

        if($this -> request -> isPost()){
            $data['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['phone'] = $_POST['phone'];
            $data['id_number'] = $_POST['id_number'];
            $res = Db::name('sale_audit') -> update($data);
            if($res){
                $this -> success('保存成功',url('Salesman/salesman_edit',array('uid' =>$_POST['uid'] )));
            }else{
                $this -> error('保存失败!');
            }
        }else{
            $uid = $this -> request -> param();
            $data = Db::name('sale_audit') -> where('uid',$uid['uid']) -> find();
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品管理列表
     */
    public function salesman_goods_list(){

        if($this -> request -> isPost()){
            $where = [
                'uid' => ['=',$_POST['uid']]
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['book_name'] = ['like',"%$keyword%"];
            }
            $data = Db::name('goods') -> order('sort desc') -> where($where) -> select() -> toArray();
            $this -> assign('good_uid',$_POST['uid']);
            $data_num = Db::name('goods') -> order('sort desc') -> where('uid',$_POST['uid']) -> select() -> toArray();
            for($i=0;$i<count($data_num);$i++){
                $data_num[$i]['wei_sort'] = $i;
            }
            /**
             * 获取假的排序顺序
             */
            for($i=0;$i<count($data_num);$i++){
                for($j=0;$j<count($data);$j++){
                    if($data[$j]['id'] == $data_num[$i]['id']){
                        $data[$j]['wei_sort'] = $data_num[$i]['wei_sort'];
                    }
                }
            }
            $this -> assign('data_num',$data_num);
            $this -> assign('data',$data);
            return $this -> fetch();
        }else{
            $uid = $this -> request -> param();
            $this -> assign('good_uid',$uid['uid']);
            $data = Db::name('goods') -> order('sort desc') -> where('uid',$uid['uid']) -> select() -> toArray();
            for($i=0;$i<count($data);$i++){
                $data[$i]['wei_sort'] = $i;
            }
            $data_num = $data;
            $this -> assign('data_num',$data_num);
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品添加
     */
    public function salesman_goods_add(){

        if($this -> request -> isPost()){
            if($_POST['money'] > $_POST['price']){
                $this -> error('佣金不能大于售价');
            }
           $goods_data = Db::name('goods') -> where('uid',$_POST['uid']) -> select() -> toArray();
            if(count($goods_data) >= 16){
                $this -> error('添加失败');
            }else{
                if(!empty($_POST['det_img'])){
                    $_POST['det_img'] = json_encode($_POST['det_img']);
                    $_POST['det_name'] = json_encode($_POST['det_name']);
                }
                $_POST['create_time'] = time();
                $data = Db::name('goods') -> insertGetId($_POST);
                Db::name('goods') -> where('id',$data) -> update(['sort'=>$data]);
                if($data){
                    $this -> success('添加成功',url('Salesman/salesman_goods_list',array('uid'=>$_POST['uid'])));
                }else{
                    $this -> error('添加失败');
                }
            }
        }else{
            $uid = $this -> request -> param();
            $this -> assign('good_uid',$uid['uid']);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品管理列表排序替换
     */
    public function salesman_change(){

        $start_id = $_POST['start_id'];
        $start_sort = $_POST['start_sort'];
        $end_id = $_POST['end_id'];
        $end_sort = $_POST['end_sort'];
        Db::name('goods') -> where('id',$start_id) -> update(['sort'=>$end_sort]);
        Db::name('goods') -> where('id',$end_id) -> update(['sort'=>$start_sort]);
        return true;

    }

    /**
     * 业务员商品管理列表编辑
     */
    public function salesman_goods_edit(){

        if($this -> request -> isPost()){
            if($_POST['money'] > $_POST['price']){
                $this -> error('佣金不能大于售价');
            }
            if(!empty($_POST['det_img'])){
                $_POST['det_img'] = json_encode($_POST['det_img']);
                $_POST['det_name'] = json_encode($_POST['det_name']);
            }
            $data = Db::name('goods') -> update($_POST);
            if($data){
                $this -> success('保存成功',url('Salesman/salesman_goods_edit',array('id'=>$_POST['id'])));
            }else{
                $this -> error('保存失败');
            }
        }else{
            $id = $this -> request -> param();
            $data = Db::name('goods') -> where('id',$id['id']) -> find();
            if(!empty($data['det_img'])){
                $data['det_img'] = json_decode($data['det_img'],true);
                $data['det_name'] = json_decode($data['det_name'],true);
                foreach ($data['det_img'] as $key => $val){
                    $data['more'][$key]['url'] = $data['det_img'][$key];
                    $data['more'][$key]['name'] = $data['det_name'][$key];
                }
            }
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品列表下架
     */
    public function salesman_goods_soldout(){

        $id = $_POST['id'];
        $data = Db::name('goods') -> where('id',$id) -> update(['is_out'=>0]);
        //删除购物车 浏览记录 收藏表中下架的数据
        Db::name('browsing_history') -> where('goods_id',$id) -> delete();
        Db::name('collect') -> where('goods_id',$id) -> delete();
        Db::name('shopping_cart') -> where('goods_id',$id) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 业务员商品列表上架
     */
    public function salesman_goods_putaway(){

        $id = $_POST['id'];
        $data = Db::name('goods') -> where('id',$id) -> update(['is_out'=>1]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 业务员商品列表删除
     */
    public function salesman_goods_del(){

        $id = $_POST['id'];
        $data = Db::name('goods') -> delete($id);
        Db::name('shopping_cart') -> where("goods_id",$id) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 业务员列表取消推广
     */
    public function salesman_list_no_pro(){

        $id = $_POST['id'];
        $data = Db::name('my_user') -> where('id',$id) -> update(['is_pro'=>0]);
        if($data){
           return true;
        }else{
            return false;
        }

    }

    /**
     * 业务员列表恢复推广
     */
    public function salesman_list_yes_pro(){

        $id = $_POST['id'];
        $data = Db::name('my_user') -> where('id',$id) -> update(['is_pro'=>1]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 下线老师列表
     */
    public function teacher_list(){
        $param = $this->request->param();
        $where = [
            'status' => 3,
            'pid' => $param['id']
        ];
        if(!empty($param['start_time']) && !empty($param['end_time'])){
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
        }
        if(!empty($param['keyword'])){
            $keyword = $param['keyword'];
            $where['b.user_nickname'] = ['like',"%$keyword%"];
        }
        $this -> assign('salesman_id',!empty($param['id']) ? $param['id'] : '');
        $data = Db::name('my_user') -> alias('a') -> field('a.*,b.user_nickname') -> join('user b','a.uid = b.id','left') -> where($where) -> paginate(12);
        $data_arr = $data -> toArray();

        foreach ($data_arr['data'] as $key => $val){
            $money_income = Db::name('money_income') -> where('uid',$val['uid']) -> select();
            $m_money = 0;
            foreach ($money_income as $key1 => $val1){
                $m_money += $val1['money'];
            }
            $data_arr['data'][$key]['money'] = $m_money;
        }
        $this->assign('start_time',!empty($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time',!empty($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
        $this -> assign('data',$data);
        $this -> assign('data_arr',$data_arr['data']);
        return $this -> fetch();

    }

    /**
     * 下线学生列表
     */
    public function student_list(){

        if($this -> request -> isPost()){
            $teacher_id = $_POST['teacher_id'];
            $salesman_id = $_POST['salesman_id'];
            $where = [
                'a.status' => 4,
                'a.pid' => $teacher_id
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['b.user_nickname'] = ['like',"%$keyword%"];
            }
            $data = Db::name('my_user') -> alias('a') -> field('a.*,b.user_nickname') -> join('user b','a.uid = b.id','left') -> where($where) -> paginate(1000000);
        }else{
            $id = $this -> request -> param();
            $teacher_id = $id['teacher_id'];
            $salesman_id = $id['salesman_id'];
            $data = Db::name('my_user') -> alias('a') -> field('a.*,b.user_nickname') -> join('user b','a.uid = b.id','left') -> where("a.status = 4 and a.pid =".$teacher_id) -> paginate(12);
        }

        $this -> assign('salesman_id',$salesman_id);
        $this -> assign('teacher_id',$teacher_id);
        $this -> assign('data',$data);
        return $this -> fetch();

    }

    /**
     * 业务员余额明细表
     */
    public function salesman_money(){

        $uid = $this -> request -> param();
        $data = Db::name('money_income') -> where('uid',$uid['uid'])->order('create_time','DESC') -> select() -> toArray();
        if(!empty($data)){
            foreach ($data as $key => $val){
                if($val['type'] == 2){
                    $data[$key]['indent'] = Db::name('indent') -> where('id',$val['indent_id']) -> find();
                    $indent_good = Db::name('indent_goods') -> where('indent_id',$val['indent_id']) -> select() -> toArray();
                    $indent_goods = '';
                    foreach ($indent_good as $key1 => $val1){
                        $indent_goods .= $val1['book_name'].',';
                    }
                    $data[$key]['indent_good'] = $indent_goods;
                }else{
                    $data[$key]['indent'] = Db::name('indent') -> where('id',$val['indent_id']) -> find();
                    $data[$key]['indent_good'] = $val['book_name'];
                }
            }
        }
        $this -> assign('data',$data);
        return $this -> fetch();

    }

    /**
     * 将老师和下线设置为业务员
     */
    public function set_free_man(){

        $id = $_POST['id'];
        //删除老师购物车 浏览记录 收藏 订单待付款信息
        //$teacher = Db::name('my_user') -> where('id',$id) -> find();
        /*Db::name('shopping_cart') -> where('uid',$teacher['uid']) -> delete();
        Db::name('browsing_history') -> where('uid',$teacher['uid']) -> delete();
        Db::name('collect') -> where('uid',$teacher['uid']) -> delete();
        Db::name('indent') -> where("uid=".$teacher['uid']." and state=4") -> delete();*/
        //获取这个老师下的所有学生信息
        //$student = Db::name('my_user') -> where('pid',$id) -> select();
        //循环删除学生的购物车 浏览记录 收藏 订单待付款信息
        /*foreach ($student as $key => $val){
            Db::name('shopping_cart') -> where('uid',$val['uid']) -> delete();
            Db::name('browsing_history') -> where('uid',$val['uid']) -> delete();
            Db::name('collect') -> where('uid',$val['uid']) -> delete();
            Db::name('indent') -> where("uid=".$val['uid']." and state=4") -> delete();
        }*/
        //改变老师的身份状态
        $data = Db::name('my_user') -> where('id',$id) -> update(['status'=>0,'pid'=>0,'is_pro'=>0,'bind_status'=>0,'is_withdrawal'=>1]);
        //改变学生的身份状态
        Db::name('my_user') -> where('pid',$id) -> update(['status'=>0,'pid'=>0,'bind_status'=>0]);
        if($data){
            return true;
        }else{
            return false;
        }

    }




























}