<template>
  <a-modal
    centered
    class="unique-code-modal"
    :footer="null"
    :maskClosable="false"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <solt>
      <div class="unique-code-tit">查看唯一码（产品编码：{{ itemData&&itemData.productCode }}，唯一码数量：{{ uniqueList&&uniqueList.length?uniqueList.length:0 }}）</div>
    </solt>
    <a-card size="small" :bordered="false" class="unique-code-con">
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 360 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
      </s-table>
    </a-card>
    <div class="btn-cont">
      <a-button id="unique-code-modal-back" @click="isShow = false" style="margin-left: 15px;">关闭</a-button>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
// 接口
import { traceCodeQueryList } from '@/api/purchase'
export default {
  name: 'UniqueCodeModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      uniqueList: [], // 唯一码数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const newData = JSON.parse(JSON.stringify(this.itemData))
        delete newData.productCode
        return traceCodeQueryList(Object.assign(parameter, newData)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
            }
            this.uniqueList = data
          }
          return data
        })
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '唯一码', dataIndex: 'bizCode', width: '15%', align: 'left', customRender: function (text) { return text || '--' } }
      ]
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.$refs.table.refresh()
        })
      }
    }
  }
}
</script>

<style lang="less">
  .unique-code-modal{
    .unique-code-tit{
      padding-bottom:20px;
      margin-bottom:20px;
      border-bottom:1px solid #e8e8e8;
    }
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
  }
</style>
