<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"
                        :clearable="false"
                    ></el-date-picker>
                </div>
                <div class="type">
                    <span>平台：</span>
                    <el-select v-model="site" placeholder="请选择" @change="filterSite">
                        <el-option
                            v-for="item in siteList"
                            :key="item.siteName"
                            :label="item.plateformAndSite"
                            :value="item.siteName"
                        ></el-option>
                    </el-select>
                    <span>账号：</span>
                    <el-select v-model="storeName" placeholder="请选择" @change="filterStore">
                        <el-option
                            v-for="item in storeNameList"
                            :key="item.storeId"
                            :label="item.sunrateBankCardNo === null? item.storeName : item.storeName +item.sunrateBankCardNo.substring(0,4) + '********'+ item.sunrateBankCardNo.substring(item.sunrateBankCardNo.length-4)"
                            :value="item.storeId"
                        ></el-option>
                    </el-select>
                </div>
                <div class="download">
                    <span>下载格式：</span>
                    <el-select v-model="downloadValue" placeholder="请选择">
                        <el-option
                            v-for="item in downloadList"
                            :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="storePlateformName" label="平台"></el-table-column>
                        <el-table-column :key="Math.random()" label="账号">
                            <template slot-scope="scope">
                                {{scope.row.storeName}}
                                <br v-if="scope.row.acctNo !== ''" />
                                {{ scope.row.acctNo === ""?"":scope.row.acctNo.substring(0,4) + '********'+ scope.row.acctNo.substring(scope.row.acctNo.length-4) }}
                            </template>
                        </el-table-column>
                        <el-table-column :key="Math.random()" prop="fileTypeDesc" 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_showTradeInit,
    api_showStoreListByCondition,
    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_FUND_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;
                }
            );
            let showTradeInitData = {
                params: {
                    tradeType: "TRADE_IN"
                }
            };
            api_showTradeInit(showTradeInitData).then(res => {
                if (res.success) {
                    if (res.data.siteList == null) {
                        this.loading = false;
                        return false;
                    }
                    this.siteList = [
                        {
                            plateformAndSite: "全部",
                            siteName: ""
                        }
                    ];
                    this.siteList.push.apply(this.siteList, res.data.siteList);
                    this.loading = false;
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        filterStore(val) {
            if (val != "") {
                this.storeNameList.filter(item => {
                    if (item.storeId == val) {
                        this.acctNo = item.sunrateBankCardNo;
                        this.storeNameDesc = item.storeName;
                    }
                });
            } else {
                this.acctNo = "";
                this.storeNameDesc = "全部";
            }
        },
        filterSite(val) {
            this.siteList.filter(item => {
                if (val == item.siteName) {
                    this.storePlateform = item.storePlateform;
                }
            });

            if (val == "") {
                this.storeNameList = [
                    {
                        storeName: "全部",
                        sunrateBankCardNo: null,
                        storeId: ""
                    }
                ];
                this.storeName = "";
                this.acctNo = "";
                this.storeNameDesc = "全部";
            } else {
                let showStoreListByConditionData = {
                    params: {
                        storePlateform: this.storePlateform,
                        storeTheSite: this.site
                    }
                };
                api_showStoreListByCondition(showStoreListByConditionData).then(
                    res => {
                        if (res.success) {
                            this.storeNameList = [
                                {
                                    storeName: "全部",
                                    sunrateBankCardNo: null,
                                    storeId: ""
                                }
                            ];
                            this.storeNameList.push.apply(
                                this.storeNameList,
                                res.data.filter(item => {
                                    return item.sunrateBankCardNo !== null;
                                })
                            );
                        } else {
                            this.$message.error(res.msg);
                        }
                    }
                );
            }
        },
        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];
        },
        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_FUND_RECORD",
                        fileType: this.downloadValue,
                        storePlateform: this.storePlateform,
                        kycStoreId: this.storeName,
                        acctNo: this.acctNo,
                        storeName: this.storeNameDesc
                    }
                };
                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.success("刷新成功！");
            });
        },
        downloadRecord(val) {
            let downloadGenerateRecordParams = {
                params: {
                    generateFileType: "GENERATE_FUND_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]);
                        }
                    }
                ]
            },
            time: "",
            siteList: [
                {
                    plateformAndSite: "全部",
                    siteName: ""
                }
            ],
            site: "",
            storeNameList: [
                {
                    storeName: "全部",
                    sunrateBankCardNo: null,
                    storeId: ""
                }
            ],
            storeName: "",
            downloadList: [
                {
                    value: "FILE_EXCEL",
                    label: "excel"
                },
                {
                    value: "FILE_PDF",
                    label: "pdf"
                }
            ],
            downloadValue: "FILE_EXCEL",
            acctNo: "",
            flag: true,
            storeNameDesc: "全部",
            recordListData: []
        };
    },
    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;
    margin-right: 20px;
}
.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 {
    padding: 20px 20px 0;
    text-align: left;
    span {
        display: inline-block;
        width: 76px;
        text-align: right;
        font-size: 14px;
    }
}
.time,
.type,
.created-record,
.download {
    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>
