<template>
  <div class="divBox">
    <div class="selCard">
      <el-form size="small" inline label-width="85px">
        <el-form-item label="商户名称：">
          <el-select
            v-model="tableForm.mer_id"
            clearable
            filterable
            placeholder="请选择"
            class="selWidth"
            @change="getList(1)"
          >
            <el-option
              v-for="item in merSelect"
              :key="item.mer_id"
              :label="item.mer_name"
              :value="item.mer_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="账单日期：">
          <el-date-picker
            v-model="tableForm.date"
            value-format="yyyy-MM"
            format="yyyy-MM"
            type="month"
            clearable
            @change="getList(1)"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <el-button class="mb10" type="primary" size="small" @click="exports">导出列表</el-button>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="small"
        class="table"
        highlight-current-row
      >
        <el-table-column label="序号" min-width="90">
          <template scope="scope">
            <span>{{ scope.$index+(tableForm.page - 1) * tableForm.limit + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="mer_name"
          label="商户名称"
          min-width="150"
        />
        <el-table-column
          prop="date"
          label="账单日期"
          min-width="150"
        />
        <el-table-column
          prop="order_amount"
          label="货款金额"
          min-width="150"
        />

        <el-table-column
          prop="postage_amount"
          label="邮费金额"
          min-width="150"
        />
        <el-table-column
          prop="refund_amount"
          label="退款金额"
          min-width="150"
        />
        <el-table-column
          prop="amount"
          label="可提现金额"
          min-width="150"
        />
        <el-table-column
          prop="invoice_amount"
          label="开票金额"
          min-width="150"
        />
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <router-link :to="{path: roterPre + '/accounts/billDetails/' + scope.row.mer_id}">
              <el-button type="text" size="small" >账单详情</el-button>
            </router-link>
            <el-button type="text" size="small" @click="exports('download',scope.row.mer_id,scope.row.date)">下载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block mb20">
        <el-pagination
          background
          :page-size="tableForm.limit"
          :current-page="tableForm.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

  </div>
</template>

<script>

import { merchantBillList,exportInancialRecordApi,exportmerDetailExportApi } from '@/api/accounts'
import { merSelectApi } from "@/api/product";
import { roterPre } from '@/settings'
import createWorkBook from '@/utils/newToExcel.js'
export default {
  name: 'MerchantBill',
  data() {
    return {
      loading: false,
      roterPre: roterPre,
      timeVal: [],
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableForm: {
        page: 1,
        limit: 10,
        mer_id: '',
        date:''
      },
      merSelect: [],
      ruleForm: {
        status: '0'
      },
      dialogVisible: false,
      rules: {
        status: [
          { required: true, message: '请选择对账状态', trigger: 'change' }
        ]
      },
      reconciliationId: 0,
      accountDetails: {
        date: '',
        charge: {},
        expend: {},
        income: {}
      }
    }
  },
  computed: {},
  mounted() { 
    this.getMerSelect()
    this.getList('')
  },
  methods: {
    // 商户列表；
    getMerSelect() {
      merSelectApi()
        .then((res) => {
          this.merSelect = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    async exports(type,id,date) {
      let params = {
        mer_id: id,
        page:1,
        limit:500,
        date
      }
      const excelData = JSON.parse(JSON.stringify(this.tableForm)); let data = []
      excelData.page = 1
      excelData.limit = 200
      // excelData.ids = this.checkedIds.toString()
      let pageCount = 1
      let lebData = {}
      for (let i = 0; i < pageCount; i++) {
        lebData = type=='download'?await this.exportmerDetailExportApi(params):await this.downOrderData(excelData)
        pageCount = Math.ceil(lebData.count / excelData.limit)
        if (lebData.export.length) {
          data = data.concat(lebData.export)
          excelData.page++
        }
      }
      createWorkBook(lebData.header, lebData.title, data, lebData.foot, lebData.filename)
      return
    },
    /** 订单 */
    downOrderData(excelData) {
      return new Promise((resolve, reject) => {
        exportInancialRecordApi(excelData).then((res) => {
          return resolve(res.data)
        })
      })
    },
    /** 下载订单 */
    exportmerDetailExportApi(excelData) {
      return new Promise((resolve, reject) => {
        exportmerDetailExportApi(excelData).then((res) => {
          return resolve(res.data)
        })
      })
    },
    // 列表
    getList(num) {
      this.listLoading = true
      this.tableForm.page = num ? num : this.tableForm.page;
      merchantBillList(this.tableForm).then(res => {
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
        this.listLoading = false
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },
    pageChange(page) {
      this.tableForm.page = page
      this.getList('')
    },
    handleSizeChange(val) {
      this.tableForm.limit = val
      this.chkName = ''
      this.getList('')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
