<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/


namespace app\admin\controller\bbfxshop\workorder;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Workorder extends Base
{
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Workorder;
    }
    
    
    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        $category_id = intval($params['category_id']);
        $where = ["a.uniacid"=>UNIACID];
        if($category_id > 0){
            $where['a.category_id'] = $category_id;
        }

        $keyword = trim($params['keyword']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        $status = trim($params['status']);
        $type = intval($params['type']);
        if($keyword){
            if($type == 1){
                $where['a.title'] = ['like',"%".$keyword."%"];
            }
            else if($type == 2){
                $where['b.nickname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 3){
                $where['a.user_id'] = intval($keyword);
            }
            
        }
        if($status != ''){
            if($status == 0){
                $where['a.status'] = 0;
            }
            else if($status == 1){
                $where['a.status'] = 1;
            }
            else if($status == 2){
                $where['a.status'] = 2;
                $where['a.is_check'] = 0;
            }
            else if($status == 3){
                $where['a.status'] = 2;
                $where['a.is_check'] = 1;
            }
            else if($status == 4){
                $where['a.status'] = 3;
            }
            
        }
        $whereTime = [];
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['a.createtime'] = ['>=',$startTime];
        } 
        
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['a.createtime'] = ['<=',$endTime];
        }
        
        $list = Db::name("bbfx_workorder")->alias("a")->join("bbfx_workorder_category c","a.category_id=c.id","LEFT")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->where($whereTime)->field("a.*,c.title as category_name,b.nickname,b.avatar")->order('a.id desc')->limit(($pindex - 1) * $psize,$psize)->select();
        $total = Db::name("bbfx_workorder")->alias("a")->join("bbfx_workorder_category c","a.category_id=c.id","LEFT")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->where($whereTime)->count();
        
        foreach ($list as $key => &$val){
            $val['avatar'] = cdnurl($val['avatar']);
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            if($val['status'] > 0) $val['cltime'] = date("Y-m-d H:i:s",$val['cltime']);
            if($val['status'] > 1) $val['completetime'] = date("Y-m-d H:i:s",$val['completetime']);
            if($val['status'] > 2) $val['closetime'] = date("Y-m-d H:i:s",$val['closetime']);
            $user = Db::name("bbfx_workorder_user")->where(['id'=>$val['fz_user_id']])->find();
            $val['user'] = $user;

            $val['images'] = cdnurls(iunserializer($val['images']));
            $val['reply_images'] = cdnurls(iunserializer($val['reply_images']));
            $prob_count = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$val['id']])->count();
            $val['prob_count'] = intval($prob_count);
            $val['status_text'] = $this->model->getStatusText($val['status'],$val['is_check'],$val['is_reply'],$val['prob_count']);
            
            $val['form'] = iunserializer($val['form']);
            foreach($val['form'] as $k1 => &$v1){
                if($v1['type'] == 'image'){
                    $v1['value'] = cdnurl($v1['value']);
                }
                else if($v1['type'] == 'images'){
                    foreach($v1['value'] as $k => &$v){
                        $v = cdnurl($v);
                    }
                    unset($v);
                }
            }
            unset($v1);

            $probe_list = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$val['id']])->order("id asc")->select();
            foreach($probe_list as $k => &$v){
                $v['images'] = cdnurls(iunserializer($v['images']));
                $v['reply_images'] = cdnurls(iunserializer($v['reply_images']));
                $v['createtime'] = date("Y-m-d H:i:s",$v['createtime']);
                if($v['replytime'] > 0){
                    $v['replytime'] = date("Y-m-d H:i:s",$v['replytime']);
                }
                else{
                    $v['replytime'] = '';
                }
            }
            unset($v);

            $val['probe_list'] = $probe_list;
        }
        unset($val);
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }

    public function allocate(){
        $id = $this->param("id/d");
        $user_id = $this->param("user_id/d");
        $result = $this->model->allocate($id,$user_id);
        __result($result);
    }
    public function transfer(){
        $id = $this->param("id/d");
        $user_id = $this->param("user_id/d");
        $result = $this->model->allocate($id,$user_id);
        __result($result);
    }
    
    public function detail(){
        $params = $this->param();
        
        $info = Db::name("bbfx_workorder")->where("id",$params['id'])->find();
        
        if(empty($info)){
            __error("数据不存在");
        }
        
        $info['createtime'] = date("Y-m-d H:i:s",$info['createtime']);
        if($info['status'] > 0) $info['cltime'] = date("Y-m-d H:i:s",$info['cltime']);
        if($info['status'] > 1) $info['completetime'] = date("Y-m-d H:i:s",$info['completetime']);
        if($info['status'] > 2) $info['closetime'] = date("Y-m-d H:i:s",$info['closetime']);
        $info['images'] = cdnurls(iunserializer($info['images']));
        $info['reply_images'] = cdnurls(iunserializer($info['reply_images']));
        $prob_count = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$info['id']])->count();
        $info['prob_count'] = intval($prob_count);
        $info['status_text'] = $this->model->getStatusText($info['status'],$info['is_check'],$info['is_reply'],$info['prob_count']);
        $info['form'] = iunserializer($info['form']);
        foreach($info['form'] as $key => &$val){
            if($val['type'] == 'image'){
                $val['value'] = cdnurl($val['value']);
            }
            else if($val['type'] == 'images'){
                foreach($val['value'] as $k => &$v){
                    $v = cdnurl($v);
                }
                unset($v);
            }
        }
        unset($val);

        $probe_list = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$info['id']])->order("id asc")->select();
        foreach($probe_list as $key => &$val){
            $val['images'] = cdnurls(iunserializer($val['images']));
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            if($val['replytime'] > 0){
                $val['replytime'] = date("Y-m-d H:i:s",$val['replytime']);
            }
            else{
                $val['replytime'] = '';
            }
        }
        unset($val);

        $info['probe_list'] = $probe_list;

        $log_list = Db::name("bbfx_workorder_log")->alias("a")->join("bbfx_workorder_user b","a.fz_user_id=b.id","LEFT")->where(['a.uniacid'=>UNIACID,'a.pid'=>$info['id']])->field("a.*,b.name as username")->order("a.id asc")->select();
        foreach($log_list as $key => &$val){
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $workorder = iunserializer($val['info']);
            $workorder['images'] = cdnurls(iunserializer($workorder['images']));
            if(!empty($workorder['probe'])){
                $workorder['probe']['images'] = cdnurls(iunserializer($workorder['probe']['images']));
            }
            $val['info'] = $workorder;
        }
        unset($val);

        $info['log_list'] = $log_list;
        
        __success('操作成功',$info);
    }


    
    public function delete(){
        $this->commonDelete('bbfx_workorder');
    }
}