<?php

namespace Admin\Api\WorkOrder;

use Base\Domain\WorkOrder as WorkOrderDomain;
use Admin\Common\Api;

/**
 * 工单
 */
class WorkOrder extends Api {

    public function getRules() {
        return array(
            'getModuleLst' => array(
            ),
            'addWorkOrder' => array(
                'title' => array('name' => 'title', 'require' => true, 'max' => '255', 'desc' => '工单标题'),
                'moduleId' => array('name' => 'module_id', 'require' => true, 'type'=>'int',   'desc' => '模块id'),
                'problemDesc' => array('name' => 'problem_desc', 'require' => true, 'desc' => '工单工单描述 '),
                'phone' => array('name' => 'phone', 'require' => true, 'desc' => '联系手机'),
                'fileIds' => array('name' => 'file_ids', 'require' => true, 'desc' => '附件ids 多个文件用，号隔开'),
            ),
            'lstWorkOrder' => array(
                'moduleId'=> array('name' => 'module_id', 'type' => 'int', 'desc' => '模块类型'),
                'status' => array('name' => 'status', 'type' => 'int', 'desc' => '审核状态：1待受理，2待回复 3已解决 4已关闭'),
                'title' => array('name' => 'title', 'desc' => '标题搜索'),
                'page' => array('name' => 'page', 'type' => 'int', 'default' => 1,  'desc' => '当前页数'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'default' => 10, 'desc' => '每页多少条'),
            ),
            'detailWorkOrder' => array(
                'id' => array('name' => 'id', 'require' => true, 'type' => 'int', 'min' => 1, 'desc' => '工单id'),
            ),
            'updateChatRecodeWorkOrder' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '工单id'),
                'status' => array('name' => 'status', 'desc' => '审核'),
                'chatRecode' => array('name' => 'chat_recode', 'max' => '255', 'desc' => '回复'),
                'chatRecodeFile' => array('name' => 'chat_recode_file','max' => '255', 'desc' => "回复时的附件 形式['url','url']"),
            )
        );
    }

    /**
     * 获得，模块列表接口
     * @desc 工单 获得，模块列表接口
     */
    public function getModuleLst(){
        $workOrderDomain = new WorkOrderDomain();
        return  $workOrderDomain->getModuleLst();
    }

    /**
     * 工单列表
     *@desc 工单列表
     */
    public function lstWorkOrder(){
        $workOrderDomain = new WorkOrderDomain();
        $searchWord = [
            'status'=>$this->unntilIsEmpty($this->status),
            'module_id'=>$this->unntilIsEmpty($this->moduleId),
        ];
        if (!empty($this->title)){
            $searchWord['title LIKE ?'] = "%{$this->title}%";
        }
        //数组去空
        $searchWord =  array_filter($searchWord);
        $rs = $workOrderDomain->adminWorkOrderLst($searchWord, $this->page, $this->perpage);

        if (empty($rs)){
            $rs['total'] = 0;
            $rs['items'] = [];
        }
        return $rs;
    }

    /**
     * 工单详情
     * @desc 工单列表
     *
     * @return int id 这是id
     */
    public function detailWorkOrder()
    {
        $workOrderDomain = new WorkOrderDomain();
        $rs = $workOrderDomain->adminDetail($this->id);
        return $rs;
    }

    /**
     * 工单更新
     * @desc 工单更新
     */
    public function updateChatRecodeWorkOrder(){
        $workOrderDomain = new WorkOrderDomain();
        $workOrderDomain->updateChatRecodeWorkOrderAdmin($this->id, $this->status, $this->chatRecode, $this->chatRecodeFile);
        return true;
    }

}
