<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="deviceId">
              <j-dict-select-tag :disabled="model.id!=null" type="list" v-model="model.deviceId" dictCode="v_bu_device,name,id" placeholder="请选择设备" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="部件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
              <a-tree-select
                :disabled="model.id!=null"
                :trigger-change="true"
                v-model="model.departId"
                :tree-data="deviceParts"
                placeholder="请选择设备部件"
                tree-default-expand-all
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="breakOrigin">
              <j-dict-select-tag :disabled="model.id!=null" type="list" v-model="model.breakOrigin" dictCode="breakdown_origin" placeholder="请选择故障来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="breakdownType">
              <j-dict-select-tag :disabled="model.id!=null" type="list" v-model="model.breakdownType" dictCode="breakdown_type" placeholder="请选择故障类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input :disabled="model.id!=null" v-model="model.description" placeholder="请输入故障描述"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障影响" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="breakImpact">
              <a-input v-model="model.breakImpact" placeholder="请输入故障影响"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reason">
              <a-input v-model="model.reason" placeholder="请输入故障原因"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picture">
              <j-image-upload isMultiple  v-model="model.picture" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">
                      <a-form-model-item label="审核人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="aduitBy">
                        <j-dict-select-tag type="list" v-model="model.aduitBy" dictCode="sys_user,realname,id" placeholder="请选择审核人" />
                      </a-form-model-item>
                    </a-col>-->
          <a-col :span="24">
            <a-form-model-item v-if="model.status=='3'" label="驳回原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="aduitRemark">
              <a-input v-model="model.aduitRemark" placeholder="请输入驳回原因"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">
                      <a-form-model-item label="故障代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="breakdownId">
                        <j-dict-select-tag type="list" v-model="model.breakdownId" dictCode="breakdown_status" placeholder="请选择故障代码" />
                      </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: 'DeviceBreakdownReportForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        deviceId: [
          { required: true, message: '请输入设备!' }
        ],
        // departId: [
        //   { required: true, message: '请输入部件!' }
        // ],
        breakOrigin: [
          { required: true, message: '请输入故障来源!' }
        ],
        breakdownType: [
          { required: true, message: '请输入故障类型!' }
        ],
        description: [
          { required: true, message: '请输入故障描述!' }
        ],
        status: [
          { required: true, message: '请输入状态!' }
        ],
        aduitBy: [
          { required: true, message: '请输入审核人!' }
        ]
      },
      deviceParts: [],
      url: {
        add: "/device/deviceBreakdownReport/add",
        edit: "/device/deviceBreakdownReport/edit",
        queryById: "/device/deviceBreakdownReport/queryById"
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  watch: {
    'model.deviceId' (newVal) {
      this.loadPartByDevice(newVal);
    }
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    convertToTreeSelect (data) {
      return data.map(item => {
        const title = item.name+'('+item.serialCode+')';
        // 创建基本节点
        const node = {
          key: item.id,
          value: item.id,
          title: title
        };
        // 如果有子节点，递归处理
        if (item.subTypes && item.subTypes.length > 0) {
          node.children = this.convertToTreeSelect(item.subTypes);
        }
        return node;
      });
    },
    loadPartByDevice(id) {
      getAction("/device/part/list", {pageNo:1,pageSize:50,device: id}).then((res) => {
        if (res.success) {
          this.deviceParts = this.convertToTreeSelect(res.result.records)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      if(that.model.status==='3'&&that.model.aduitRemark==null){
        that.$message.error("请输入驳回原因！")
        return
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
            this.model.status = 0;
          }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;
          })
        }

      })
    },
  }
}
</script>