<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    title="促销品列表"
    v-model="isShow"
    @cancle="cansel"
    width="80%">
    <a-card size="small" :bordered="false" class="productInfoList-wrap">
      <!-- 列表 -->
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
          getCheckboxProps: record => ({
            props: {
              disabled: record.product.cost == undefined, // Column configuration not to be checked
            }
          })
        }"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data-source="list"
        :loading="loading"
        :scroll="{ y: 350 }"
        bordered>
        <!-- 销售数量 -->
        <template slot="salesNums" slot-scope="text, record">
          <a-input-number
            size="small"
            :max="999999"
            :min="1"
            v-model="record.qty"
            :precision="0"
            :step="1"
          />
        </template>
      </a-table>
      <div style="display: flex;justify-content: center;padding: 10px 0;">
        <a-button
          size="large"
          type="primary"
          :loading="newLoading"
          class="button-primary"
          @click="handleSubmit"
          style="padding: 0 60px;">
          确定
        </a-button>
        <a-button size="large" :loading="newLoading" style="padding: 0 60px;margin-left: 15px;font-size: 12px;" @click="cansel">
          取消
        </a-button>
      </div>
    </a-card>
  </a-modal>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getPromoGoodsList } from '@/api/salesDetail'
export default {
  name: 'ChooseProduct',
  components: { STable, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    buyerSn: {
      type: String,
      default: ''
    },
    newLoading: Boolean
  },
  data () {
    return {
      isShow: this.openModal,
      columns: [
        { title: '产品编码', dataIndex: 'product.code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'product.cost', align: 'center', customRender: function (text) { return '￥' + (text == undefined ? '--' : text) } },
        { title: '原售价', dataIndex: 'product.productPrice', align: 'center', customRender: function (text) { return '￥' + (text || 0) } },
        { title: '促销价', dataIndex: 'goodsPrice', align: 'center', customRender: function (text) { return '￥' + (text || 0) } },
        { title: '数量', dataIndex: 'qty', scopedSlots: { customRender: 'salesNums' }, align: 'center' },
        { title: '单位', dataIndex: 'product.unit', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销类型', dataIndex: 'promoRuleTypeName', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      selectedRowKeys: [], // Check here to configure the default column
      selectedRows: [],
      loading: false,
      list: [],
      salesBillDetailSn: null
    }
  },
  methods: {
    getData (row) {
      this.loading = true
      this.salesBillDetailSn = row.salesBillDetailSn
      getPromoGoodsList({ salesBillDetailSn: row.salesBillDetailSn, buyerSn: this.buyerSn }).then(res => {
        res.data.map(item => {
          item.qty = 1
        })
        this.list = res.data
        this.loading = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    // 选择产品
    handleSubmit () {
      const _this = this
      if (_this.selectedRowKeys.length < 1) {
        _this.$message.warning('请选择促销品！')
        return
      }
      const obj = []
      _this.selectedRows.map(item => {
        obj.push({
          showCost: item.product.cost,
          price: item.goodsPrice,
          origPrice: item.product.productPrice,
          packQty: item.product.packQty,
          productSn: item.product.productSn,
          promotionSourceSn: this.salesBillDetailSn,
          promotionActivitySn: item.promoActiveSn,
          promotionActivityName: item.promoActiveName,
          promotionRules: item.promoRuleSn,
          promoGoodsType: item.promoGoodsType,
          qty: item.qty,
          promotableFlag: 0, // 可促销标记 有活动的传1，没活动的传0
          promotionFlag: 1 // 促销标记 正品传0，促销品传1
        })
      })
      _this.$emit('ok', obj)
    },
    cansel () {
      this.$emit('close')
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>
