<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="12">
            <a-form-model-item label="设备单元" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="isVirtual">
              <a-switch
                :checked="model.isVirtual === '1'"
                checked-children="是"
                un-checked-children="否"
                @change="changeAvaliable(model.isVirtual)"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="涉及补油" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="isOil">
              <a-switch
                :checked="model.isOil === '1'"
                checked-children="是"
                un-checked-children="否"
                @change="changeOilable(model.isOil)"
              />
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item v-show="!model.device" label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="device">-->
          <!--              <a-input v-model="model.device" placeholder="请输入设备"  ></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="部件编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serialCode">
              <a-input v-model="model.serialCode" placeholder="请输入部件编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="部件名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入部件名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="部件类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="type"
            >
              <a-tree-select
                v-decorator="['type',{rules: [{ required: true, message: '请选择部件类型!' }]}]" :trigger-change="true"
                v-model="model.type"
                :tree-data="deviceTypes"
                placeholder="请选择部件类型"
                :dropdownStyle="{ maxHeight: '500px', overflow: 'auto' }"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.isVirtual === '0'"
              label="部件厂家"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="vender"
            >
              <a-input v-model="model.vender" placeholder="请输入部件厂家"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.isVirtual === '0' && model.id"
              label="生命周期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="cycle"
            >
              <j-dict-select-tag
                v-model="model.cycle"
                placeholder="请选择所属生命周期"
                dict-code="device_cycle"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.isVirtual === '0' && model.id"
              label="状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="status"
            >
              <j-dict-select-tag
                v-model="model.status"
                placeholder="请选择状态"
                dict-code="device_status"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.isVirtual === '0'"
              label="数量"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="quantity"
            >
              <a-input-number v-model="model.quantity" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.isVirtual === '0'"
              label="启用时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="beginTime"
            >
              <j-date
                placeholder="请选择启用时间"
                v-model="model.beginTime"
                :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.isVirtual === '0'"
              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="model.isVirtual === '0'"
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="remark"
            >
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.isOil === '1'" label="润滑油牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oilTag">
              <a-input v-model="model.oilTag" placeholder="请输入润滑油牌号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.isOil === '1'" label="上油量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oilNumber">
              <a-input v-model="model.oilNumber" placeholder="请输入上油量"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.isOil === '1'" label="补油周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fillCycle">
              <a-input v-model="model.fillCycle" placeholder="请输入补油周期"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction, putAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'PartForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        isVirtual: '0',
        isOil: '0',
      },
      labelCol1: {
        xs: { span: 12 },
        sm: { span: 10 },
      },
      wrapperCol1: {
        xs: { span: 12 },
        sm: { span: 5 },
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        device: [{ required: true, message: '请输入设备!' }],
        serialCode: [{ required: true, message: '请输入部件编码!' }],
        name: [{ required: true, message: '请输入部件名称!' }],
      },
      url: {
        add: '/device/part/add',
        edit: '/device/part/edit',
        queryById: '/device/part/queryById',
        listDeviceType:"/device/buDeviceType/list/all"
      },
      deviceTypes:[],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    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;
      });
    },
    loadAllDeviceType() {
      getAction(this.url.listDeviceType, {deviceId:this.modelDefault.device,calss:2}).then((res) => {
        if (res.success) {
          this.deviceTypes = this.convertToTreeSelect(res.result)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    changeAvaliable(record) {
      this.model.isVirtual = record === '0' ? '1' : '0'
    },
    changeOilable(record) {
      this.model.isOil = record === '0' ? '1' : '0'
    },
    add(record) {
      console.log(record)
      if (record) {
        switch (record.level) {
          case 'device':
            this.modelDefault.device = record.id
            break
          case 'part':
            this.modelDefault.pid = record.id
            break
        }
      }
      console.log('model', this.modelDefault)
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if(record.oilTag || record.fillCycle){
        this.model.isOil='1';
      }
      this.visible = true
      this.loadAllDeviceType();
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            this.model.cycle = '0'
            this.model.status = 0
            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>
