<template>
    <div id="loan-record">
        <br>


        <div class="top">
            <el-form :inline="true" :model="queryModel" class="demo-form-inline">

                <el-select v-model="code" placeholder="导出编号">
                    <el-option v-for="item in codes" :label="item.name" :value="item.value"></el-option>
                </el-select>


                <el-date-picker v-if="code!='7001'" v-model="searchDate" type="date"
                                placeholder="选择日期"></el-date-picker>

                <el-date-picker v-if="code=='7001'" v-model="searchDate" type="daterange" range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"></el-date-picker>


                <div class="btn">
                    <el-button v-if="$permissionUtils.btnPermission('admin')" @click="download" type="primary">导出
                    </el-button>
                </div>
            </el-form>

            <div>
                <div class="system-list">
                    <el-table :data="records" stripe style="width: 95%">
                        <el-table-column type="index" width="60"></el-table-column>
                        <el-table-column prop="code" sortable label="编号"></el-table-column>
                        <el-table-column prop="name" sortable label="文件名字"></el-table-column>
                        <el-table-column prop="use" sortable label="适用"></el-table-column>
                    </el-table>
                </div>
            </div>
        </div>


    </div>

</template>


<script type="text/ecmascript-6">

    import * as moment from "moment";


    export default {
        name: "DownloadExecl",
        data() {
            return {
                fileUrl: '',
                searchDate: '',
                request: '',
                code: '',
                codes: [
                    {name: '1001', value: '1001'},
                    {name: '1002', value: '1002'},
                    {name: '2001', value: '2001'},
                    {name: '2002', value: '2002'},
                    {name: '3001', value: '3001'},
                    {name: '3002', value: '3002'},
                    {name: '3003', value: '3003'},
                    {name: '4001', value: '4001'},
                    {name: '4002', value: '4002'},
                    {name: '4003', value: '4003'},
                    {name: '5001', value: '5001'},
                    {name: '5002', value: '5002'},
                    {name: '5003', value: '5003'},
                    {name: '5004', value: '5004'},
                    {name: '6001', value: '6001'},
                    {name: '7001', value: '7001'},
                ],
                queryModel: {},
                records: [
                    {code: "1001", name: "新增借条", use: ""},
                    {code: "1002", name: "放款记录", use: ""},
                    {code: "2001", name: "每日到期", use: ""},
                    {code: "2002", name: "银行每日回款", use: ""},
                    {code: "3001", name: "提醒还款", use: ""},
                    {code: "3002", name: "逾期还款", use: ""},
                    {code: "3003", name: "逾期减免结清", use: ""},
                    {code: "4001", name: "新增逾期", use: ""},
                    {code: "4002", name: "每日移交天思数据", use: ""},
                    {code: "4003", name: "每日实际逾期数据", use: ""},
                    {code: "5001", name: "天女每日新增", use: ""},
                    {code: "5002", name: "天女代付", use: ""},
                    {code: "5003", name: "天女每日到期", use: ""},
                    {code: "5004", name: "续贷走代付数据", use: ""},
                    {code: "6001", name: "每日断约客户", use: ""},
                    {code: "7001", name: "每日夜间客户", use: ""},
                ],
            }
        },


        methods: {


            download() {


                if (this.code == '') {
                    this.$message.error("编码不能为空");
                    return
                }

                if (this.searchDate == '') {
                    this.$message.error("日期不能为空");
                    return
                }

                if (this.code == '7001') {
                    var url = `/file/downloadFile/` + this.code + "/"
                    var date = this.getDate(this.searchDate,0)+","+this.getDate(this.searchDate,1)
                    url = url + date
                    this.$$get(url)
                        .then(res => {
                            if (res.data.code < 1) {
                                throw res.data.msg
                            }
                            if (res.data.code == '1') {
                                this.fileUrl = res.data.data
                                let link = document.createElement('a')
                                link.style.display = 'none'
                                link.href = this.fileUrl
                                document.body.appendChild(link)
                                link.click()
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })
                        .catch(err => this.$$msg.err(err))
                } else {

                    this.$$get(`/file/downloadFile/` + this.code + "/" + this.$date.dateToWebFormat(this.searchDate))
                        .then(res => {
                            if (res.data.code < 1) {
                                throw res.data.msg
                            }
                            if (res.data.code == '1') {
                                this.fileUrl = res.data.data
                                let link = document.createElement('a')
                                link.style.display = 'none'
                                link.href = this.fileUrl
                                document.body.appendChild(link)
                                link.click()
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })
                        .catch(err => this.$$msg.err(err))
                }
            },


            dateFormat: function (param) {
                var loanBean = row['loanBean'];
                var date = loanBean.startDate;
                if (date == undefined) {
                    return "";
                }
                return moment(date).format("YYYY-MM-DD");
            },
            getDate(param, num) {
                if (param && param[num] !== '') {
                    return moment(param[num]).format("YYYY-MM-DD HH:mm:ss")
                }
                return ""
            },
        }
    }
</script>

