<template>
    <div>
        <el-card class="el-card">
            <el-form
                :inline="true"
                :model="queryParams"
                size="mini"
            >

                <el-form-item class="query-form-item" label="类型">
                    <el-select
                        v-model="queryParams.type"
                        placeholder="类型"
                    >
                        <el-option
                            v-for="item in typeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
            <span style="color: #000; margin-left: 10px; margin-right: 6px"
            >创建日期:</span
            >
                    <el-date-picker
                        :picker-options="pickerOptions"
                        v-model="dateRange"
                        type="daterange"
                        format="yyyy 年 MM 月 dd 日"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                        <el-button
                            type="primary"
                            icon="el-icon-refresh"
                            @click="onReset"
                        ></el-button>
                        <el-button type="primary" icon="search" @click="onSubmit"
                        >查询
                        </el-button
                        >

                    </el-button-group>
                </el-form-item>

            </el-form>
            <el-table
                v-loading="loading"
                :data="list"
                style="width: 100%"

            >
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="类型" prop="type">
                    <template slot-scope="scope">
                        <span>{{ getType(scope.row.type) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="日期" prop="downloadDate"/>
                <el-table-column label="提示行数" prop="totalCount"/>
                <el-table-column label="实际行数" prop="realCount"/>
                <el-table-column label="插入行数" prop="insertCount"/>

                <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true"/>
                <el-table-column label="操作" fixed="right" width="130">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.s.stop="downloadData(scope.row.downloadDate)">
                            重新下载
                        </el-button>
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="showContent(scope.row)"
                        >详情
                        </el-button>
                    </template>
                </el-table-column>

            </el-table>
            <el-pagination
                :page-size="queryParams.limit"
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="total"
            ></el-pagination>
        </el-card>
        <el-dialog
            title="提示"
            :visible.sync="dialog.visible"
            width="70%">
            <span>类型：</span><span>{{dialog.type}}</span>
            <el-divider></el-divider>
            <span>消息：</span><span>{{dialog.msg}}</span>
            <el-divider></el-divider>
            <span>详情：</span><span>{{ dialog.content }}</span>
        </el-dialog>

    </div>
</template>

<script>
import {logList,downloadByDay} from "@/api/UDIDownload/jobLog"

export default {
    name: "jobLog",
    data() {
        return {
            dateRange: [],
            typeOptions: [{value: "", label: "全部"}, {value: "error", label: "错误信息"}, {
                value: "warning",
                label: "警告信息"
            }, {value: "info", label: "正常信息"}],
            queryParams: {
                page: 1,
                limit: 10,
                type: ""
            },
            loading: false,
            list: [],
            total: 0,
            dialog: {
                visible: false,
                type:"",
                msg:"",
                content: ""
            },
            pickerOptions: {
                shortcuts: [
                    {
                        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]);
                        },
                    },
                ],
            },
        }
    },
    created() {
        this.getList();
    },
    methods: {
        getList() {
            logList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
                this.loading = false;
                this.list = res.data.list || [];
                this.total = res.data.total || 0;
            })
        },
        handleCurrentChange(val) {
            this.queryParams.page = val;
            this.getList();
        },
        onReset() {
            this.$router.push({
                path: "",
            });
            this.queryParams = {
                page: 1,
                limit: 10,
                type: ""
            };
            this.dateRange = [];
            this.getList();
        },
        onSubmit() {
            this.getList();
        },
        getType(type) {
            for (let i = 0; i < this.typeOptions.length; i++) {
                if (this.typeOptions[i].value == type)
                    return this.typeOptions[i].label;
            }
            return "未知";
        },
        showContent(row) {
            console.log(JSON.stringify(row));
            this.dialog.type = row.type;
            this.dialog.msg = row.msg;
            this.dialog.content = row.content;
            this.dialog.visible = true;
        },
        downloadData(fdate) {
            if(fdate == null || fdate == "")
            {
                this.$message.error("日期为空，无法下载！");
                return;
            }
           let _this=this;
            this.$confirm("是否重新下载日期:"+fdate+"的数据?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    downloadByDay(fdate).then(res=>{
                        let msg='已发送下载指令，请稍等查看日志,防止同时下载！';
                        //_this.$message.info(msg);
                        _this.$message({
                            type: 'info',
                            message:msg
                        });
                    });

                })
                .catch(() => {
                    _this.$message({
                        type: 'error',
                        message:"发生错误！"
                    });
                });
        },

        // 添加日期范围
        addDateRange(params, dateRange) {
            let search = params;
            search.startDate = null
            search.endDate = null;
            if (null != dateRange && '' != dateRange) {
                search.startDate = dateRange[0];
                search.endDate = dateRange[1];
            }
            return search;
        }
    }
}
</script>

<style scoped>

</style>
