<template>
  <div class="inventoryCheckingDetail-wrap">
    <a-page-header :ghost="false" @back="handleBack" class="inventoryCheckingDetail-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="inventoryCheckingDetail-back-btn" href="javascript:;" @click="handleBack">返回列表</a>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="1" type="danger" @click="handleExport" :loading="exportLoading" id="inventoryCheckingDetail-export-btn">导出</a-button>
      </template>
    </a-page-header>
    <!-- 基础信息 -->
    <a-card size="small" :bordered="false" class="inventoryCheckingDetail-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1" header="基础信息">
          <a-descriptions :column="3">
            <a-descriptions-item label="盘点单号">箭冠营销中心</a-descriptions-item>
            <a-descriptions-item label="监盘审核状态">箭冠营销中心</a-descriptions-item>
            <a-descriptions-item label="完结状态">箭冠营销中心</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <a-card size="small" :bordered="false" class="inventoryCheckingDetail-cont">
      <!-- 总计 -->
      <a-alert type="info" showIcon style="margin-bottom:15px">
        <div slot="message">
          库存总数量：
          <strong>6</strong>
          ，盈亏总数量：
          <strong>6</strong>
        </div>
      </a-alert>
      <!-- 筛选条件 -->
      <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-select placeholder="请选择" id="inventoryCheckingDetail-type" allowClear v-model="queryParam.dataSourceNo">
                  <a-select-option v-for="item in brandData" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 24px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryCheckingDetail-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="inventoryCheckingDetail-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="record => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 820 }"
        bordered>
        <!-- 采购数量 -->
        <template slot="purchaseNum" slot-scope="text, record">
          <span>{{ record.purchaseNum }}</span>
        </template>
        <!-- 缺货数量 -->
        <template slot="outOfStockNum" slot-scope="text, record">
          <span>{{ record.outOfStockNum }}</span>
        </template>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
export default {
  components: { STable, VSelect },
  data () {
    return {
      queryParam: {
        dataSourceNo: undefined
      },
      exportLoading: false, // 导出loading
      disabled: false, //  查询、重置按钮是否可操作
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productCode', width: 140, align: 'center' },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'productOrigUnit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', dataIndex: 'qty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘点数量', dataIndex: 'qtsy', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盈亏数量', dataIndex: 'qdty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // return customerBundleDelayList( 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
        // })
        const _this = this
        return new Promise(function (resolve, reject) {
          const data = {
            pageNo: 1,
            pageSize: 10,
            list: [
              { id: '1', purchaseNo: 'jgqp11111111111', creatDate: '产品1', custName: 'jgqp111123545', totalP: '箭冠品牌', totalNums: '产品分类1', totalPrice: '5', payType: '122' }
            ],
            count: 10
          }
          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
          resolve(data)
        })
      },
      brandData: []
    }
  },
  methods: {
    //  重置
    resetSearchForm () {},
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/inventoryManagement/inventoryChecking/list' })
    },
    //  导出
    handleExport () {
      const params = this.queryParam
      if (this.oldTime && this.oldTime.length > 0) {
        params.beginOldExpiryDate = moment(this.oldTime[0]).format('YYYY-MM-DD')
        params.endOldExpirydDate = moment(this.oldTime[1]).format('YYYY-MM-DD')
      } else {
        params.beginOldExpiryDate = ''
        params.endOldExpirydDate = ''
      }
      if (this.newTime && this.newTime.length > 0) {
        params.beginDate = moment(this.newTime[0]).format('YYYY-MM-DD')
        params.endDate = moment(this.newTime[1]).format('YYYY-MM-DD')
      } else {
        params.beginDate = ''
        params.endDate = ''
      }
      this.exportLoading = true
      customerBundleExportDelay(params).then(res => {
        this.exportLoading = false
        this.download(res)
      })
    },
    download (data) {
      if (!data) {
        return
      }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDHHmmss')
      const fname = '仓库调拨' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  }
}
</script>

<style lang="less">
.inventoryCheckingDetail-cont,
.inventoryCheckingDetail-back {
  margin-bottom: 15px;
}
</style>
