<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="dynamicValidatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workOrder">
              <a-input :disabled="model.id!=null" v-model="model.workOrder" placeholder="请输入工单"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请点站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="permitStation">
              <a-tree-select
                v-decorator="['permitStation']" :trigger-change="true"
                v-model="model.permitStation"
                :tree-data="deviceLocations"
                placeholder="请选择请点站"
                :disabled="model.id!=null"
                tree-default-expand-all
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请点人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="permitUser">
              <a-input :disabled="model.id!=null" v-model="model.permitUser" placeholder="请输入请点人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请点时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="permitTime">
              <j-date :disabled="model.id!=null" placeholder="请选择请点时间" v-model="model.permitTime" :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="permitOfficer">
              <a-input :disabled="model.comple===1" v-model="model.permitOfficer" placeholder="请输入站点值班员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.comple" label="销点站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="compleStation">
              <a-tree-select
                v-decorator="['compleStation']" :trigger-change="true"
                v-model="model.compleStation"
                :tree-data="deviceLocations"
                placeholder="请选择销点站"
                tree-default-expand-all
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.comple" label="销点人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="compleUser">
              <a-input v-model="model.compleUser" placeholder="请输入销点人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.comple" label="销点站值班员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="compleOfficer">
              <a-input v-model="model.compleOfficer" placeholder="请输入站点值班员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.comple" label="销点时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="compleTime">
              <j-date placeholder="请选择销点时间" v-model="model.compleTime" :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="ramark">
              <a-input v-model="model.ramark" 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'
import moment from 'dayjs'
import store from '@/store'

export default {
  name: 'WorkPermitCompleteForm',
  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: {
        workOrder: [
          { required: true, message: '请输入工单!' }
        ],
        permitStation: [
          { required: true, message: '请输入请点站!' }
        ],
        permitTime: [
          { required: true, message: '请输入请点时间!' }
        ],
        permitUser: [
          { required: true, message: '请输入请点人!' }
        ],
      },
      url: {
        add: '/workOrder/workPermitComplete/add',
        edit: '/workOrder/workPermitComplete/edit',
        queryById: '/workOrder/workPermitComplete/queryById',
        listDeviceLocation:"/device/deviceLocation/list/all"
      },
      deviceLocations:[],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    dynamicValidatorRules() {
      const rules = { ...this.validatorRules }
      // 只有是销点操作的时候才判断销点字段是否必填
      if (this.model.comple && this.model.id && this.model.permitStation && this.model.permitTime && this.model.permitUser && this.model.permitOfficer) {
        rules.compleStation = [{ required: true, message: '请输入销点站!' }]
        rules.compleUser = [{ required: true, message: '请输入销点人!' }]
        rules.permitOfficer = [{ required: true, message: '请输入请点值班员!' }]
        if(this.model.comple===2) {
          rules.compleTime = [{ required: true, message: '请输入销点时间!' }]
          rules.compleOfficer = [{ required: true, message: '请输入销点值班员!' }]
        }
      } else {
        rules.compleStation = []
        rules.compleTime = []
        rules.compleUser = []
        rules.compleOfficer = []
      }
      return rules
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.loadAllDeviceLocation()
  },
  methods: {
    async add(item) {
      if (item.id) {
        const res = await getAction('/workOrder/workOrder/queryById', { id: item.id });
        if (res.success && res.result != null) {
          this.modelDefault.workOrder = res.result.id;
          this.modelDefault.permitUser = res.result.coordinator;
          this.modelDefault.permitTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        }
      }
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if(record.compleUser){
        this.model.comple=1;//销点已申请
      }
      if(record.compleOfficer){
        this.model.comple=2;//销点已通过
      }
      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
          })
        }
      })
    },
    convertToTreeSelect (data) {
      return data.map(item => {
        const title = item.title || '未知标题';
        // 创建基本节点
        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;
      });
    },
    loadAllDeviceLocation() {
      getAction(this.url.listDeviceLocation, {}).then((res) => {
        if (res.success) {
          this.deviceLocations = this.convertToTreeSelect(res.result)
        } else {
          this.$message.error(res.message)
        }
      })
    },
  }
}
</script>