<template>
  <div class="salesReturnList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <div style="width:80%">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="salesReturnList-productCode" v-model.trim="queryParam.code" 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.name" 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>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :customRow="handleClickRow"
      :defaultLoadData="false"
      :scroll="{ 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="showReceiveQty?0:1"
            :max="99999999"
            style="width: 100%;"
            placeholder="请输入" />
        </div>
      </template>
      <!-- 仓库实收数量 -->
      <template slot="receiveQty" slot-scope="text, record">
        <div @dblclick.stop>
          <a-input-number
            size="small"
            v-model="record.receiveQty"
            :precision="0"
            :min="1"
            :max="99999999"
            style="width: 100%;"
            placeholder="请输入" />
        </div>
      </template>
      <!-- 退货原因 -->
      <template slot="returnReason" slot-scope="text, record">
        <div @dblclick.stop>
          <returnReason :goodFlag="goodFlag" v-model.trim="record.returnReasonCode"></returnReason>
        </div>
      </template>
      <!-- 备注 -->
      <template slot="remarks" slot-scope="text, record">
        <div @dblclick.stop>
          <a-input :maxLength="50" size="small" v-model="record.returnReasonRemarks" placeholder="请输入备注(最多50字符)"/>
        </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="link"
          class="button-info"
          :disabled="newLoading"
          v-if="record.productPrice"
          @click="handleAdd(record)"
        >添加</a-button>
        <span v-else>--</span>
      </template>
    </s-table>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { queryDealerProductPage } from '@/api/salesReturn'
import returnReason from '@/views/common/returnReason'
import { STable, VSelect } from '@/components'
export default {
  name: 'QueryPart',
  mixins: [commonMixin],
  components: { STable, VSelect, returnReason },
  props: {
    newLoading: Boolean,
    isShowPrice: {
      type: Boolean,
      default: true
    },
    // 是否显示仓库实收数量
    showReceiveQty: {
      type: Boolean,
      default: false
    },
    showRemark: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      buyerSn: '',
      priceType: '',
      queryParam: { //  查询条件
        name: '', // 产品名称
        code: '' //  产品编码
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [],
      goodFlag: '',
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.queryParam.dealerSn = this.buyerSn
        return queryDealerProductPage(Object.assign(parameter, this.queryParam)).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].qty = this.showReceiveQty ? 0 : 1
              data.list[i].receiveQty = 1
              data.list[i].returnReasonRemarks = ''
            }
            this.disabled = false
          }
          return data
        })
      }
    }
  },
  methods: {
    // 双击列表选择配件
    handleClickRow (record) {
      const _this = this
      return {
        on: {
          dblclick: (event) => {
            event.stopPropagation()
            if (record.productPrice) {
              _this.$emit('add', record)
            }
          }
        }
      }
    },
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.queryParam.code = ''
      this.$refs.table.refresh(true)
    },
    pageInit (buyerSn, warehouseSn, goodFlag) {
      this.buyerSn = buyerSn
      this.goodFlag = goodFlag
      this.queryParam.warehouseSn = warehouseSn
      const arr = [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '产品编码', dataIndex: 'code', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'name', width: '23%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'unit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请退货数量', dataIndex: 'qty', width: '8%', align: 'center', scopedSlots: { customRender: 'qty' } },
        { title: '退货原因', dataIndex: 'returnReason', width: '12%', align: 'center', scopedSlots: { customRender: 'returnReason' } },
        { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
      let i = 5
      if (this.$hasPermissions('B_salesReturnEdit_salesPrice') && this.isShowPrice) { //  售价权限
        arr.splice(4, 0, { title: '当前售价', dataIndex: 'productPrice', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(5, 0, { title: '价格级别', dataIndex: 'priceLevelDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } })
        i = 6
      }
      // 实收数量是否显示
      if (this.showReceiveQty) {
        arr.splice(i, 0, { title: '仓库实收数量', dataIndex: 'receiveQty', width: '8%', align: 'center', scopedSlots: { customRender: 'receiveQty' } })
      }
      if (this.showRemark) {
        arr.splice(i + 2, 0, { title: '备注', dataIndex: 'remarks', width: '12%', align: 'center', scopedSlots: { customRender: 'remarks' } })
      }
      this.columns = arr
      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>
