<template>
  <div>
    <detail-page ref="detail" :list="detailtList" :isView="isView" :defData="detailData"> </detail-page>
    <p-modal
      :visible="visible"
      :destroyOnClose="true"
      width="80%"
      :closable="false"
      @ok="handleOk"
      @cancel="visible = false"
    >
      <modalPage ref="modalPageRef" />
    </p-modal>
  </div>
</template>

<script>
import DetailPage from '../components/bill-editor/index'
import { detailConfig } from './config/itemLockApply'
import { invAdjustStatusAdd, queryMovePrintList } from '../api/api'
import { getCargoOwner } from '../../utils/index'
import modalPage from './components/itemLockApplyModal'

export default {
  components: { DetailPage, modalPage },
  props: {
    detailItem: {
      type: Object,
      default: null,
    },
    isView: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isReady: false,
      detailtList: null,
      detailData: null,
      visible: false,
      remoteParams: {},
      formsVal: {},
      tiemID: null,
    }
  },

  created() {
    this.handleView()
    this.getAuthBycargoOwner()
  },
  mounted() {
    let user = this.$store.state.user
    console.log(user, 'useruseruseruseruser')
    let warehouse = this.$store.state.user.warehouse
    let today = new Date()
    let tYear = today.getFullYear()
    let tMonth = today.getMonth() + 1
    let tDate = today.getDate()
    let day = tYear + '-' + tMonth + '-' + tDate
    this.queryRefFromName('main').form.setFieldsValue({
      today: day,
      warehouseName: warehouse.warehouseName,
      warehouseId: warehouse.warehouseId,
      warehouseWorkerId: user.info.userId,
      warehouseWorkerName: user.info.username,
    })
    this.remoteParams.warehouseId = warehouse.warehouseId
  },
  methods: {
    handleOk() {
      // 提取库存
      let arrDatas = this.$refs.modalPageRef.save()
      this.visible = false
      console.log(arrDatas, 'arrDatasarrDatasarrDatas')
      this.queryRefFromName('baseMtlItemUomConversionsList').setConcatData(arrDatas)
    },
    toBack() {
      this.$emit('changeStep', { step: 1, item: null })
    },
    queryRefFromName(name) {
      return this.$refs.detail.$refs[name][0]
    },
    async getAuthBycargoOwner() {
      await getCargoOwner().then((res) => {
        if (res.success) {
          this.$nextTick(() => {
            this.queryRefFromName('main').form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerName: res.data.list[0].companyName,
              cargoOwnerCode: res.data.list[0].companyCode,
            })
          })
        }
        if (!res.success) {
          this.$exception(res)
          return
        }
      })
    },
    getData() {
      return queryItem({ itemId: this.detailItem.itemId }).then((res) => {
        if (!res.success) {
          this.$message.error(res.message)
          return
        }
        let d = { main: {} }
        Object.keys(res.data).forEach((k) => {
          if (res.data[k] === null) {
            // d.main[k] = res.data[k];
            d.main[k] = ''
          } else if (typeof res.data[k] === 'object') {
            d[k] = res.data[k]
          } else {
            d.main[k] = res.data[k]
          }
        })
        this.detailData = d
      })
    },

    getDetailData() {
      let vals = this.$refs.detail.getDetailData()
      let main = vals.main
      delete vals.main
      let params = { ...main, ...vals }
      // console.log('vals', vals);
      if (!vals) {
        return
      }
      if (this.detailItem) {
        this.$confirm({
          content: this.$lang('是否提交修改?'),
          onOk: () => {
            return this.handleUpload(params)
          },
        })
      } else {
        this.$confirm({
          content: this.$lang('是否确定提交?'),
          onOk: () => {
            return this.handleAdd(params)
          },
        })
      }
    },
    saveData(datas) {
      datas
        .then((val) => {
          console.log(val, 'valvalval')
          if (Array.isArray(val) && val.length < 1) return this.$message.warn('请选择商品')
          let datas = val
          datas.forEach((res) => {
            res.lotId = res.id
            res.uom = res.pieceUom
            res.conversionRate = res.caseConversionRate
          })
          let vals = this.$refs.detail.getDetailData()
          let main = vals.main
          let params = datas
          invAdjustStatusAdd(params).then((res) => {
            if (!res.success) {
              return this.$exception(res)
            }
            this.$message.success('保存成功')
            this.queryRefFromName('baseMtlItemUomConversionsList').setDataSourceAll([])
          })
        })
        .catch((error) => {
          this.$message.error(error[0]?.errors[0]?.field + error[0]?.errors[0]?.message)
        })
    },
    handleView() {
      let arr = detailConfig(this)
      if (this.isView) {
        arr.forEach((t) => {
          t.list.forEach((tt) => {
            if (tt.item.type === 'p-grid-form') {
              tt.item.config = tt.item.config.map((t) => ({ ...t, disabled: true, placeholder: '' }))
            } else if (tt.item.type === 'p-table-edit') {
            }
          })
        })
      }
      this.detailtList = arr
    },
    handleAdd(params) {
      return addItem(params).then((res) => {
        if (!res.success) {
          this.$message.error(res.message)
          return
        }
        this.$message.success(this.$lang('新增成功'))
        this.toBack()
      })
    },
    handleUpload(params) {
      params.itemId = this.detailItem.itemId
      return uploadItem(params).then((res) => {
        if (!res.success) {
          this.$message.error(res.message)
          return
        }
        this.$message.success(this.$lang('新增成功'))
        this.toBack()
      })
    },
  },
}
</script>

<style></style>
