<template>
    <main class="wrap">
        <div class="layout-box">
            <div class="center-title clearfix">
                <h3>下载交易记录</h3>
                <a href="javascript:;" class="back-btn" @click="goBack">返回</a>
            </div>
            <div class="filter-box">
                <div class="time">
                    <span>时间：</span>
                    <el-date-picker
                        v-model="time"
                        type="daterange"
                        align="right"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :picker-options="pickerOptions"
                        @change="changeTime"
                        :clearable="false"
                    ></el-date-picker>
                </div>
                <div class="type">
                    <span>类型：</span>
                    <el-select v-model="typeValue" placeholder="请选择" @change="changeType">
                        <el-option
                            v-for="item in typeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </div>
                <div class="created-record">
                    <span></span>
                    <a href="javascript:;" class="btn" @click="createRecord">生成交易记录</a>
                </div>
            </div>
            <div class="record-list">
                <div class="center-title clearfix">
                    <h3>已生成交易记录</h3>
                    <span class="cl-red">*最多保存5份交易记录</span>
                    <a href="javascript:;" class="back-btn" @click="refresh">刷新</a>
                </div>
                <div class="record-list-table" v-loading="loading">
                    <el-table :data="recordListData" style="width: 100%" empty-text="很遗憾，暂无数据！">
                        <el-table-column :key="Math.random()" label="时间范围">
                            <template
                                slot-scope="scope"
                            >{{ scope.row.generateStartDate.substring(0,10) + " ~ " + scope.row.generateEndDate.substring(0,10) }}</template>
                        </el-table-column>
                        <el-table-column :key="Math.random()" prop="createDts" label="生成时间"></el-table-column>
                        <el-table-column :key="Math.random()" prop="tradeTypeDesc" label="类型"></el-table-column>
                        <el-table-column
                            :key="Math.random()"
                            prop="authStatus"
                            label="操作"
                            width="100px"
                        >
                            <template slot-scope="scope">
                                <el-button
                                    @click="downloadRecord(scope.row)"
                                    type="text"
                                    size="small"
                                    class="cl-blue"
                                >下载</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </div>
    </main>
</template>

<script>
import {
    api_generateFileRecordList,
    api_generateTradeRecord,
    api_downloadGenerateRecord
} from "api/portal.js";
import utils from "utils/utils.js";
export default {
    name: "dealDetail",
    computed: {},
    components: {},
    methods: {
        init(callbackFn) {
            this.loading = true;
            let generateFileRecordListParams = {
                params: {
                    generateFileType: "GENERATE_TRADE_RECORD"
                }
            };
            api_generateFileRecordList(generateFileRecordListParams).then(
                res => {
                    if (res.success) {
                        if (res.data == null) {
                            this.recordListData = [];
                        } else {
                            this.recordListData = res.data;
                        }
                        if (callbackFn) {
                            callbackFn();
                        }
                    } else {
                        this.$message.error(res.msg);
                    }
                    this.loading = false;
                }
            );
        },
        goBack() {
            this.$router.go(-1);
        },
        defaultTime() {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            this.time = [start, end];
        },
        changeTime(val) {},
        changeType(val) {},
        createRecord() {
            if (this.flag) {
                this.flag = false;
                let generateTradeRecordData = {
                    data: {
                        tradeStartDate: utils.formatDate(
                            this.time[0],
                            "yyyy-MM-dd"
                        ),
                        tradeEndDate: utils.formatDate(
                            this.time[1],
                            "yyyy-MM-dd"
                        ),
                        tradeType: this.typeValue,
                        generateFileType: "GENERATE_TRADE_RECORD"
                    }
                };
                api_generateTradeRecord(generateTradeRecordData).then(res => {
                    if (res.success) {
                        this.init();
                    } else {
                        this.$message.error(res.msg);
                    }
                });
                setTimeout(() => {
                    this.flag = true;
                }, 5000);
            } else {
                this.$message.error("请勿频繁点击！");
            }
        },
        refresh() {
            this.init(() => {
                this.$message({
                    message: "刷新成功！",
                    type: "success"
                });
            });
        },
        downloadRecord(val) {
            let downloadGenerateRecordParams = {
                params: {
                    generateFileType: "GENERATE_TRADE_RECORD",
                    id: val.id
                }
            };
            api_downloadGenerateRecord(downloadGenerateRecordParams).then(
                res => {
                    const blob = new Blob([res]);
                    const fileName = val.fileShowName;
                    let status;
                    var reader = new FileReader();
                    reader.onload = e => {
                        let str = e.target.result.substring(
                            e.target.result.indexOf("success") + 9
                        );
                        status = str.substring(0, 4);
                        if (status == "true") {
                            if ("download" in document.createElement("a")) {
                                // 非IE下载
                                const elink = document.createElement("a");
                                elink.download = fileName;
                                elink.style.display = "none";
                                elink.href = URL.createObjectURL(blob);
                                document.body.appendChild(elink);
                                elink.click();
                                URL.revokeObjectURL(elink.href); // 释放URL 对象
                                document.body.removeChild(elink);
                            } else {
                                // IE10+下载
                                navigator.msSaveBlob(blob, fileName);
                            }
                        } else {
                            this.$message.error("此文件无记录！");
                        }
                    };
                    reader.readAsText(blob);
                }
            );
        }
    },
    data() {
        return {
            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 * 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]);
                        }
                    }
                ],
                disabledDate: time => {
                    return time.getTime() > new Date().getTime();
                },
                onPick: time => {
                    if (time.maxDate !== null) {
                        if (
                            time.maxDate.getTime() - 3600 * 1000 * 24 * 90 >
                            time.minDate.getTime()
                        ) {
                            let newDate = time.minDate;
                            setTimeout(() => {
                                this.time = [
                                    time.minDate,
                                    new Date().setTime(
                                        newDate.getTime() +
                                            3600 * 1000 * 24 * 90
                                    )
                                ];
                            }, 30);
                            this.$message.error("时间选择区间不可大于90天！");
                        }
                    }
                }
            },
            time: "",
            typeList: [
                {
                    value: "TRADE_WITHDRAW",
                    label: "提现"
                }
            ],
            typeValue: "TRADE_WITHDRAW",
            recordListData: [],
            loading: false,
            flag: true
        };
    },
    created() {
        this.init();
        this.defaultTime();
    }
};
</script>

<style lang="less" scoped>
@import "../../assets/styles/common.css";
.layout-box {
    min-height: calc(100vh - 264px);
}
.el-select,
.el-date-editor {
    width: 330px;
}
.layout-box {
    padding-bottom: 20px;
    background: #fff;
}
.center-title {
    position: relative;
}
.back-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    padding-left: 22px;
    font-size: 14px;
    background: url(~assets/images/icon-center-back.png) left center no-repeat;
    color: #3d86e9;
}
.filter-box {
    overflow: hidden;
    padding: 20px 20px 0;
    text-align: left;
    span {
        display: inline-block;
        width: 56px;
        font-size: 14px;
    }
}
.time,
.type,
.created-record {
    margin-bottom: 20px;
}
.btn {
    display: inline-block;
    width: 330px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 4px;
    background: #3d86e9;
    color: #ffffff;
    cursor: pointer;
}
.record-list {
    .center-title {
        border-top: 1px solid #eaeaea;
        border-bottom: none;
        span {
            font-size: 12px;
            margin-left: 10px;
        }
    }
    .record-list-table {
        padding: 0 20px;
    }
    .el-table::before {
        content: "";
        height: 0;
    }
    & /deep/ th {
        padding: 0;
        height: 36px;
        text-align: center;
        border-bottom: 1px solid transparent;
        background: #f5f5f5;
    }
    & /deep/ td {
        text-align: center;
        border-bottom: 1px solid transparent;
    }
    & /deep/ .el-table__empty-text {
        width: initial;
        &::before {
            content: "";
            display: block;
            width: 191px;
            height: 131px;
            margin: 40px 0 0;
            background: url(~assets/images/icon-status-no-data.png) no-repeat;
            background-size: 191px 131px;
        }
    }
    & /deep/ .el-button {
        width: 100%;
        span {
            display: block;
            width: 100%;
        }
    }
}
</style>
