<template>
  <div>
    <detail-page ref="detail" :list="detailtList" :isView="isView" :defData="detailData"> </detail-page>
  </div>
</template>

<script>
import DetailPage from '../../components/bill-editor/index'
import { detailConfig } from './config/position'
// import { queryItem, addItem, uploadItem } from './api/goods'

export default {
  components: { DetailPage },
  props: {
    detailItem: {
      type: Object,
      default: null
    },
    isView: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      isReady: false,
      detailtList: null,
      detailData: null,
      visible: false,
      formsVal: {}
    }
  },

  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 })
    },
    queryRefFromName(name) {
      return this.$refs.detail.$refs[name][0]
    },
    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)
          }
        })
      }
    },

    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>
