<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"
      :defaultLoadData="false"
      :pageSize="10"
      :scroll="{ y: 149 }"
      bordered>
      <!-- 价格 -->
      <template slot="price" slot-scope="text, record">
        <span v-if="grabFlag==1">{{ toThousands(text) }}</span>
        <div v-else>
          <a-input-number
            size="small"
            v-model="record.salePrice"
            :precision="2"
            :min="0"
            :max="999999"
            placeholder="请输入"
            style="width: 100%;" />
        </div>
      </template>
      <span slot="customTitle">
        操作
        <!-- <a-popover>
          <template slot="content">
            双击列表配件可快速选中添加
          </template>
          <a-icon type="question-circle" theme="twoTone" />
        </a-popover> -->
      </span>
      <!-- 本次退货数量 -->
      <template slot="returnQty" slot-scope="text, record">
        <a-input-number
          size="small"
          v-model="record.returnQty"
          :precision="0"
          :min="0"
          :max="record.qty-record.hasReturnQty"
          placeholder="请输入"
          style="width: 100%;"
          v-if="(grabFlag==1)&&(record.qty!=record.hasReturnQty)||grabFlag==0"
        />
        <span v-else>--</span>
      </template>
      <!-- 废品数量 -->
      <template slot="celQty" slot-scope="text, record">
        <a-input-number
          size="small"
          v-model="record.celQty"
          :precision="0"
          :min="0"
          :max="record.returnQty"
          placeholder="请输入"
          style="width: 100%;"
          v-if="(grabFlag==1)&&(record.qty!=record.hasReturnQty)||grabFlag==0"/>
        <span v-else>--</span>
      </template>
      <!-- 仓库仓位 -->
      <template slot="warehouse" slot-scope="text, record, index">
        <a-cascader
          size="small"
          @change="e => changeWarehouseCascade(e, record, index)"
          v-model="record.warehouseCascade"
          expand-trigger="hover"
          :options="warehouseCascadeData"
          :allowClear="false"
          :fieldNames="{ label: 'name', value: 'sn', children: 'warehouseLocationList' }"
          placeholder="请选择仓库仓位"
          style="width: 100%;"
          v-if="(grabFlag==1)&&(record.qty!=record.hasReturnQty)||grabFlag==0"/>
        <span v-else>--</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <span v-if="(grabFlag==1)&&(record.qty==record.hasReturnQty)">--</span>
        <a-button
          v-else
          size="small"
          type="link"
          class="button-primary"
          :loading="newLoading"
          @click="handleAdd(record)"
        >添加</a-button>
      </template>
    </s-table>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
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 },
  mixins: [commonMixin],
  props: {
    // buyerSn: {
    //   type: [Number, String],
    //   default: ''
    // },
    warehouseCascadeData: {
      type: Array,
      default: function () {
        return []
      }
    },
    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 => {
          console.log(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
              // 不抓单
              if (this.grabFlag == 0) {
                console.log(this.warehouseCascadeData, data)
                data.list[i].putCost = data.list[i].lastStockCost
                const warehouse = this.warehouseCascadeData.find(item => item.sysFlag == 1 && item.wasteFlag == 0)
                if (warehouse) {
                  const warehouseLocation = warehouse.warehouseLocationList.find(item => item.sysFlag == 1 && item.wasteFlag == 0)
                  if (warehouseLocation) {
                    data.list[i].warehouseCascade = [warehouse.warehouseSn, warehouseLocation.warehouseLocationSn]
                  }
                }
                data.list[i].returnQty = 1
                data.list[i].celQty = 0
              } else {
                // 抓单
                data.list[i].returnQty = data.list[i].qty - data.list[i].hasReturnQty
                data.list[i].celQty = 0
                const warehouseSn = data.list[i].warehouseSn || undefined
                const warehouseLocationSn = data.list[i].warehouseLocationSn || undefined
                if (warehouseSn || warehouseLocationSn) {
                  data.list[i].warehouseCascade = [warehouseSn, warehouseLocationSn]
                } else {
                  data.list[i].warehouseCascade = undefined
                }
              }
            }
            this.listData = data.list || []
            this.disabled = false
          }
          return data
        })
      },
      listData: []
    }
  },
  mounted () {
    const _this = this
    // 抓单
    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: 'right', 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 : '--') } },
        { title: '本次退货数量', align: 'center', width: '7%', scopedSlots: { customRender: 'returnQty' } },
        { title: '废品数量', align: 'center', width: '6%', scopedSlots: { customRender: 'celQty' } },
        { title: '仓库仓位', scopedSlots: { customRender: 'warehouse' }, width: '10%', align: 'center' },
        { 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: 'right', scopedSlots: { customRender: 'price' } },
        { title: '单位', dataIndex: 'productUnit', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库仓位', scopedSlots: { customRender: 'warehouse' }, width: '10%', align: 'center' },
        { title: '本次退货数量', align: 'center', width: '7%', scopedSlots: { customRender: 'returnQty' } },
        { title: '废品数量', align: 'center', width: '6%', scopedSlots: { customRender: 'celQty' } },
        { 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, 'new')
            }
          }
        }
      }
    },
    // 修改仓库仓位
    changeWarehouseCascade (val, opt, ind) {
      console.log(val, opt, ind)
      const loadData = this.listData[ind]
      if (val.length < 2) {
        this.$message.warning('当前仓库无仓位，请选择其他仓库')
        const warehouseSnBackups = loadData.warehouseSnBackups || undefined
        const warehouseLocationSnBackups = loadData.warehouseLocationSnBackups || undefined
        loadData.warehouseSn = warehouseSnBackups
        loadData.warehouseLocationSn = warehouseLocationSnBackups
        if (warehouseSnBackups || warehouseLocationSnBackups) {
          loadData.warehouseCascade = [warehouseSnBackups, warehouseLocationSnBackups]
        } else {
          loadData.warehouseCascade = undefined
        }
      } else {
        loadData.warehouseSn = val[0] ? val[0] : ''
        loadData.warehouseLocationSn = val[1] ? val[1] : ''
        loadData.warehouseSnBackups = val[0] ? val[0] : ''
        loadData.warehouseLocationSnBackups = val[1] ? val[1] : ''
      }
    },
    //  重置
    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.refreshData()
    },
    refreshData () {
      this.$refs.table.refresh()
    },
    // 选择配件
    handleAdd (row) {
      this.$emit('add', row, 'new')
    }
  }
}
</script>
