<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">
              <a-select :disabled="isEdit" v-model="model.deviceId" placeholder="请选择的设备" >
                <a-select-option v-for="i in deviceSet" :key="i.deviceId" :value="i.deviceId">
                  {{ i.deviceName+'('+i.deviceCode+')' }}
                </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="modelType">
              <j-dict-select-tag :disabled="isEdit" v-model="model.modelType" dictCode="work_order" placeholder="请选择工单类型" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="工单模板" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modelId">-->
<!--              <a-select v-model="model.modelId" placeholder="请选择工单模板" :options="selectOptions"></a-select>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">
            <a-form-model-item label="来源计划" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planId">
              <a-input v-model="model.planId" placeholder="请输入检修计划/维修计划"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="修程/故障类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regularId">
<!--              <a-input v-model="model.regularId" placeholder="请输入修程/故障类型"  ></a-input>-->
              <j-dict-select-tag :disabled="isEdit" v-model="model.modelType" dictCode="repair_range" placeholder="请选择修程" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.id" label="工单状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag v-model="model.status" dictCode="work_order_status" placeholder="请选择工单状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工单负责人" v-if="model.id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coordinator">
              <a-input :disabled="true" v-model="model.coordinator_dictText" placeholder="请输入工单负责人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工单配合人" v-if="model.id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executor">
              <a-input :disabled="true" v-model="model.executor_dictText" placeholder="请输入工单配合人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date :disabled="model.id!=null" placeholder="请选择开工时间"  v-model="model.startTime" :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="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 label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" 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: 'WorkOrderForm',
    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: {
           modelType: [
              { required: true, message: '请输入工单类型!'},
           ],
           modelId: [
              { required: true, message: '请输入工单模板!'},
           ],
           deviceId: [
              { required: true, message: '请输入设备!'},
           ],
           status: [
              { required: true, message: '请输入工单状态!'},
           ],
        },
        url: {
          add: "/workOrder/workOrder/add",
          edit: "/workOrder/workOrder/edit",
          queryById: "/workOrder/workOrder/queryById"
        },
        selectOptions: [],
        deviceSet:{},
        isEdit: false,
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.queryMap();
      this.getDevice();
    },
    methods: {
      async add(item) {
        if (item.id) {
          this.isEdit = true;
          this.modelDefault.deviceId = item.deviceId;
          this.modelDefault.planId = item.id;
          this.modelDefault.modelType = item.rapairRange;
          // let params = {
          //   pageNo: 1,
          //   pageSize: 1,
          //   rapairRange: item.rapairRange,
          //   deviceId: item.deviceId,
          // };
          // //根据设备id查询设备类型id，再联合修程查询regularId，然后查询对应的workordermodel
          // const res = await getAction('/workOrder/workOrderModel/queryOne', params);
          // if (res.success && res.result != null) {
          //   this.modelDefault.regularId = res.result.regularId;
          //   this.modelDefault.modelId = res.result.id;
          // }
        }

        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        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';
              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;
            })
          }

        })
      },
      queryMap() {
        getAction("/workOrder/workOrderModel/querymap" ).then((res) => {
          if (res.success) {
            this.selectOptions = res.result;
          }
        })
      },
      getDevice(){
        getAction("/device/buDeviceOnline/getDevice").then(res => {
          if (res.success) {
            this.deviceSet = res.result;
          }
        })
      },
    }
  }
</script>