<template>
  <a-card size="small" :bordered="false" class="approveStore-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="日期">
                <rangeDate ref="rangeDate" v-model="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="经销商">
                <storeList ref="storeList" @change="custChange"></storeList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <div>
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="approveStore-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="approveStore-reset">重置</a-button>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.no"
        rowkeyName="no"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 数量-->
        <template slot="countVal" slot-scope="text, record">
          <a-button class="button-info" type="link" @click="handleDetail(record)" id="approveStore-refresh">{{ record.queryCount||record.queryCount==0? record.queryCount:'--' }}</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 数量 -->
    <recordModal ref="recordModal" :openModal="openRecordModal" @close="openRecordModal=false" />
  </a-card>
</template>

<script>
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import storeList from '@/views/common/storeList.vue'
import recordModal from './recordModal.vue'
// 接口
import { vinPartLogDayList } from '@/api/vinPartLog'
export default {
  components: { STable, VSelect, rangeDate, storeList, recordModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      // 查询条件
      queryParam: {
        dealerSn: undefined, // 经销商sn
        beginDate: undefined, // 开始日期
        endDate: undefined// 结束日期
      },
      time: [], // 查询日期
      openRecordModal: false, // 数量弹窗
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '日期', dataIndex: 'dateInfo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '经销商名称', dataIndex: 'dealerName', width: '40%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '数量', dataIndex: 'queryCount', width: '20%', align: 'center', scopedSlots: { customRender: 'countVal' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const parames = Object.assign(parameter, this.queryParam)
        return vinPartLogDayList(parames).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
              if (data.list[i].queryYeary && data.list[i].queryMonth && data.list[i].queryDay) {
                data.list[i].dateInfo = data.list[i].queryYeary + '-' + String(data.list[i].queryMonth).padStart(2, '0') + '-' + String(data.list[i].queryDay).padStart(2, '0')
              }
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] + ' 00:00:00'
      this.queryParam.endDate = date[1] + ' 23:59:60'
    },
    // 经销商查询
    custChange (val) {
      this.queryParam.dealerSn = val ? val.key : undefined
    },
    // 数量弹窗
    handleDetail (row) {
      const ajaxData = {
        dealerSn: row.tenantId,
        queryYeary: row.queryYeary,
        queryMonth: row.queryMonth,
        queryDay: row.queryDay
      }
      this.$refs.recordModal.pageInit(ajaxData)
      this.$nextTick(() => {
        this.openRecordModal = true
      })
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.queryParam.dealerSn = undefined
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.$refs.rangeDate.resetDate(this.time)
      this.$refs.storeList.resetForm()
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 330
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
        _this.resetSearchForm()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时,需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开,则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表,不重置页面
    if (this.$store.state.app.updateList) {
      this.setTableH()
      this.$refs.table.refresh()
    }
  }
}
</script>