<template>
  <div class="productInfoList-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="审核时间">
              <rangeDate ref="rangeDate" @change="dateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品编码">
              <a-input id="productInfoList-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="productInfoList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced && grabFlag == 1">
            <a-col :md="6" :sm="24">
              <a-form-item label="销售单号">
                <a-input id="productInfoList-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24" style="margin-bottom: 10px">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productInfoList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productInfoList-reset">重置</a-button>
            <a @click="advanced=!advanced" style="margin:0 15px 0 8px" v-if="grabFlag == 1">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
          </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"
      :defaultLoadData="false"
      :pageSize="10"
      :scroll="{ y: 144 }"
      bordered>
      <!-- 价格 -->
      <template slot="price" slot-scope="text, record">{{ text }}</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.qty==record.hasReturnQty)"
          @click="handleAdd(record)"
        >添加</a-button>
      </template>
    </s-table>
  </div>
</template>

<script>
import { salesReturnProductList } from '@/api/salesReturn'
import { stockList } from '@/api/stock'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
export default {
  name: 'QueryPart',
  components: { STable, VSelect, rangeDate },
  props: {
    // buyerSn: {
    //   type: [Number, String],
    //   default: ''
    // },
    newLoading: Boolean,
    grabFlag: [Number, String]
    // priceType: [Number, String]
  },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        salesBillNo: '', //  销售单号
        productName: '', // 产品名称
        productCode: '' //  产品编码
      },
      buyerSn: '',
      priceType: '',
      disabled: false, //  查询、重置按钮是否可操作
      columns: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // 抓单时
        if (this.grabFlag == 1) {
          this.queryParam.buyerSn = this.buyerSn
        }
        const fun = [stockList, salesReturnProductList]
        if (this.grabFlag == 0) {
          this.queryParam.salePriceType = this.priceType
        }
        return fun[this.grabFlag](Object.assign(parameter, this.queryParam)).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
            if (this.grabFlag == 0) {
              data.list[i].salePrice = data.list[i].lastStockCost
              data.list[i].putCost = data.list[i].lastStockCost
            }
          }
          this.listData = data.list || []
          this.disabled = false
          return data
        })
      },
      listData: []
    }
  },
  mounted () {
    // 抓单
    if (this.grabFlag == 1) {
      this.columns = [
        { title: '序号', dataIndex: 'no', align: 'center', width: '4%' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: '14%', align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', dataIndex: 'salesBillNo', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售审核时间', dataIndex: 'auditDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '售价', dataIndex: 'price', align: 'center', width: '5%', scopedSlots: { customRender: 'price' } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售数量', dataIndex: 'qty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '已退数量', dataIndex: 'hasReturnQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: '7%', align: 'center' }]
    } else {
      this.columns = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '22%', align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '产品名称', dataIndex: 'productName', width: '22%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '21%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '售价', dataIndex: 'salePrice', width: '10%', align: 'center', scopedSlots: { customRender: 'price' } },
        { title: '单位', dataIndex: 'productUnit', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }]
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 双击列表选择配件
    handleClickRow (record) {
      const _this = this
      return {
        on: {
          dblclick: (event) => {
            if ((_this.grabFlag == 1) && (record.qty == record.hasReturnQty)) {
              _this.$message.info('该产品暂时不能添加')
            } else {
              event.stopPropagation()
              _this.$emit('add', record)
            }
          }
        }
      }
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate && this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.salesBillNo = ''
      this.queryParam.productName = ''
      this.queryParam.productCode = ''
      this.$refs.table.refresh(true)
    },
    pageInit (buyerSn, priceType) {
      console.log(buyerSn, priceType)
      this.buyerSn = buyerSn
      this.priceType = priceType
      this.resetSearchForm()
    },
    refreshData () {
      if (this.listData.length > 0) {
        this.$refs.table.refresh(true)
      }
    },
    // 选择配件
    handleAdd (row) {
      this.$emit('add', row, 'new')
    }
  }
}
</script>
