<?php

namespace app\home\controller;

use think\Lang;
use think\Validate;

class Jobcv extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'home/lang/' . config('default_lang') . '/jobcv.lang.php');
    }

    /**
     * 聊天历史管理
     */
    public function index()
    {
        $model_chatlog = Model('Chatlog');
        $erwh = array();
        $erwh['service_pid'] = session('service_pid');
        $allkf = Model('service')->allkef($erwh, $field = 'service_id,service_nickname');//所有客服
        if(request()->isPost()){
            $lyaddtime = $_POST['lyaddtime'];
        }else{
            $lyaddtime = 0;
        }

        if($lyaddtime==1){
            $kstime = strtotime(date("Y-m-d 23:59:59",strtotime("-3 day")));
        }elseif($lyaddtime==2){
            $kstime = strtotime(date("Y-m-d 23:59:59",strtotime("-1 week")));
        }elseif($lyaddtime==3){
            $kstime = strtotime(date("Y-m-d",strtotime("-1 month")));
        }else{
            $kstime = "1546272000";
        }
        $endtime = TIMESTAMP;

        if (input('post.kstime')) {
            $kstime = strtotime(input('post.kstime'));
        }
        if (input('post.endtime')) {
            $endtime = strtotime(input('post.endtime'));
        }

        $readok = input('post.record_readok');
        $condition = array();
        $condition['create_time'] = array('between', array($kstime, $endtime));
        if($readok){
            $condition['sid'] = $readok;
        }
        $condition['uid'] = session('service_pid');
        $record_list = $model_chatlog->getChatlogList($condition, '*', 10);
// var_dump($record_list);die;
        if($record_list){
            for ($i=0; $i < count($record_list); $i++) { 
                $where['log_id'] = $record_list[$i]["id"];
                $list[] = db('chat_message')->field('id,create_time,from')->where($where)->select();
            }
            // var_dump($list);die;
            if($list){
                for ($ii=0; $ii < count($list); $ii++) { 
                    for ($ie=0; $ie < count($list[$ii]); $ie++) { 
                        $who['id'] = $list[$ii][$ie]["id"];
                        $cjtime[] = date("Y-m-d H:i:s",$list[$ii][$ie]["create_time"]);
                        $conlist[] = db('chat_message_content')->where($who)->find();
                    }
                }
                $this->assign('conlist',$conlist);
            }
            //所属公司
            for ($i=0; $i < count($record_list); $i++) { 
                $ere['member_id'] = $record_list[$i]["uid"];
                $gslist[] = db('member')->field('member_name')->where($ere)->find();
            }
            $this->assign('gslist',$gslist);
            //所属客服
            for ($i=0; $i < count($record_list); $i++) { 
                $sere['service_id'] = $record_list[$i]["sid"];
                $kflist[] = db('service')->field('service_name')->where($sere)->find();
            }
            $this->assign('kflist',$kflist);
        }

        $allgs = Model('member')->allcompany('');
        $allpower = $this->qxhans();
        $this->assign('allgs',$allgs);
        $this->assign('allpower',$allpower);
        $this->assign('record_list', $record_list);
        $this->assign('lyaddtime',$lyaddtime);
        $this->assign('endtime',$endtime);
        $this->assign('kstime',$kstime);
        $this->assign('show_page', $model_chatlog->page_info->render());
        $this->assign('allkf',$allkf);
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    //客服下的访问客户
    public function kfvisitor(){
        $pid = $_GET['uid'];//客服id
        $kflist = model('Chatlog');
        $visitorlist = $kflist->allvisitor($pid);
        echo json_encode($visitorlist);
    }

    //客服客户聊天内容
    public function chatcontent(){
        $sid = $_GET['forWhat'];//客服id
        $kname = $_GET['kname'];//客户名
        $type = $_GET['type'];
        $choicetime = $_GET['choicetime'];
        if($type=="nearWeek"){
            $begin = strtotime(date('Y-m-d', strtotime('-7 days')));
        }else{
            $begin = strtotime("-0 year -1 month -0 day");
        }
        $end = TIMESTAMP;
        if(!empty($choicetime)){
            $gap = explode(' - ', $choicetime);
            $begin = strtotime($gap[0]);
            $end = strtotime($gap[1]);
        }

        $ltlist = model('Chatlog');
        $ltjt = $ltlist->chatdata($begin,$end,$kname,$sid);
        if($ltjt){
            for ($i=0; $i < count($ltjt); $i++) { 
                $where[' log_id'] = $ltjt[$i]["id"];
                $list[] = db('chat_message')->field('id,create_time,from')->where($where)->select();
            }
            if(!empty($list)){
                for ($i=0; $i < count($list); $i++) { 
                    for ($ie=0; $ie < count($list[$i]); $ie++) { 
                        $who['id'] = $list[$i][$ie]["id"];
                        $cjtime[] = date("Y-m-d H:i:s",$list[$i][$ie]["create_time"]);
                        $conlist[] = db('chat_message_content')->where($who)->find();
                    }
                }
            }
        }
        
        if(!empty($cjtime) && !empty($conlist) && !empty($kname)){
            $array = array(
                'list'=>$list,
                'cjtime'=>$cjtime,
                'conlist'=>$conlist, 
                'kname'=>$kname,
            );
        }else{
            $array = array(
                'list'=>'',
                'cjtime'=>'',
                'conlist'=>'',
                'kname'=>'',
            );
        }
        echo json_encode($array);
    }

    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('ds_manage'), 'url' => url('jobcv/index')
            )
        );
        return $menu_array;
    }

}

?>
