<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"
        :style="{ maxHeight: 300+'px' }"
        :scroll="{ y:230 }"
        bordered>
        <template slot="price" slot-scope="text,record">
          <a-input-number
            :min="0"
            :step="1"
            :precision="2"
            :max="99999999"
            @blur="editAllPrice(record,'price')"
            placeholder="请输入"
            :disabled="disabledVal"
            v-model="record.price"
            :id="'productTable-price'+record.id "
            size="small"/>
        </template>
        <template slot="ruleValue" slot-scope="text,record">
          <a-input-number
            :min="0"
            :step="1"
            :precision="2"
            :max="99999999"
            :disabled="disabledVal"
            @blur="editAllPrice(record,'ruleValue')"
            placeholder="请输入"
            v-model="record.ruleValue"
            :id="'productTable-ruleValue'+record.id "
            size="small"/>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text,record">
          <a-button
            size="small"
            type="link"
            class="button-error"
            :id="'productTable-del-btn'+record.id "
            :disabled="disabledVal"
            @click="handleDel(record)"
          >删除</a-button>
        </template>
      </s-table>
    </a-spin>
  </div>
</template>
<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
// 接口
import { promoRuleProductList, deleteBatch, promoUpdateBatch } from '@/api/promoActive'
export default {
  name: 'ProductTable',
  mixins: [commonMixin],
  components: { STable },
  props: {
    promoActiveSn: {// 活动sn
      type: String,
      default: ''
    },
    disabledVal: {// 修改编辑时，禁用标志
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      rowSelectionInfo: null, // 已选数据
      queryParam: {}, // 查询条件
      dataListQty: '0', // 表格数据总条数
      columns: [// 表头
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'origCode', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '活动价', scopedSlots: { customRender: 'price' }, width: '10%', align: 'right' },
        { title: '返券金额', scopedSlots: { customRender: 'ruleValue' }, width: '10%', align: 'right' },
        { title: '参考终端价', dataIndex: 'terminalPrice', width: '10%', align: 'right', customRender: text => { return ((text || text == 0) ? this.toThousands(text) : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        params.promoActiveSn = this.promoActiveSn
        return promoRuleProductList(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 (!data.list[i].price) {
                data.list[i].price = data.list[i].terminalPrice
              } else {
                data.list[i].price = data.list[i].price
              }
            }
            this.disabled = false
            this.dataListQty = res.data.count
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 批量修改价格
    editAllPrice (row, typeName) {
      let ajaxData = []
      if (typeName != 'all') {
        ajaxData.push({
          id: row.id,
          price: typeName === 'price' ? row.price : undefined,
          ruleValue: typeName === 'ruleValue' ? row.ruleValue : undefined
        })
      } else {
        ajaxData = row || []
      }
      promoUpdateBatch(ajaxData).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh(true)
          this.$refs.table.clearSelected() // 清空表格选中项
        }
      })
    },
    // 修改产品活动价、返券金额
    editMorePrice (numInfo) {
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRows && this.rowSelectionInfo.selectedRows.length === 0)) {
        this.$message.warning('请选择要修改的产品！')
        return
      }
      const ajaxArr = this.rowSelectionInfo.selectedRows.map(item => { return { id: item.id, ruleValue: numInfo } })
      this.editAllPrice(ajaxArr, 'all')
    },
    // 删除
    handleDel (row) {
      const _this = this
      _this.spinning = true
      deleteBatch([row.id]).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 获取表格总条数
    getDataNum () {
      return this.dataListQty
    },
    // 初始化
    pageInit () {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    }
  },
  mounted () {
    this.pageInit()
  },
  activated () {
    this.pageInit()
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style>
 /* form input[type='radio'], form input[type='checkbox'] {
      width: 15px;
      height: 14px;
  } */
</style>
