<template>
  <bill-editor v-show="isReady" ref="detail" :list="detailList" :isView="isView" :defData="detailData"
               :handleBack="isSingle ? null : toBack"/>
</template>

<script>
import BillEditor from '../../components/bill-editor'
import {detailConfig} from './detail'
import {queryItem, addItem, uploadItem} from '../../api/basedata/goods'

export default {
  components: {BillEditor},
  props: {
    detailItem: {
      type: Object,
      default: null
    },
    isView: {
      type: Boolean,
      default: false
    },
    isSingle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isReady: false,
      detailList: null,
      detailData: null
    }
  },
  created() {
    if (this.detailItem)
      this.getData().then(res => {
        this.isReady = true
      })
    else this.isReady = true
    this.handleView()
  },

  methods: {
    toBack() {
      this.$emit('changeStep', {step: 1, item: null})
    },

    getData() {
      return queryItem({itemId: this.detailItem.itemId}).then(res => {
        if (!res.success) {
          this.$exception(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]
          }
        })
        // 由 是否整散分离->是否整散合一，传给后端的值将反转
        if (!d.baseMtlItemWms) d.baseMtlItemWms = {}
        d.baseMtlItemWms.isCaseOrPiece = d.baseMtlItemWms.isCaseOrPiece ? 0 : 1
        this.detailData = d
      })
    },

    getDetailData() {
      let vals = this.$refs.detail.getDetailData()
      if (!vals) {
        this.$message.warning(this.$lang('请填写必填项'))
        return
      }
      if (!vals.baseMtlItemUomConversionsList.length) {
        this.$message.warning(this.$lang('包装属性不能为空'))
        return
      }
      if (!vals.baseMtlItemUomConversionsList.some(t => t.minSu == 'Y')) {
        this.$message.warning(this.$lang('包装属性缺少最小统计单位'))
        return
      }
      if (!vals.baseMtlItemUomConversionsList.some(t => t.maxSu == 'Y')) {
        this.$message.warning(this.$lang('包装属性缺少最大统计单位'))
        return
      }
      if (vals.baseMtlItemUomConversionsList.some(t => t.length == 33 && t.width == 33 && t.height == 33)) {
        this.$message.warning(this.$lang('包装属性不允许长宽高同时为33'))
        return
      }
      let main = vals.main
      delete vals.main
      let params = {...main, ...vals}
      // 通过包装属性设置主表冗余字段
      if (params.baseMtlItemUomConversionsList.length === 1) {
        params.pieceUom = params.baseMtlItemUomConversionsList[0].uom
        params.pieceConversionRate = params.baseMtlItemUomConversionsList[0].conversionRate
        params.pieceBarcode = params.baseMtlItemUomConversionsList[0].barcode
        params.caseUom = params.baseMtlItemUomConversionsList[0].uom
        params.caseConversionRate = params.baseMtlItemUomConversionsList[0].conversionRate
        params.caseBarcode = params.baseMtlItemUomConversionsList[0].barcode
      } else {
        let min = params.baseMtlItemUomConversionsList.find(t => t.minSu == 'Y')
        let max = params.baseMtlItemUomConversionsList.find(t => t.maxSu == 'Y')
        params.pieceUom = min.uom
        params.pieceConversionRate = min.conversionRate
        params.pieceBarcode = min.barcode
        params.caseUom = max.uom
        params.caseConversionRate = max.conversionRate
        params.caseBarcode = max.barcode
      }
      // 体积单位 重量单位 默认 cm^3  g
      params.baseMtlItemUomConversionsList.forEach(t => {
        t.volumeUom = 'cm^3'
        t.weightUom = 'g'
      })
      // 由 是否整散分离->是否整散合一，传给后端的值将反转
      params.baseMtlItemWms.isCaseOrPiece = params.baseMtlItemWms.isCaseOrPiece ? 0 : 1
      if (this.detailItem) {
        this.$confirm({
          content: this.$lang('是否提交修改?'),
          onOk: () => {
            return this.handleUpload(params)
          }
        })
      } else {
        this.$confirm({
          content: this.$lang('是否确定提交?'),
          onOk: () => {
            return this.handleAdd(params)
          }
        })
      }
    },

    handleView() {
      let arr = detailConfig(this)
      let undisablearr = [
        'itemAssistantCode',
        'pieceBarcode',
        'barcode',
        'barcodeOld',
        'length',
        'width',
        'height',
        'weight',
        'netWeight',
        'isCaseOrPiece',
        'segment1'
      ]
      if (this.isView) {
        arr.forEach(t => {
          t.btns = []
          t.list.forEach(tt => {
            tt.btns = []
            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') {
              tt.item.remoteParams = {
                itemId: this.detailItem.itemId
              }
            }
          })
        })
      } else if (this.detailItem) {
        arr.forEach(t => {
          if (this.isSingle) t.btns = []
          t.list.forEach(tt => {
            if (tt.item.type === 'p-grid-form') {
              tt.item.config = tt.item.config.map(t => ({
                ...t,
                type: undisablearr.some(item => t.dataIndex === item) ? 'txt' : t.type,
                placeholder: ''
              }))
            } else if (tt.item.type === 'p-table-edit') {
              tt.item.config = tt.item.config.map(t => ({
                ...t,
                type: undisablearr.some(item => t.dataIndex === item) ? t.type : 'txt',
                placeholder: ''
              }))
            } else if (tt.item.type === 'p-table') {
              tt.item.autoLoad = true
              tt.item.remoteParams = {
                itemId: this.detailItem.itemId
              }
            }
          })
        })
      } else {
        arr.forEach(t => {
          if (this.isSingle) t.btns = []
          t.list.forEach(tt => {
            // tt.btns = []
            if (tt.item.type === 'p-grid-form') {
              tt.item.config = tt.item.config.map(t => ({
                ...t,
                // disabled: !undisablearr.some(item => {
                //   return t.name === item
                // }),
                // type: undisablearr.some(item => {
                //   return t.dataIndex === item
                // })
                //   ? 'txt'
                //   : t.type,
                placeholder: ''
              }))
            } else if (tt.item.type === 'p-table-edit') {
              tt.item.config = tt.item.config.map(t => ({
                ...t,
                // type: undisablearr.some(item => {
                //   return t.dataIndex === item
                // })
                //   ? t.type
                //   : 'txt',
                placeholder: ''
              }))
            } else if (tt.item.type === 'p-table') {
              console.log(tt.item)
              tt.item.autoLoad = false
            }
          })
        })
      }
      this.detailList = 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>
