<template>
  <a-card size="small" :bordered="false" class="shelfOrder-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" 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-input id="shelfOrder-orderBillNo" v-model.trim="queryParam.orderBillNo" allowClear placeholder="请输入订单编号" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="货架名称">
                <shelfSList ref="shelfSList" mode="multiple" v-model="queryParam.shelfSnList"></shelfSList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="下单时间">
                <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="订单状态">
                <v-select
                  v-model="queryParam.billState"
                  ref="billState"
                  id="shelfOrder-billState"
                  code="ORDER_BILL_STATE"
                  placeholder="请选择订单状态"
                  allowClear></v-select>
              </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="shelfOrder-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm(0)" :disabled="disabled" id="shelfOrder-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 提示 -->
      <a-alert type="info" style="margin-bottom: 10px" v-if="countData">
        <div slot="message">总款数：{{ countData.productCategory }}；总数量：{{ countData.totalQty }}；总结算金额：{{ countData&&(countData.settleAmount||countData.settleAmount==0)?toThousands(countData.settleAmount,2):'--' }}；</div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <template slot="orderBillNo" slot-scope="text, record">
          <span class="table-td-link" @click="handleDetail(record)">{{ record.orderBillNo }}</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="link" class="button-primary" @click="handleSet(record)">设置</a-button>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import shelfSList from '@/views/common/shelfList'
import rangeDate from '@/views/common/rangeDate.vue'
import { orderBillQueryPage, orderBillQueryCount } from '@/api/shelf'
export default {
  components: { STable, VSelect, shelfSList, rangeDate },
  mixins: [commonMixin],
  data () {
    const _this = this
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      time: [],
      queryParam: {
        orderStartDate: '',
        orderEndDate: '',
        shelfSnList: undefined,
        orderBillNo: '',
        billState: undefined
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '订单编号', scopedSlots: { customRender: 'orderBillNo' }, width: '15%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfName', width: '34%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '总款数', dataIndex: 'productCategory', align: 'center', width: '8%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'totalQty', align: 'center', width: '8%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总结算金额', dataIndex: 'settleAmount', align: 'right', width: '8%', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } },
        { title: '下单时间', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '订单状态', dataIndex: 'billStateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        this.orderBillQueryCount(Object.assign(parameter, this.queryParam))
        return orderBillQueryPage(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
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false,
      countData: null
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.orderStartDate = date[0] || null
      this.queryParam.orderEndDate = date[1] || null
    },
    handleDetail (data) {
      this.$router.push({ name: 'shelfOrderDetail', params: { sn: data.orderBillSn } })
    },
    orderBillQueryCount (params) {
      orderBillQueryCount(params).then(res => {
        this.countData = res.data || null
      })
    },
    // 重置
    resetSearchForm (flag) {
      this.time = []
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.orderStartDate = ''
      this.queryParam.orderEndDate = ''
      this.queryParam.orderBillNo = ''
      this.queryParam.billState = undefined
      console.log(flag)
      if (this.$route.query && this.$route.query.bizType == 'SHELF_WARN' && flag == 1) {
        this.queryParam.shelfSnList = [this.$route.query.shelfSn]
      } else {
        this.queryParam.shelfSnList = undefined
      }
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm(1)
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab || this.$route.query && this.$route.query.bizType == 'SHELF_WARN') {
      this.pageInit()
      this.resetSearchForm(1)
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
