<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">
            <a-form-item label="审核时间">
              <a-range-picker v-model="shDate" id="salesReturn-shDate"/>
            </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">
            <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">
            <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productInfoList-refresh">查询</a-button>
            <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="productInfoList-reset">重置</a-button>
            <a @click="advanced=!advanced" style="margin:0 15px 0 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :customRow="handleClickRow"
      :defaultLoadData="false"
      :scroll="{ x: grabFlag == '1' ? 1700 : 860, y: 300 }"
      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"
          @click="handleAdd(record)"
        >添加</a-button>
      </template>
    </s-table>
  </div>
</template>

<script>
import { salesReturnProductList, salesQueryByReturn } from '@/api/salesReturn'
import { querySumByProductLocation } from '@/api/stock'
import { STable, VSelect } from '@/components'
import moment from 'moment'
export default {
  name: 'QueryPart',
  components: { STable, VSelect },
  props: {
    buyerSn: {
      type: [Number, String],
      default: ''
    },
    newLoading: Boolean,
    grabFlag: [Number, String],
    priceType: [Number, String]
  },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      shDate: [],
      dateFormat: 'YYYY-MM-DD',
      queryParam: { //  查询条件
        salesBillNo: '', //  销售单号
        productName: '', // 产品名称
        productCode: '' //  产品编码
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出loading
      brandData: [], //  下拉数据
      typeData: [], //  产品类别  下拉数据
      columns: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // 抓单时
        if (this.grabFlag == 1) {
          this.queryParam.buyerSn = this.buyerSn
        }
        // 创建时间
        if (this.shDate && this.shDate.length > 0) {
          this.queryParam.beginDate = moment(this.shDate[0]).format(this.dateFormat)
          this.queryParam.endDate = moment(this.shDate[1]).format(this.dateFormat)
        } else {
          this.queryParam.beginDate = undefined
          this.queryParam.endDate = undefined
        }
        const fun = [salesQueryByReturn, 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
          }
          this.disabled = false
          return data
        })
      }
    }
  },
  mounted () {
    // 抓单
    if (this.grabFlag == 1) {
      this.columns = [{
        title: '序号',
        dataIndex: 'no',
        align: 'center',
        width: 100
      },
      {
        title: '产品编码',
        dataIndex: 'dealerProductEntity.code',
        align: 'center',
        sorter: true,
        width: 160
      },
      {
        title: '产品名称',
        dataIndex: 'dealerProductEntity.name',
        align: 'center'
      },
      {
        title: '原厂编码',
        dataIndex: 'dealerProductEntity.origCode',
        align: 'center',
        customRender: function (text) { return text || '--' },
        width: 120
      },
      {
        title: '销售单号',
        dataIndex: 'salesBillNo',
        align: 'center',
        width: 160
      },
      {
        title: '销售审核时间',
        dataIndex: 'auditTime',
        align: 'center',
        customRender: function (text) { return text || '--' },
        width: 120
      },
      {
        title: '售价',
        dataIndex: 'price',
        align: 'center',
        width: 120,
        scopedSlots: { customRender: 'price' }
      },
      {
        title: '单位',
        dataIndex: 'dealerProductEntity.unit',
        align: 'center',
        width: 100
      },
      {
        title: '销售数量',
        dataIndex: 'qty',
        align: 'center',
        width: 100
      },
      {
        title: '已退数量',
        dataIndex: 'hasReturnQty',
        align: 'center',
        width: 120
      },
      { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }]
    } else {
      this.columns = [{
        title: '序号',
        dataIndex: 'no',
        align: 'center',
        width: 100
      },
      {
        title: '产品编码',
        dataIndex: 'productCode',
        align: 'center',
        sorter: true,
        width: 160
      },
      {
        title: '产品名称',
        dataIndex: 'productName',
        align: 'center'
      },
      {
        title: '原厂编码',
        dataIndex: 'productOrigCode',
        align: 'center',
        customRender: function (text) { return text || '--' },
        width: 160
      },
      {
        title: '售价',
        dataIndex: 'salePrice',
        align: 'center',
        width: 120,
        scopedSlots: { customRender: 'price' }
      },
      {
        title: '单位',
        dataIndex: 'unit',
        align: 'center',
        customRender: function (text) { return text || '--' },
        width: 120
      },
      { slots: { title: 'customTitle' }, scopedSlots: { customRender: 'action' }, width: 100, align: 'center' }]
    }
  },
  methods: {
    // 双击列表选择配件
    handleClickRow (record) {
      const _this = this
      return {
        on: {
          dblclick: (event) => {
            event.stopPropagation()
            _this.$emit('add', record)
          }
        }
      }
    },
    //  重置
    resetSearchForm () {
      this.queryParam.salesBillNo = ''
      this.queryParam.productName = ''
      this.queryParam.productCode = ''
      this.$refs.table.refresh(true)
    },
    // 选择配件
    handleAdd (row) {
      this.$emit('add', row, 'new')
    }
  }
}
</script>
