<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工具" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="toolId">
              <a-select :disabled="this.model.id" v-model="model.toolId" placeholder="请选择的设备">
                <a-select-option v-for="i in checkTools" :key="i.id" :value="i.id">
                  {{ i.toolId_dictText+'['+i.code+']' }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item  label="检定机构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificationBody">
              <a-input :disabled="this.model.id" v-model="model.certificationBody" placeholder="请输入检定机构"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="this.model.id" label="检定结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="check_status" placeholder="请选择检定结果" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="this.model.id" label="完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="finishTime">
              <j-date placeholder="请选择检定完成时间"  v-model="model.finishTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="this.model.id" label="报告编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificationCode">
              <a-input v-model="model.certificationCode" placeholder="请输入检定报告编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="this.model.id" label="检定报告" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificationReport">
              <j-upload :disabled="formDisabled" v-model="model.certificationReport" text="检定报告上传" fileType="all" :multiple="false"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ToolCheckForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        fileList: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           toolId: [
              { required: true, message: '请输入工具!'},
           ],
           status: [
              { required: true, message: '请选择检定结果!'},
           ],
           finishTime: [
              { required: true, message: '请选择检定完成时间!'},
           ],
           certificationCode: [
              { required: true, message: '请输入检定报告编号!'},
           ],
           certificationReport: [
              { required: true, message: '请输入检定报告!'},
           ],
        },
        checkTools:[],
        url: {
          add: "/toolCheck/toolCheck/add",
          edit: "/toolCheck/toolCheck/edit",
          tml: "/toolCheck/toolCheck/tml",
          queryById: "/toolCheck/toolCheck/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getCheckTools();
    },
    methods: {
      add (toolId, toolName) {
        this.modelDefault.toolId = toolId;
        this.modelDefault.toolName = toolName;
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        // 如果有已存在的文件，设置fileList
        if (this.model.certificationReport) {
          this.fileList = [{
            uid: '-1',
            name: this.model.certificationReport,
            status: 'done',
            url: this.model.certificationReport
          }];
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      getCheckTools() {
        getAction(this.url.tml).then(res=>{
          if(res.success) {
            this.checkTools = res.result;
          }
        })
      },
      beforeUpload(file) {
        const isLt10M = file.size / 1024 / 1024 < 10;
        if (!isLt10M) {
          this.$message.error('文件大小不能超过10MB!');
          return false;
        }
        return true;
      },
      handleRemove() {
        this.fileList = [];
        this.model.certificationReport = '';
      },
      handleUpload({ file }) {
        const formData = new FormData();
        formData.append('file', file);

        // 这里需要替换成实际的文件上传接口
        httpAction('/sys/common/upload', formData, 'post').then((res) => {
          if (res.success) {
            this.model.certificationReport = res.message;
            this.$message.success('文件上传成功');
          } else {
            this.$message.error('文件上传失败');
          }
        });
      },
    }
  }
</script>