<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form label-width="77px" class="ele-form-search">
        <el-row :gutter="15">
          <el-col :md="6" :sm="12">
            <el-form-item label="姓名:">
              <el-input
                placeholder="请输入姓名"
                clearable
                v-model="where.name"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <el-form-item label="手机号:">
              <el-input
                placeholder="请输入手机号"
                clearable
                v-model="where.mobile"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <el-form-item label="状态:">
              <el-select v-model="where.status" clearable>
                <el-option label="已发放" value="2"></el-option>
                <el-option label="待发放" value="1"></el-option>
                <el-option label="发放失败" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12">
            <el-form-item label="筛选日期:">
              <el-date-picker
                unlink-panels
                v-model="where.dateRange"
                range-separator="-"
                type="daterange"
                end-placeholder="结束日期"
                start-placeholder="开始日期"
                :picker-options="pickerOptions"
                value-format="yyyy-MM-dd"
                class="ele-fluid"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
              >
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="receiverCustomerTables"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button type="primary" size="small" @click="exportBas">
            导出
          </el-button>
        </template>
        <template v-slot:userName="{ row }">
          <el-link type="primary" :underline="false" @click="showDetail(row)">
            <div>{{ row.name }}</div>
            <div>{{ row.mobile }}</div>
          </el-link>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link type="primary" :underline="false" @click="editStatus(row)">
            修改状态
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <edit :data="current" :visible.sync="showEdit" @done="reload" />
    <edit-status
      :data="current"
      :visible.sync="showEditStatus"
      @done="reload"
    />
  </div>
</template>

<script>
  import { sendLog } from '@/api/receiver/subsidy';
  import edit from './components/edit.vue';
  import editStatus from './components/editStatus.vue';
  import { utils, writeFile } from 'xlsx';

  export default {
    name: 'receiverSendLog',
    components: { edit, editStatus },
    data() {
      return {
        where: {
          name: '',
          mobile: '',
          status: '',
          dateRange: []
        },
        // 表格列配置
        columns: [
          {
            prop: 'userName',
            label: '姓名手机',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80,
            slot: 'userName'
          },
          {
            prop: 'price',
            label: '发放金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'checkTime',
            label: '审核通过时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'statusCn',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            resizable: false,
            slot: 'action'
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 日期时间选择器快捷项
        pickerOptions: {
          shortcuts: [
            {
              text: '今天',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '昨天',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24);
                end.setTime(end.getTime() - 3600 * 1000 * 24);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一周',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit('pick', [start, end]);
              }
            }
          ]
        },
        reasonVisible: false,
        reason: '',
        showEditStatus: false
      };
    },
    created() {},
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return sendLog({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload({ page: 1, where: this.where });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
      },
      editStatus(row) {
        this.current = row;
        this.showEditStatus = true;
      },
      showDetail(row) {
        this.current = row;
        this.showEdit = true;
      },
      exportBas() {
        const array = [['姓名', '手机', '身份证号', '银行卡号', '开户行名称']];
        sendLog({ page: 1, limit: 5000, where: this.where }).then((data) => {
          if (data.list.length > 0) {
            data.list.forEach((d) => {
              array.push([
                d.name,
                d.mobile,
                d.cardId,
                d.bankCardNum,
                d.bankName
              ]);
            });
          }
          const sheetName = 'Sheet1';
          const workbook = {
            SheetNames: [sheetName],
            Sheets: {}
          };
          const sheet = utils.aoa_to_sheet(array);
          workbook.Sheets[sheetName] = sheet;
          // 设置列宽
          sheet['!cols'] = [
            { wch: 10 },
            { wch: 10 },
            { wch: 10 },
            { wch: 10 },
            { wch: 20 },
            { wch: 40 },
            { wch: 10 }
          ];
          writeFile(workbook, '补贴发放明细.xlsx');
        });
      }
    }
  };
</script>
<style scoped></style>
