<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.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>
    </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="1"
            :max="999999"
            style="width: 100%;"
            placeholder="请输入" />
        </div>
      </template>
      <!-- 退货原因 -->
      <template slot="returnReason" slot-scope="text, record">
        <div @dblclick.stop>
          <returnReason v-model="record.returnReason"></returnReason>
        </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 { queryStockProductPage } from '@/api/stock'
import returnReason from '@/views/common/returnReason'
import { STable, VSelect } from '@/components'
export default {
  name: 'QueryPart',
  components: { STable, VSelect, returnReason },
  props: {
    newLoading: Boolean,
    isShowPrice: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      buyerSn: '',
      priceType: '',
      queryParam: { //  查询条件
        productName: '', // 产品名称
        productCode: '' //  产品编码
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.queryParam.dealerSn = this.buyerSn
        return queryStockProductPage(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 = 1
            }
            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.productName = ''
      this.queryParam.productCode = ''
      this.$refs.table.refresh(true)
    },
    pageInit (buyerSn) {
      this.buyerSn = buyerSn
      const arr = [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '24%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '27%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'productUnit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '当前售价', dataIndex: 'productPrice', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '申请退货数量', dataIndex: 'qty', width: '9%', align: 'center', scopedSlots: { customRender: 'qty' } },
        { title: '退货原因', dataIndex: 'returnReason', width: '10%', align: 'center', scopedSlots: { customRender: 'returnReason' } },
        { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
      if (this.$hasPermissions('B_isShowPrice') && this.isShowPrice) { //  售价权限
        arr.splice(4, 0, { title: '当前售价', dataIndex: 'productPrice', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      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>
