<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">
              <a-switch
                :checked="manualType"
                checked-children="检修"
                un-checked-children="维修"
                @change="changeType"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="作业项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manualName">
              <a-input v-model="model.manualName" placeholder="请输入作业项"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceTypeId">
              <a-select placeholder="请选择设备" :options="deviceTypes" v-model="model.deviceTypeId"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="部件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
              <a-tree-select
                v-model="model.departId"
                :disabled="!model.deviceTypeId"
                :tree-data="departs"
                placeholder="请选择部件"
                tree-default-expand-all
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="manualType" label="修程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repairRange">
              <j-dict-select-tag v-model="model.repairRange" placeholder="请选择修程" dict-code="repair_range"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="检修项/故障类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regularDetailId">
              <a-input v-if="manualType" v-model="model.regularDetailId" placeholder="请输入检修项/故障类型"></a-input>
              <j-dict-select-tag v-if="!manualType" v-model="model.regularDetailId" placeholder="请输入检修项/故障类型" dict-code="breakdown_type"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="技术要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-input v-model="model.content" placeholder="请输入技术要求文字说明"  ></a-input>
            </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: 'ManualForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        manualType: true,//指导书类型true检修，false维修
        departs: [],
        deviceTypes: [],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           repairRange: [
              { required: true, message: '请选择修程!'},
           ],
           regularDetailId: [
              { required: true, message: '请输入检修项/故障类型!'},
           ],
           manualName: [
              { required: true, message: '请输入作业项!'},
           ],
           deviceTypeId: [
              { required: true, message: '请选择设备!'},
           ],
        },
        url: {
          add: "/manual/manual/add",
          edit: "/manual/manual/edit",
          queryById: "/manual/manual/queryById",
          listDeparts: '/device/buDeviceType/list',
          listDeviceType:"/device/buDeviceType/list/all",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    watch: {
      'model.deviceTypeId'(newVal, oldVal) {
        this.loadDeparts(newVal);
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.loadAllDeviceType();
    },
    methods: {
      changeType() {
        this.manualType=!this.manualType
      },
      convertToTreeSelect (data) {
        return data.map(item => {
          // 创建基本节点
          const node = {
            key: item.id,
            value: item.id,
            title: item.typeName
          };
          // 如果有子节点，递归处理
          if (item.subTypes && item.subTypes.length > 0) {
            node.children = this.convertToTreeSelect(item.subTypes);
          }
          return node;
        });
      },
      loadDeparts(id) {
        getAction(this.url.listDeviceType).then((res) => {
          if (res.success) {
            const targetItem = res.result.find(item => item.id === id);
            const subTypes = targetItem.subTypes;
            this.departs = this.convertToTreeSelect(subTypes)
          } else {
            this.$message.error(res.message)
          }
        })
      },
      loadAllDeviceType() {
        getAction(this.url.listDeviceType, {calss: '1'}).then((res) => {
          if (res.success) {
            this.deviceTypes = res.result.map(item => ({
              value: item.id,
              label: item.typeName
            }))
          } else {
            this.$message.error(res.message)
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.manualType = record.repairRange!==null;
        this.visible = true;
      },
      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;
            })
          }

        })
      },
    }
  }
</script>