<template>
  <!-- 已选配件列表 -->
  <s-table
    class="sTable"
    ref="table"
    size="small"
    :rowKey="(record) => record.id"
    :columns="columns"
    :data="loadData"
    :scroll="{ x:1430, y: 300 }"
    :defaultLoadData="false"
    bordered>
    <!-- 产品名称 -->
    <template slot="productName" slot-scope="text, record">
      <span>{{ text }}</span>
    </template>
    <!-- 销售数量 -->
    <template slot="salesNums" slot-scope="text, record">
      <editable-cell
        size="small"
        :text="record.qty"
        :max="999999"
        :min="1"
        :precision="0"
        @change="onCellChange(record, $event)" />
    </template>
    <!-- 操作 -->
    <template slot="action" slot-scope="text, record">
      <a-button
        size="small"
        type="primary"
        :loading="delLoading"
        class="button-error"
        @click="handleDel(record)"
      >删除</a-button>
    </template>
  </s-table>
</template>

<script>
import { salesDetailList } from '@/api/salesDetail'
import { STable, VSelect } from '@/components'
import EditableCell from '@/views/common/editInput.js'
export default {
  name: 'Promotable',
  components: { STable, VSelect, EditableCell },
  props: {
    salesBillSn: {
      type: [Number, String],
      default: ''
    },
    delLoading: Boolean
  },
  data () {
    return {
      productForm: {
        salesBillSn: '',
        promotionFlag: 1
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '产品编码', dataIndex: 'productEntity.code', width: 220, align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '产品名称', dataIndex: 'productEntity.name', scopedSlots: { customRender: 'productName' }, align: 'center' },
        { title: '成本价', dataIndex: 'showCost', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? ('￥' + text) : '--') } },
        { title: '原售价', dataIndex: 'origPrice', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? ('￥' + text) : '--') } },
        { title: '促销价', dataIndex: 'price', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? ('￥' + text) : '--') } },
        { title: '数量', dataIndex: 'qty', scopedSlots: { customRender: 'salesNums' }, width: 150, align: 'center' },
        { title: '单位', dataIndex: 'productEntity.unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销类型', dataIndex: 'promotionRulesName', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.productForm.salesBillSn = this.salesBillSn
        return salesDetailList(Object.assign(parameter, this.productForm)).then(res => {
          const 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
          }
          return data
        })
      }
    }
  },
  methods: {
    // 刷新当前页面
    resetCurForm () {
      this.$refs.table.refresh()
    },
    // 修改数量
    onCellChange (row, $event) {
      this.$emit('onCellChange', row, $event)
    },
    // 删除
    handleDel (row) {
      this.$emit('del', row)
    },
    getData () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>

<style>
</style>
