<template>
  <!-- 已选配件列表 -->
  <s-table
    class="sTable"
    ref="table"
    size="small"
    :rowKey="(record) => record.id"
    :columns="columns"
    :data="loadData"
    :scroll="{ y: 300 }"
    :defaultLoadData="false"
    bordered>
    <!-- 销售数量 -->
    <template slot="salesNums" slot-scope="text, record">
      <a-input-number
        id="salesEdit-qty"
        size="small"
        v-model="record.qty"
        :precision="0"
        :min="1"
        :max="99999999"
        placeholder="请输入"
        @blur="e => onCellBlur(e.target.value, record)"
        style="width: 100%;" />
    </template>
    <!-- 操作 -->
    <template slot="action" slot-scope="text, record">
      <a-button
        size="small"
        type="link"
        :loading="delLoading"
        class="button-error"
        @click="handleDel(record)"
      >删除</a-button>
    </template>
  </s-table>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { salesDetailList } from '@/api/salesDetail'
import { STable, VSelect } from '@/components'
export default {
  name: 'Promotable',
  mixins: [commonMixin],
  components: { STable, VSelect },
  props: {
    delLoading: Boolean
  },
  data () {
    return {
      productForm: {
        salesBillSn: '',
        promotionFlag: 1
      },
      salesBillSn: '',
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.productForm.salesBillSn = this.salesBillSn
        return salesDetailList(Object.assign(parameter, this.productForm)).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
              data.list[i].qtyBackups = data.list[i].qty
            }
          }
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productEntity.code', width: '18%', align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '原售价', dataIndex: 'origPrice', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '促销价', dataIndex: 'price', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '数量', dataIndex: 'qty', scopedSlots: { customRender: 'salesNums' }, width: '8%', align: 'center' },
        { title: '单位', dataIndex: 'productEntity.unit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销类型', dataIndex: 'promotionRulesName', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
      if (this.$hasPermissions('B_salesEdit_costPrice')) { // 成本价权限
        arr.splice(3, 0, { title: '成本价', dataIndex: 'showCost', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      if (this.$hasPermissions('B_salesEdit_salesPrice')) { // 售价权限
        const ind = this.$hasPermissions('B_salesEdit_costPrice') ? 4 : 3
        arr.splice(ind, 0, { title: '原售价', dataIndex: 'origPrice', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(ind + 1, 0, { title: '促销价', dataIndex: 'price', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      return arr
    }
  },
  methods: {
    // 刷新当前页面
    resetCurForm () {
      this.$refs.table.refresh()
    },
    // 修改数量
    onCellBlur (val, record) {
      this.$emit('onCellBlur', val, record)
    },
    // 删除
    handleDel (row) {
      this.$emit('del', row)
    },
    getData (salesBillSn) {
      this.salesBillSn = salesBillSn
      this.$refs.table.refresh(true)
    }
  }
}
</script>

<style>
</style>
