<template>
  <div>
    <el-select
      v-model="selVal"
      :disabled="disabled"
      placeholder="请选择"
    >
      <el-option
        v-for="(item,i) in requestOp"
        :key="i"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
  </div>
</template>

<script>
import { get, post } from '@/utils/http'
import config from './config'

export default {
  componentName: '串件架次选择',
  name: 'SerialPieceSorties',
  components: {},
  mixins: [config],
  props: {
    value: {
      type: String,
      default: ''
    },
    basicData: {
      type: Object,
      default: null
    },
    form: {
      type: Object,
      default: null
    },
    itemObj: {
      type: Object,
      default: null
    },
    componentProp: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      selVal: '',
      linkageValue: '',
      requestOp: [],
      loading: false,
      com: null
    }
  },
  computed: {
    disabled() {
      let disabled = false
      this.$utils.findByNameVnode(this, 'DeeAsForm').formData.forEach(item => {
        item.data && item.data.forEach(x => {
          if (x.key === this.itemObj.attrKey) {
            disabled = x.component.disabled
          }
        })
      })
      return disabled
    }
  },
  watch: {
    'form.aircraftType': function(val) {
      this.getSortiesByCode(val)
    },
    'form.extMaterial': function(val) {
      // this.findCanReplaceSerno(val.id, this.selVal)
    },
    selVal(val) {
      this.$set(this.form, this.itemObj.key, val)
      let obj = this.form
      const keyList = this.itemObj.key.split('.')
      keyList.forEach((item, i) => {
        if (i < keyList.length - 1) {
          if (!obj[item]) {
            obj[item] = {}
            this.$set(obj, item, {})
            obj = obj[item]
          }
        } else {
          this.$set(obj, item, val)
        }
      })
      if (this.form.extMaterial) {
        // this.findCanReplaceSerno(this.form.extMaterial.id, val)
      }
    }
  },
  // 生命周期 - 创建完成(可以访问当前this 实例)
  created() {
  },
  // 生命周期 - 挂载之前
  beforeMount() {
  },
  // 生命周期 - 挂载完成(可以访问 DOM 元素)
  mounted() {
    this.com = this.$utils.findByNameVnode(this, 'DeeAsForm')
    if (Object.keys(this.basicData).length) {
      this.setData(this.form.aircraftType)
    }
  },
  methods: {
    async setData(value) {
      await this.getSortiesByCode(value)
      if (this.itemObj.key.includes('source')) {
        this.selVal = this.basicData.sourceSorties
      } else {
        this.selVal = this.basicData.targetSorties
      }
    },
    async getSortiesByCode(val) {
      await get(`/AircraftSorties/getSortiesByCode?aircraftType=${val}&aircraftTypeId=${val}`)
        .then(res => {
          this.loading = false
          if (res.code === 0) {
            this.requestOp = res.items.map(item => {
              return {
                label: item.defName,
                value: item.defCode
              }
            })
          }
        })
    },
    findCanReplaceSerno(materialId, sorties) {
      if (materialId && sorties) {
        post(`/Inventory/findCanReplaceSerno?materialId=${materialId}&sorties=${sorties}`)
          .then(res => {
            this.loading = false
            if (res.code === 0) {
              const keyList = Object.keys(res.items)
              let attrKey = 'sourceSerno'
              if (this.itemObj.key.includes('source')) {
                this.form.sourceInventoryMap = res.items
              } else {
                this.form.targetInventoryMap = res.items
                attrKey = 'targetSerno'
              }
              let options = []
              if (keyList.length) {
                options = keyList.map(key => {
                  return {
                    label: key,
                    value: key
                  }
                })
              } else {
                if (this.itemObj.key.includes('source')) {
                  this.$set(this.form, 'sourceSerno', '')
                  this.$set(this.form, 'sourceApprovalCert', '')
                  this.$set(this.form, 'sourceProductDrawver', '')
                  this.$set(this.form, 'sourceElecHardwareNo', '')
                  this.$set(this.form, 'sourceSoftConfPieceNo', '')
                } else {
                  this.$set(this.form, 'targetSerno', '')
                  this.$set(this.form, 'targetApprovalCert', '')
                  this.$set(this.form, 'targetProductDrawver', '')
                  this.$set(this.form, 'targetElecHardwareNo', '')
                  this.$set(this.form, 'targetSoftConfPieceNo', '')
                }
              }
              this.com.formData.forEach(item => {
                item.data && item.data.forEach(x => {
                  if (x.key === attrKey) {
                    this.$set(x.component, 'options', options)
                  }
                })
              })
            }
          })
      }
    }
  }
}
</script>

<style lang='scss' scoped>

</style>