<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table class="table-box" ref="multipleTable" :data="tableData" stripe style="width: 100%;"
                @selection-change="handleSelectionChange"
                :height="tool.getTableHeight()">
        <el-table-column type="selection" width="70" v-if="tableHeaders"></el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''" :prop="tableHeader.other.prop ? tableHeader.other.prop : null">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" >{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="success" size="small" @click="handleImport" v-show="permission.hasPermission(`shop/order-shop-list-import`)">订单导入</el-button>

        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
    <el-dialog title="订单导入" :visible.sync="importDialogVisible" width="50%">
      <el-form ref="form" :model="excelForm" style="width: 100%;" class="form-page">
        <el-form-item label="当前导入订单日期">
          <el-date-picker  v-model="excelForm.orderDay" type="date" placeholder="选择日期"  value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前导入订单类型">
          <el-select v-model="excelForm.orderType" placeholder="请选择订单类型" style="width: 400px;">
            <el-option label="复销订单" value="cash" :key="1"></el-option>
            <el-option label="积分订单" value="point" :key="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-show="false" label="当前导入订单期数">
          <el-input v-model="excelForm.periodNum"></el-input>
        </el-form-item>
        <el-form-item label="Excel 文件总行数">
          <el-input v-model="excelForm.rowCount"></el-input>
        </el-form-item>
        <el-form-item>
          <leo-excel-uploader
            :request-upload-route="`file/upload-excel`"
            :request-import-to-excel-table-route="`shop/import-order-shop-to-excel-table`"
            :request-import-excel-table-to-data-route="`shop/import-order-shop`"
            :import-row-count="excelForm.rowCount"
            :other-params="excelForm"
            :finish-callback-func="uploadFinishFunc"
            excel-option="addUser"
            upload-btn-title="Excel导入"
            style="float: left;"
          ></leo-excel-uploader>
<!--          <el-button type="primary" @click="handleExcel" style="float: left; margin-left: 10px;">下载模板</el-button>-->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import baseInfo from '@/utils/baseInfo'
  import FilterUser from '@/components/FilterUser'
  import permission from '@/utils/permission'
  import LeoExcelUploader from '../../components/ExcelUploader'
  import Pagination from '@/components/Pagination'
  import filterHelper from '@/utils/filterHelper'
  import {CDN_BASE_URL} from "../../utils/config";

  export default {
    name: 'shop_order_shop-list',
    components: {FilterUser, Pagination, LeoExcelUploader},
    mounted() {
      this.initData()
      this.getData()
    },
    data() {
      return {
        tableHeaders: null,
        tableData: null,
        tableHeight: window.innerHeight - 310,
        loading: true,
        multipleSelection: [],
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: null,
        filterModel: {},
        dialogDeliveryVisible: false,
        deliveryForm: {
          sn: '',
          expressCompany: '',
          orderTrackNo: '',
        },
        excelForm: {
          rowCount: 0,
          orderType: '',
          periodNum: 0,
          orderDay: 0,
        },
        importDialogVisible:false,
      }
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      initData(){
        let myDate = new Date();
        let thisYear = myDate.getFullYear();
        let thisMonth = myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1
        let thisDate = myDate.getDate() < 10 ? '0' + (myDate.getDate()) : myDate.getDate()
        this.excelForm.orderDay = thisYear + '-' + thisMonth + '-' + thisDate
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        network.getPageData(this, 'shop/order-shop-list', page, pageSize, filterData, response => {
          this.filterTypes = response.filterTypes
          this.allData = response
        })
      },
      handleExcel() {
        window.open(CDN_BASE_URL + `/files/order-shop-import.xlsx`)
      },
      handleImport() {
        this.importDialogVisible = true
      },
      uploadFinishFunc(){
        this.importDialogVisible = false
        this.getData()
      }
    }
  }
</script>

<style scoped>
</style>
