<template>
  <div class="veTableCon">
    <a-spin :spinning="spinning" tip="Loading...">
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :row-selection="{ columnWidth: 40 }"
        @rowSelection="rowSelectionFun"
        :defaultLoadData="false"
        bordered>
        <!-- 买 -->
        <template slot="conditionValue" slot-scope="text,record">
          <a-input-number
            :min="1"
            :step="1"
            :precision="0"
            :max="99999999"
            style="width:90%;"
            @blur="editProductVal(record,'conditionValue')"
            placeholder="请输入"
            v-model="record.conditionValue"
            :id="'productTable-conditionValue'+record.id "
            size="small"/>
        </template>
        <!-- 赠 -->
        <template slot="resultValue" slot-scope="text,record">
          <a-input-number
            :min="1"
            :step="1"
            :precision="0"
            :max="99999999"
            style="width:90%;"
            @blur="editProductVal(record,'resultValue')"
            placeholder="请输入"
            v-model="record.resultValue"
            :id="'productTable-resultValue'+record.id "
            size="small"/>
        </template>
        <!-- 买产品送代金券 返券金额 -->
        <template slot="priceValue" slot-scope="text,record">
          <a-input-number
            :min="0.01"
            :step="1"
            :precision="2"
            :max="99999999"
            style="width:90%;"
            @blur="editProductVal(record,'resultValue')"
            placeholder="请输入"
            v-model="record.resultValue"
            :id="'productTable-resultValue'+record.id "
            size="small"/>
        </template>
        <!-- 特价价格-->
        <template slot="specialOffer" slot-scope="text,record">
          <a-input-number
            :step="1"
            :precision="2"
            :max="99999999"
            style="width:90%;"
            @blur="editProductVal(record,'conditionValue')"
            placeholder="请输入"
            v-model="record.conditionValue"
            :id="'productTable-conditionValue'+record.id "
            size="small"/>
          <!-- 直降 -->
          <!-- <span v-if="discountType==='STRAIGHT_DOWN'">{{ (record.resultValue&&record.shopProductPrice)?(record.shopProductPrice-record.resultValue).toFixed(2):'--' }}</span> -->
          <!-- 折扣 -->
          <!-- <span v-else>{{ (record.resultValue&&record.shopProductPrice)?(record.shopProductPrice*record.resultValue/100).toFixed(2):'--' }}</span> -->
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text,record">
          <a-button
            size="small"
            type="link"
            class="button-error"
            :id="'productTable-del-btn'+record.id "
            @click="handleDel(record)"
          >删除</a-button>
        </template>
      </s-table>
    </a-spin>
  </div>
</template>
<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
// 接口
import { chooseProductList, delChooseProduct, modifyChooseProduct, clearProductByPromoSn } from '@/api/shopPromo'
export default {
  name: 'ProductTable',
  mixins: [commonMixin],
  components: { STable },
  props: {
    promoActiveSn: {// 活动sn
      type: String,
      default: ''
    },
    activeType: {// 促销活动类型
      type: String,
      default: ''
    },
    discountType: {// 特价产品时，优惠方式
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      rowSelectionInfo: null, // 已选数据
      queryParam: {}, // 查询条件
      chooseProductNum: 0, // 列表数据个数
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        params.promoSn = this.promoActiveSn
        return chooseProductList(params).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
              if (this.activeType === 'PROMO_PROD') {
                data.list[i].conditionValue = data.list[i].conditionValue ? data.list[i].conditionValue : data.list[i].shopProductPrice
              }
            }
            this.chooseProductNum = data.count
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商城售价', dataIndex: 'shopProductPrice', width: '10%', align: 'right', customRender: text => { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
      if (_this.activeType === 'BUY_PROD_GIVE_PROD') {
        arr.splice(4, 0, { title: '买', scopedSlots: { customRender: 'conditionValue' }, width: '10%', align: 'center' })
        arr.splice(5, 0, { title: '赠', scopedSlots: { customRender: 'resultValue' }, width: '10%', align: 'center' })
      } else if (_this.activeType === 'PROMO_PROD') {
        arr.splice(5, 0, { title: '特价价格', width: '10%', align: 'right', scopedSlots: { customRender: 'specialOffer' } })
      } else {
        arr.splice(5, 0, { title: '返券金额', scopedSlots: { customRender: 'priceValue' }, width: '10%', align: 'center' })
      }
      return arr
    },
    // 计算列表选中数据条数
    selectCount () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
      this.$emit('changeSel', this.selectCount)
    },
    // 批量已选产品信息
    editProductVal (row, typeName) {
      let ajaxData = []
      row.resultValue = this.activeType === 'PROMO_PROD' ? undefined : row.resultValue
      if (typeName != 'all') {
        ajaxData.push({
          id: row.id,
          promoSn: this.promoActiveSn,
          productPrice: typeName === 'productPrice' ? row.productPrice : undefined,
          conditionValue: typeName === 'conditionValue' ? row.conditionValue : undefined,
          resultValue: typeName === 'resultValue' ? row.resultValue : undefined
        })
        if (this.activeType === 'PROMO_PROD' && this.discountType === 'DISCOUNT') {
          ajaxData[0].resultValue = row.resultValue ? row.resultValue / 100 : ''
        }
      } else {
        ajaxData = row || []
      }
      modifyChooseProduct(ajaxData).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh()
          this.$refs.table.clearSelected() // 清空表格选中项
        }
      })
    },
    // 修改产品活动价、返券金额
    editMorePrice (oldObjInfo) {
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRows && this.rowSelectionInfo.selectedRows.length === 0)) {
        this.$message.warning('请选择要修改的产品！')
        return
      }
      let ajaxArr = []
      const objInfo = JSON.parse(JSON.stringify(oldObjInfo))
      if (this.activeType === 'PROMO_PROD') {
        if (this.discountType === 'STRAIGHT_DOWN') {
          ajaxArr = this.rowSelectionInfo.selectedRows.map(item => { return { id: item.id, promoSn: this.promoActiveSn, productPrice: item.shopProductPrice, conditionValue: (item.shopProductPrice - objInfo.resultValue), resultValue: objInfo.resultValue } })
        } else {
          ajaxArr = this.rowSelectionInfo.selectedRows.map(item => { return { id: item.id, promoSn: this.promoActiveSn, conditionValue: (item.shopProductPrice * (objInfo.resultValue / 100)), resultValue: objInfo.resultValue / 100 } })
        }
      } else {
        ajaxArr = this.rowSelectionInfo.selectedRows.map(item => { return { id: item.id, promoSn: this.promoActiveSn, conditionValue: objInfo.conditionValue, resultValue: objInfo.resultValue } })
      }
      this.editProductVal(ajaxArr, 'all')
    },
    // 删除
    handleDel (row) {
      const _this = this
      _this.spinning = true
      delChooseProduct([{ id: row.id, promoSn: _this.promoActiveSn }]).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh(true)
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 清空表格
    clearTable () {
      this.spinning = true
      clearProductByPromoSn({ promoSn: this.promoActiveSn }).then(res => {
        this.spinning = false
        this.pageInit()
      })
    },
    getChooseProductNum () {
      return this.chooseProductNum
    },
    // 初始化
    pageInit () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
