<template>
  <div class="salesReturnList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24" v-if="grabFlag == 1">
            <a-form-item label="销售单号">
              <a-input id="salesReturnList-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品编码">
              <a-input id="salesReturnList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="salesReturnList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="salesReturnList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="salesReturnList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :customRow="handleClickRow"
      :rowClassName="(record, index) => (grabFlag == '1'&&record.refundableQty==0) ? 'redBg-row':''"
      :defaultLoadData="false"
      :scroll="{ x: grabFlag == '1' ? 930 : 710, y: 300 }"
      bordered>
      <!-- 本次退货数量 -->
      <template slot="qty" slot-scope="text, record">
        <div @dblclick.stop>
          <a-input-number
            size="small"
            v-model="record.qty"
            :precision="0"
            :min="0"
            :max="999999"
            placeholder="请输入" />
        </div>
      </template>
      <span slot="customTitle">
        操作
        <a-popover>
          <template slot="content">
            双击列表配件可快速选中添加
          </template>
          <a-icon type="question-circle" theme="twoTone" />
        </a-popover>
      </span>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          size="small"
          type="primary"
          class="button-info"
          :loading="newLoading"
          :disabled="grabFlag == '1'&&record.refundableQty==0"
          @click="handleAdd(record)"
        >添加</a-button>
      </template>
    </s-table>
  </div>
</template>

<script>
import { salesQueryByReturn } from '@/api/salesReturn'
import { queryStockProductPage } from '@/api/stock'
import { STable, VSelect } from '@/components'
export default {
  name: 'QueryPart',
  components: { STable, VSelect },
  props: {
    newLoading: Boolean
  },
  data () {
    return {
      buyerSn: '',
      grabFlag: '',
      priceType: '',
      queryParam: { //  查询条件
        salesBillNo: '', //  销售单号
        productName: '', // 产品名称
        productCode: '' //  产品编码
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // 抓单时
        if (this.grabFlag == 1) {
          this.queryParam.buyerSn = this.buyerSn
        }
        // 不抓单
        if (this.grabFlag == 0) {
          this.queryParam.dealerSn = this.buyerSn
        }
        const fun = [queryStockProductPage, salesQueryByReturn]
        return fun[this.grabFlag](Object.assign(parameter, this.queryParam, { onlineFalg: '1' })).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
            data.list[i].qty = 1
          }
          this.disabled = false
          return data
        })
      }
    }
  },
  mounted () {
    // 抓单
    if (this.grabFlag == 1) {
      this.columns = [
        { title: '序号', dataIndex: 'no', width: 70, align: 'center' },
        { title: '销售单号', dataIndex: 'salesBillNo', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productEntity.code', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '售价', dataIndex: 'price', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '剩余可退数量', dataIndex: 'refundableQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '本次退货数量', dataIndex: 'qty', width: 100, align: 'center', scopedSlots: { customRender: 'qty' } },
        { title: '采购价', dataIndex: 'showCost', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productEntity.unit', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
        { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: 80, align: 'center', fixed: 'right' }]
    } else {
      this.columns = [
        { title: '序号', dataIndex: 'no', width: 70, align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '当前售价', dataIndex: 'productPrice', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '退货数量', dataIndex: 'qty', width: 100, align: 'center', scopedSlots: { customRender: 'qty' } },
        { title: '采购价', dataIndex: 'lastStockCost', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
        { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: 80, align: 'center' }
      ]
    }
  },
  methods: {
    // 双击列表选择配件
    handleClickRow (record) {
      const _this = this
      return {
        on: {
          dblclick: (event) => {
            if (_this.grabFlag == '1' && record.refundableQty == 0) {
              _this.$message.info('该产品暂时不能添加')
            } else {
              event.stopPropagation()
              _this.$emit('add', record)
            }
          }
        }
      }
    },
    //  重置
    resetSearchForm () {
      this.queryParam.salesBillNo = ''
      this.queryParam.productName = ''
      this.queryParam.productCode = ''
      this.$refs.table.refresh(true)
    },
    pageInit (buyerSn, grabFlag) {
      this.buyerSn = buyerSn
      this.grabFlag = grabFlag
      this.resetSearchForm()
    },
    // 选择配件
    handleAdd (row) {
      this.$emit('add', row)
    },
    refreshData () {
      this.$refs.table.refresh()
    }
  }
}
</script>
<style lang="less">
  .salesReturnList-wrap{
    .redBg-row{
      background-color: #f5cdc8;
    }
  }
</style>
