<template>
  <div>
    <bill-editor ref="detail" :list="detailtList" :defData="detailData" @ok="onOk"> </bill-editor>
  </div>
</template>

<script>
import BillEditor from '../../../../components/bill-editor'
import { detailConfig } from './config/detail'
import { querySubList, getLocationType, getRcvInstockLabelPrintData } from './api'
import { getCargoOwner } from '../../../../utils'
import { businessRuleModule } from '../../../mixins/rule'
import { inboundValidateModule } from '../../../mixins/inboundValidate'
import { toPrintServer, tempType } from '../../../../utils/printServer'

export default {
  mixins: [businessRuleModule, inboundValidateModule],
  components: {
    'bill-editor': BillEditor,
  },

  data() {
    return {
      detailtList: null,
      detailData: null,
      visible: false,
      confirmLoading: false,
      selectedMainItem: null,
      L_type: null,
    }
  },

  created() {
    this.handleView()
  },
  mounted() {
    this.getL_Type()
  },
  methods: {
    // 货位限定regCode=INBOUND_IN_STOCK_LOCATOR_TYPE&warehouseId=
    getL_Type() {
      let _this = this
      // this.$nextTick(() => {
      //   let ref = this.queryRefFromName('main')
      // })
      let warehouse = this.$store.state.user.warehouse
      let params = { regCode: 'INBOUND_IN_STOCK_LOCATOR_TYPE', warehouseId: warehouse.warehouseId }
      return getLocationType(params).then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        if (!res.data.regValue) {
          _this.L_type = 1
        } else {
          _this.L_type = res.data.regValue
        }
        _this.handleView()
      })
    },

    queryRefFromName(name) {
      return this.$refs.detail.$refs[name][0]
    },

    onOk() {
      this.$nextTick(() => {
        let warehouse = this.$store.state.user.warehouse
        let ref = this.queryRefFromName('main')
        ref.$refs.form.form.setFieldsValue({
          warehouseId: warehouse.warehouseId,
          warehouseCode: warehouse.warehouseCode,
          warehouseName: warehouse.warehouseName,
        })
        this.getCargoOwnerFirst()
      })
    },

    handleView() {
      this.detailtList = detailConfig(this)
    },
    // 打印
    print() {
      let ref = this.queryRefFromName('list')
      if (ref.selectedRowKeys.length === 0) {
        this.$message.warning(this.$lang('请先勾选需要打印的商品明细'))
        return
      }
      let vals = ref.data.filter((tt) => ref.selectedRowKeys.some((key) => key == tt.taskId))
      console.log(vals, 'vals')
      // {"incloudIds ": ["123","456"]}
      let params = {}
      let incloudIds = []
      vals.forEach((t) => {
        incloudIds.push(t.taskId)
      })
      params.incloudIds = incloudIds
      console.log(params, incloudIds, 'params')
      getRcvInstockLabelPrintData(params).then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        // this.$message.success(this.$lang('打印成功'))
        this.queryRefFromName('main').reload()
        this.handleQuerySubList({ rcvHeaderId: ref.selectedRowKeys[0] }).then((res) => {
          ref.data = res
        })
        const printParms = {
          consignorId: vals[0].mtlOwnerId,
        }
        const warehouseId = vals[0].warehouseId
        var temp = tempType.SJ_BQ
        if (res.data.tempType) {
          temp = res.data.tempType
        }
        //调用打印方法()
        toPrintServer(res.data.data, warehouseId, temp, printParms).then((res) => {
          if (!res.success) {
            this.$exception(res)
            return
          }
          this.$message.success(res.message)
        })
      })
    },

    handleQuerySubList(params) {
      this.$refs.detail.$refs.list[0].loading = true
      this.$refs.detail.$refs.list[0].selectedRowKeys = []
      return querySubList(params).then((res) => {
        this.$refs.detail.$refs.list[0].loading = false
        if (!res.success) {
          this.$exception(res)
          return
        }
        if (!res.data.list) return []
        return res.data.list.map((item) => {
          return {
            ...item,
            taskId: item.taskInboundId,
            oddCaseQty: item.caseQty,
            oddPieceQty: item.oddPieceQty,
            pieceQty: item.pieceQty,
          }
        })
      })
    },

    handleReset() {
      this.detailData = null
    },

    //获取货主信息
    getCargoOwnerFirst() {
      getCargoOwner().then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        this.$nextTick(() => {
          if (res.data.list.length > 0) {
            let ref = this.queryRefFromName('main')
            ref.$refs.form.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerCode: res.data.list[0].companyCode,
              cargoOwnerName: res.data.list[0].companyName,
            })
          }
        })
        // }
      })
    },
  },
}
</script>

<style></style>
