<?php
/**
 *  移动端进客
 *
 * ============================================================================
 * 版权所有 2017北京素玄科技，并保留所有权利。
 *
 * 网站地址: http://www.suxuantech.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经允许的情况下，您不能对本系统代码做任何修改 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如有修改需求，请联系素玄科技有限公司：contact@suxuantech.cn
 * ============================================================================
 * $Author: songdemei<songdemei@suxuantech.cn> 2019-10-20 16:47:11$
 */
namespace app\mobile\controller;

class Intostore extends Base
{
    
    function index()
    {
        $workTypes = explode(',', session('work_type'));
        if(!in_array(1, $workTypes) && !in_array(31, $workTypes)){
            session('return_url','/mobile/intostore/index');
            echo '当前操作必须是门市工作类型或店长工作类型才可操作。<br/>';
            echo '<a href="#3" onclick="location.reload();">重新登录</a>';
            echo '<script type="text/javascript" src="/crm/customer/logout.html"></script>';
            die;
        }
        if(IS_AJAX){
            /**
             
        -- 查当天未进店客资
select 
i.yjd_day,i.syjd_day,c.customer_name,c.customer_tel,c.customer_wechat,c.customer_sex,i.reception_name,
from c_customer_intostore i 
left join c_customerinfo c on c.id = i.cid 
where  c.is_status = 1 and i.status = 1 and i.yjd_day like '2020-03-30%' and i.consulation_shop_code = 'VVC001' and i.is_intostore in (1,4)

-- 查当天已进店未分配门市的客资

select 
i.yjd_day,i.syjd_day,c.customer_name,c.customer_tel,c.customer_wechat,c.customer_sex,i.reception_name,
from c_customer_intostore i 
left join c_customerinfo c on c.id = i.cid 
where c.is_status = 1 and i.status = 1 
and i.yjd_day like '2020-03-30%' 
and i.consulation_shop_code = 'VVC001' 
and i.is_intostore in (2,3,5) 
and reception_id is null 

-- 查当天已进店已分配门市的客资

select 
i.yjd_day,i.syjd_day,c.customer_name,c.customer_tel,c.customer_wechat,c.customer_sex,i.reception_name,
from c_customer_intostore i 
left join c_customerinfo c on c.id = i.cid 
where c.is_status = 1 and i.status = 1 
and i.yjd_day like '2020-03-30%' 
and i.consulation_shop_code = 'VVC001' 
and i.is_intostore in (2,3,5) 
and reception_id is not null 



             * 
             */
            $tab = input('tab');
            $date = input('date');
            $date = $date?$date.'%':date('Y-m-d').'%';
            $shopCode = session('shop_code');
            $tab = $tab?$tab:2;
            if($tab == 1){
                $sql = '
                    select 
                    i.id,i.yjd_day,i.syjd_day,c.id customer_id,c.customer_name,c.customer_tel,c.mate_tel,c.customer_wechat,c.customer_sex,i.reception_name,i.reception_id,i.is_intostore
                    from c_customer_intostore i 
                    left join c_customerinfo c on c.id = i.cid 
                    where  c.is_status = 1 and i.status = 1 and i.yjd_day like \''.$date.'\' and i.consulation_shop_code = \''.$shopCode.'\' and i.is_intostore in (1,4)
                    ';


            }elseif ($tab == 2) {
                $sql = '
                    select i.id,i.yjd_day,i.syjd_day,c.id customer_id,c.customer_name,c.mate_tel,c.customer_tel,c.customer_wechat,c.customer_sex,i.reception_name,i.reception_id,i.is_intostore
                    from c_customer_intostore i 
                    left join c_customerinfo c on c.id = i.cid 
                    where c.is_status = 1 and i.status = 1 
                    and i.yjd_day like \''.$date.'\' 
                    and i.consulation_shop_code = \''.$shopCode.'\' 
                    and i.is_intostore in (2,3,5) 
                    and i.reception_id is null 
                    order by i.syjd_day desc
                ';
            }elseif ($tab == 3) {
                $sql = '
                select i.id,i.yjd_day,i.syjd_day,c.id customer_id,c.customer_name,c.mate_tel,c.customer_tel,c.customer_wechat,c.customer_sex,i.reception_name,i.reception_id,i.is_intostore
                    from c_customer_intostore i 
                    left join c_customerinfo c on c.id = i.cid 
                    where c.is_status = 1 and i.status = 1 
                    and i.yjd_day like \''.$date.'\' 
                    and i.consulation_shop_code = \''.$shopCode.'\'
                    and i.is_intostore in (2,3,5) 
                    and i.reception_id is not null
                    order by i.syjd_day desc
                ';
            }else{
                return ['code'=>100,'msg'=>'无效的请求。'];
            }
            $data = db('')->query($sql);
            /*
            $data = [
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'reception_id'=>12,
                        'reception_name'=>'张三',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'reception_name'=>'',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],
                ];
                //*/
            return ['code'=>200,'msg'=>'','data'=>$data];
            /*
            $arr3 = [
                'code'=>0,
                'msg'=>'',
                'data'=>
                [
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'reception_id'=>12,
                        'reception_name'=>'张三',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'reception_name'=>'',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],
                ]
            ];
            $arr2 = [
                'code'=>0,
                'msg'=>'',
                'data'=>
                [
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'reception_id'=>12,
                        'reception_name'=>'张三',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12342,
                        'customer_name'=>'12342',
                        'customer_tel'=>'13212344322',
                        'yjd_day'=>'2019-10-12 12:22:33',
                        'reception_name'=>'',
                        'is_intostore'=>'未进店',
                    ],
                    [
                        'customer_id'=>12341,
                        'customer_name'=>'12341',
                        'customer_tel'=>'13212344321',
                        'yjd_day'=>'2019-10-12 12:12:33',
                        'is_intostore'=>'未进店',
                    ],

                ] 
            ];//*/
        }


        return $this->fetch();
    }
    public function updateReception(){

        if(request()->isPost()){
            $receptionId = input('reception_id/d');
            $receptionName = input('reception_name');
            $id = input('id');
            if(!$receptionId || !$id){
                return ['code'=>100,'msg'=>'参数不全'];
            }
            $staffId = session('staff_id');
            $staffName = session('staff_name');
            $data = [
                'id'=>$id,
                'reception_id'=>$receptionId,
                'reception_name'=>$receptionName,
                'staff_id'=>$staffId,
                'staff_name'=>$staffName,
            ];
            $res = controller('index/intostore','api')->allotReception($data);
            if($res['error']){
                return ['code'=>$res['code'],'msg'=>$res['msg']];
            }else{
                return ['code'=>200];
            }

        }
        
        //取所有门市人员
        $shopCode = session('shop_code');

        $url = '5a1510583e3a5';
        $data['shop_code'] = $shopCode;
        $data['work_type'] = MS_WORK_TYPE;
        $data['get_leave']  = 2;//只取在职的
        $temp_staffs = sx_call($url,$data);
        if ($temp_staffs['code'] == 200) {
            $reception = $temp_staffs['data'];
        }
        $this->assign('oldid',input('oldid'));
        $this->assign('reception',$reception);
        return $this->fetch();
    }
}