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

namespace app\api\controller\workorder;

use app\api\controller\Base;
use think\Db;


class Manage extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Workorder;
    }
    
    public function complete(){
        $id = $this->param("id/d");
        $reply_id = $this->param("reply_id/d");
        $content = $this->param("content/s");
        $images = $this->param("images/a");
        $result = $this->model->complete($id,$content,$images,$reply_id,$this->user['id']);
        __result($result);
    }

    public function close(){
        $id = $this->param("id/d");
        $result = $this->model->close($id,$this->user['id'],1);
        __result($result);
    }

    public function allocate(){
        $id = $this->param("id/d");
        // $user_id = $this->param("user_id/d");

        $user = Db::name("bbfx_workorder_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id']])->find();
        if(empty($user)){
            __error("你还不是工单用户，无法接单");
        }

        $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 getUserList(){
        $category_id = $this->param("category_id/d");
        $where = ['uniacid'=>UNIACID];
        if($category_id > 0){
            $where['category_id'] = $category_id;
        }
        $list = Db::name("bbfx_workorder_user")->where($where)->order("id desc")->select();

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

    public function getAllocateList(){

        $user = Db::name("bbfx_workorder_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id']])->find();
        
        $list = Db::name("bbfx_workorder")->where(['uniacid'=>UNIACID,'status'=>0,'category_id'=>$user['category_id']])->field("id,title,status,is_reply,is_check,content,createtime")->order("id desc")->select();
        
        foreach($list as $key => &$val){
            $prob_count = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$val['id']])->count();
            $val['prob_count'] = intval($prob_count);
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['status_text'] = $this->model->getStatusText($val['status'],$val['is_check'],$val['is_reply'],$val['prob_count']);
        }
        unset($val);

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

    public function getList(){

        $user = Db::name("bbfx_workorder_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id']])->find();

        $list = Db::name("bbfx_workorder")->where(['uniacid'=>UNIACID,'fz_user_id'=>$user['id']])->field("id,title,status,is_check,is_reply,content,createtime")->order("id desc")->select();
        
        foreach($list as $key => &$val){
            $prob_count = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$val['id']])->count();
            $val['prob_count'] = intval($prob_count);
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['status_text'] = $this->model->getStatusText($val['status'],$val['is_check'],$val['is_reply'],$val['prob_count']);
        }
        unset($val);

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

    public function detail(){
        $id = $this->param("id/d");

        $fz_user = Db::name("bbfx_workorder_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id']])->find();

        $info = Db::name("bbfx_workorder")->where(['uniacid'=>UNIACID,'id'=>$id])->find();

        if(empty($info)){
            __error("工单不存在");
        }

        if($info['status'] > 0 && $info['fz_user_id'] != $fz_user['id']){
            __error("无权限查看");
        }
        $prob_count = Db::name("bbfx_workorder_probe")->where(['uniacid'=>UNIACID,'pid'=>$info['id']])->count();
        $info['prob_count'] = intval($prob_count);
        $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['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")->alias("a")->join("bbfx_workorder_user b","a.fz_user_id=b.id","LEFT")->join("bbfx_user c","b.user_id=c.id","LEFT")->where(['a.uniacid'=>UNIACID,'a.pid'=>$info['id']])->field("a.*,b.name as username,c.avatar")->order("a.id asc")->select();
        foreach($probe_list as $key => &$val){
            $val['avatar'] = cdnurl($val['avatar']);
            $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;

        $user = Db::name("bbfx_user")->where(['id'=>$info['user_id']])->field("id,nickname,avatar")->find();
        if(!empty($user)){
            $user['avatar'] = cdnurl($user['avatar']);
            $info['user'] = $user;
        }
        $fz_user['avatar'] = $this->user['avatar'];
        $info['fz_user'] = $fz_user;

        __success("操作成功",$info);
    }
}