<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\DictApi;
use app\common\api\UploadApi;
use think\facade\Db;

class CurrencyFiletype extends AdminController{

    public function index(){
        return view("");
    }

    public function list(){
        $order = trim($this->request->param("order")) ?: "desc";
        $offset = trim($this->request->param("offset")) ?: 0;
        $limit = trim($this->request->param("limit")) ?: 10;
        $type = trim($this->request->param("type"));
        $name = trim($this->request->param("name"));
        $must = trim($this->request->param("must"));
        $active = trim($this->request->param("active"));

        $where = [];

        if ($name) {
            $where[] = ["name", "like", "%" . $name . "%"];
        }
        if ($type) {
            $where[] = ["type", "=", $type];
        }
        if ($must) {
            $where[] = ["must", "=", $must];
        }
        if ($active) {
            $where[] = ["active", "=", $active];
        }


        $count = \app\admin\model\CurrencyFiletype::where($where)->count();
        if($count > 0){
            $rows = \app\admin\model\CurrencyFiletype::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
            $typeName = DictApi::selectByParentCode("common_declareType");
            foreach ($rows as $k => &$v){
                $v['typeName'] = $typeName[$v['type']];
                $v['templateUrl'] = getStoragePath($v['templateUrl']);
            }
        }else{
            $rows = [];
        }

        return ["total" => $count, "rows" => $rows];
    }

    public function currencyFiletype_add(){
        return view("");
    }

    public function add(){
        $response_obj = new \StdClass();
        $name = trim($this->request['name']);
        if(\StrUtil::isEmpOrNull($name)){
            $response_obj->code = 500;
            $response_obj->msg = '附件名称不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $type = trim($this->request['type']);
        if(\StrUtil::isEmpOrNull($type)){
            $response_obj->code = 500;
            $response_obj->msg = '申报类别不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $api = trim($this->request['api']);
        if(\StrUtil::isEmpOrNull($api)){
            $response_obj->code = 500;
            $response_obj->msg = 'api不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $must = trim($this->request['must']);
        if(\StrUtil::isEmpOrNull($must)){
            $response_obj->code = 500;
            $response_obj->msg = '是否必传不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $active = trim($this->request['active']);
        if(\StrUtil::isEmpOrNull($active)){
            $response_obj->code = 500;
            $response_obj->msg = '是否有效不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $sn = trim($this->request['sn']);
        if(\StrUtil::isEmpOrNull($sn)){
            $response_obj->code = 500;
            $response_obj->msg = '排序不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }


        if($this->request['templateFile']){
            $uploadapi = new UploadApi();
            $file_check_res = $uploadapi->uploadOne($this->request->file('templateFile'), 'system');
            if ($file_check_res->code == 500) {
                return \StrUtil::back($file_check_res, "CurrencyFiletypeInfoDlg.callBack");
            }
            $templateUrl = $file_check_res->filepath;
        }else{
            $templateUrl = "";
        }


        $data = [
            'id' => getStringId(),
            'type' => $type,
            'name' => $name,
            'api' => $api,
            'must' => $must,
            'active' => $active,
            'sn' => $sn,
            'checkCompany' => '1158293574504660993',
            'description' => $this->request['description'],
            'createTime' => date("Y-m-d H:i:s"),
            'createUser' => session('user')['uid'],
            'templateUrl' => $templateUrl
        ];

        \app\admin\model\CurrencyFiletype::create($data);
        $response_obj->code = 200;
        $response_obj->msg = '添加成功';
        return \StrUtil::back($response_obj, "CurrencyFiletypeInfoDlg.callBack");
    }

    public function currencyFiletype_update(){
        $id = trim($this->request->get('id'));
        if(\StrUtil::isEmpOrNull($id)){
            return json(['msg'=>'id不能为空']);
        }
        $cft = \app\admin\model\CurrencyFiletype::findOrEmpty($id);
        return view("",['item'=>$cft]);
    }

    public function update(){
        $response_obj = new \StdClass();
        $id = \StrUtil::getRequestDecodeParam($this->request,'id');
        if(\StrUtil::isEmpOrNull($id)){
            $response_obj->code = 500;
            $response_obj->msg = 'id不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $cft = \app\admin\model\CurrencyFiletype::findOrEmpty($id);
        if(!$cft){
            $response_obj->code = 500;
            $response_obj->msg = '找不到记录';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }

        $cft->name = $name = trim($this->request['name']);
        if(\StrUtil::isEmpOrNull($name)){
            $response_obj->code = 500;
            $response_obj->msg = '附件名称不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $cft->type = $type = trim($this->request['type']);
        if(\StrUtil::isEmpOrNull($type)){
            $response_obj->code = 500;
            $response_obj->msg = '申报类别不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $cft->api = $api = trim($this->request['api']);
        if(\StrUtil::isEmpOrNull($api)){
            $response_obj->code = 500;
            $response_obj->msg = 'api不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $cft->must = $must = trim($this->request['must']);
        if(\StrUtil::isEmpOrNull($must)){
            $response_obj->code = 500;
            $response_obj->msg = '是否必传不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $cft->active = $active = trim($this->request['active']);
        if(\StrUtil::isEmpOrNull($active)){
            $response_obj->code = 500;
            $response_obj->msg = '是否有效不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $cft->sn = $sn = trim($this->request['sn']);
        if(\StrUtil::isEmpOrNull($sn)){
            $response_obj->code = 500;
            $response_obj->msg = '排序不能为空';
            return \StrUtil::back($response_obj,'CurrencyFiletypeInfoDlg.callBack');
        }
        $files = $this->request->file();


        if(array_key_exists('templateFile',$files)){
            $uploadapi = new UploadApi();
            $file_check_res = $uploadapi->uploadOne($this->request->file('templateFile'), 'system');
            if ($file_check_res->code == 500) {
                return \StrUtil::back($file_check_res, "CurrencyFiletypeInfoDlg.callBack");
            }
            $cft->templateUrl = $file_check_res->filepath;
        }else{
            $cft->templateUrl = "";
        }

        $cft->updateTime = date("Y-m-d H:i:s");
        $cft->updateUser = session('user')['uid'];
        $cft->save();
        $response_obj->code = 200;
        $response_obj->msg = '修改成功';
        return \StrUtil::back($response_obj, "CurrencyFiletypeInfoDlg.callBack");
    }

    public function delete(){
        $id = \StrUtil::getRequestDecodeParam($this->request,'currencyFiletypeId');
        if(\StrUtil::isEmpOrNull($id)){
            return json(['code' => 500,'msg' => '缺少参数']);
        }
        \app\admin\model\CurrencyFiletype::destroy($id);
        return json(['code' => 200,'msg' => '删除成功']);
    }
}