<template>
  <a-modal
    centered
    class="orderStatisticsDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="促销品明细"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <div>
      <p>销售单号：{{ nowData ? (nowData.salesBillNo || '--') : '--' }}</p>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        :scroll="{ y: 400 }"
        bordered>
      </s-table>
      <div class="btn-cont"><a-button id="orderStatisticsDetail-back" @click="isShow = false">返回列表</a-button></div>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { getOperationalPrecision } from '@/libs/tools.js'
import { salesDetailList } from '@/api/salesDetail'
export default {
  name: 'OrderStatisticsDetailModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return salesDetailList(Object.assign(parameter, { salesBillSn: this.itemSn, promotionFlag: 1 })).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              const origPrice = data.list[i].origPrice || 0
              const qty = data.list[i].qty || 0
              // 成本小计  由于数据库内小数位数为4位，页面则需显示2位。因此会做小数运算精度处理
              data.list[i].priceSubtotal = getOperationalPrecision(origPrice, qty)
            }
          }
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productEntity.code', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '30%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '原价', dataIndex: 'origPrice', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '促销价', dataIndex: 'price', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '数量', dataIndex: 'qty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productEntity.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '原价小计', dataIndex: 'priceSubtotal', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '促销价小计', dataIndex: 'totalAmount', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '节省金额小计', dataIndex: 'totalEconomizeAmount', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '促销类型', dataIndex: 'promotionRulesName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ]

        arr.splice(3, 0, { title: '原价', dataIndex: 'origPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(4, 0, { title: '促销价', dataIndex: 'price', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(7, 0, { title: '原价小计', dataIndex: 'priceSubtotal', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(8, 0, { title: '促销价小计', dataIndex: 'totalAmount', width: '7%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(9, 0, { title: '节省金额小计', dataIndex: 'totalEconomizeAmount', width: '7%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
 
      return arr
    }
  },
  methods: {

  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearTable()
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        const _this = this
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 300)
      }
    }
  }
}
</script>

<style lang="less">
  .orderStatisticsDetail-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
  }
</style>
