/**
* @Description:
* @author cxg
* @date 2022/03/29
*/
<template>
  <div class="AddOutStorageMaterialDetailDialog-diloag">
    <dee-as-com
      ref="materielSearch"
      :lay-config="{ typeName: 'Inventory', layKey: 'search_sanqichukuwuliaomingxi' }"
      :basic-data="defaultData"
      @searchEvent="searchEvent"
    />
    <dee-as-com
      ref="materielTable"
      class="list-table"
      :lay-config="{ typeName: 'Inventory', layKey: 'table_sanqichukuwuliaomingxi' }"
      @selectionChange="selectionChange"
    />
    <div slot="footer" class="foot-btn-box">
      <el-button type="primary" @click="submitEvent">确认</el-button>
      <el-button @click="cancelEvent">取消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  componentName: '添加三期出库物料明细弹框',
  name: 'AddOutStorageMaterialDetailDialog',
  components: {},
  props: {
    basicData: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      selection: [],
      defaultData: {}
    }
  },

  computed: {},
  watch: {
    basicData: {
      immediate: true,
      deep: true,
      handler() {
        setTimeout(() => {
          this.searchEvent({
            items: [],
            operator: 'AND'
          })
        }, 500)
      }
    }
  },
  mounted() {
  },
  methods: {
    searchEvent(val) {
      // const materialType = this.findByNameVnode(this, 'DeeAsForm').form.materialType
      val.items.push({ fieldName: 'subTypeName', operator: 'EQ', value: 'Inventory' }/*, { fieldName: 'inventory.materialType', operator: 'EQ', value: materialType }*/)
      val.items.push({ fieldName: 'status', operator: 'EQ', value: 'disposed' })
      val.items.push({ fieldName: 'usableAmount', operator: 'GT', value: 0 })
      val.items.push({ fieldName: 'disposeDes', operator: 'LIKE', value: '三期' })
      const extWorkCenterName = this.basicData.extWorkCenteName || (this.basicData.extWorkCenter && this.basicData.extWorkCenter.extname)
      if (extWorkCenterName) {
        val.items.push({ fieldName: 'workcenter', operator: 'EQ', value: extWorkCenterName })
      }
      const el = this.$refs['materielTable']
      el.$refs.asCom.getData(val.items, val.items)
    },
    findByNameVnode(obj, targetName) {
      if (obj && obj.$vnode && obj.$vnode.tag && obj.$vnode.tag.includes(targetName)) {
        return obj
      } else {
        return this.findByNameVnode(obj.$parent, targetName)
      }
    },
    selectionChange(val) {
      this.selection = val
    },
    submitEvent() {
      const selection = this.selection.filter(r => {
        if (!this.basicData.inStorageRequestItems) {
          return true
        }
        return !this.basicData.inStorageRequestItems.find(item => r.id === item.inventoryId)
      })
      if (selection.length) {
        if (selection.find(row => !row.jobResponse || !row.jobResponse.jobOrder || !row.jobResponse.jobOrder.extMaterial)) return this.$utils.showMessageWarning('所选数据信息不全,请重新选择!')
        const data = selection.map(r => {
          return {
            ...r.jobResponse.jobOrder,
            'subTypeName': 'OutStorageExpireItem',
            'reqStatus': 'Apply',
            'outStorageId': r.jobResponseId,
            'outStorageIdType': r.jobResponseIdType,
            'inventory': r,
            // inventory: { amount: r.amount },
            'inventoryId': r.id,
            'inventoryIdType': this.$utils.getModelName4dxClassName(r),
            'purchaseUnitId': r.jobResponse.jobOrder.extMaterial.extUnitId,
            'purchaseUnitIdType': r.jobResponse.jobOrder.extMaterial.extUnitIdType,
            'extDxSipplierId': r.jobResponse.jobOrder.extDxSipplierId,
            'extDxSipplierIdType': r.jobResponse.jobOrder.extDxSipplierIdType,
            extDxSipplier: r.jobResponse.jobOrder.extDxSipplier,
            extMaterial: r.jobResponse.jobOrder.extMaterial,
            storageZone: r.extProductUnit && r.extProductUnit.extCenter,
            storageCondition: r.jobResponse.jobOrder.storageCondition,
            'extMaterialId': r.jobResponse.jobOrder.extMaterialId,
            'extMaterialIdType': r.jobResponse.jobOrder.extMaterialIdType,
            'arrivalDate': r.jobResponse.jobOrder.arrivalDate,
            'reqAmount': r.usableAmount,
            'airModel': r.jobResponse.jobOrder.airModel,
            'sorties': r.jobResponse.jobOrder.sorties,
            'manufacturer': r.jobResponse.jobOrder.manufacturer,
            'contractNo': r.jobResponse.jobOrder.contractNo,
            'stockPrice': r.jobResponse.jobOrder.stockPrice,
            'lotNo': r.jobResponse.jobOrder.lotNo,
            'taxRate': r.jobResponse.jobOrder.taxRate,
            'taxUnitPrice': r.jobResponse.jobOrder.taxUnitPrice,
            'taxPrice': r.jobResponse.jobOrder.taxPrice,
            'stockUnitPrice': r.jobResponse.jobOrder.stockUnitPrice,
            'purchaseOrderNo': r.jobResponse.jobOrder.purchaseOrderNo,
            'operator': 'ADD',
            id: null,
            remark: r.jobResponse.jobOrder.remark,
            'isRoot': r.jobResponse.jobOrder.isRoot

          }
        })
        this.$emit('submitEvent', { formData: data })
      }
    },
    cancelEvent() {
      this.$emit('cancel')
    }
  }
}
</script>
<style lang='scss'>
.AddOutStorageMaterialDetailDialog-diloag {
  .list-table {
    height: 510px !important;
  }

  .foot-btn-box {
    display: flex;
    margin-top: 10px;
    justify-content: center;
    align-content: center;
  }
}
</style>