<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 class="query-form-item">
                    <el-select
                        v-model="queryParams.downloadType"
                        placeholder="类型"
                    >
                        <el-option
                            v-for="item in downloadTypeOptions"
                            :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%"
                :row-style="rowStyle"
            >
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="类型" prop="type" width="100">
                    <template slot-scope="scope">

                        <el-tag
                            :type="statusFilterType(scope.row.type)"
                        >{{ getType(scope.row.type) }}
                        </el-tag
                        >


                    </template>
                </el-table-column>
                <el-table-column label="下载方式" prop="downloadType" width="80">
                    <template slot-scope="scope">
                        <span>{{ getDownloadType(scope.row.downloadType) }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="下载类型" prop="downloadType" width="80">
                    <template slot-scope="scope">
                        <span>{{ taskTypeMap[scope.row.taskType] }}</span>
                    </template>
                </el-table-column>
                <!--                <el-table-column label="状态" prop="status" width="80">-->
                <!--                    <template slot-scope="scope">-->
                <!--                        <span>{{ getStatus(scope.row.status) }}</span>-->
                <!--                    </template>-->
                <!--                </el-table-column>-->

                <el-table-column label="日期" prop="downloadDate" width="100"/>
                <el-table-column label="提示行数" prop="totalCount" width="80"/>
                <el-table-column label="实际行数" prop="realCount" width="80"/>
                <el-table-column label="插入行数" prop="insertCount" width="80"/>
                <el-table-column label="创建时间" align="center" prop="createTime" width="140"
                                 :show-overflow-tooltip="true"/>
                <el-table-column label="结果信息" align="center" prop="msg" :show-overflow-tooltip="true"/>
                <el-table-column label="操作" fixed="right" width="220">
                    <template slot-scope="scope">

                        <el-button
                            type="text"
                            size="small"
                            :disabled="scope.row.type=='info' || scope.row.type=='success'"
                            @click.native.s.stop="continueDownload(scope.row)">
                            继续下载
                        </el-button>

                        <el-button
                            type="text"
                            size="small"
                            @click.native.s.stop="downloadData(scope.row)">
                            重新下载
                        </el-button>
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="showContent(scope.row)"
                        >详情
                        </el-button>

                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="deleteClick(scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>

            </el-table>
            <el-pagination
                :current-page="queryParams.page"
                :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, downloadByDays, goOnDlDi, deleteLog} from "@/api/UDIDownload/jobLog"

export default {
    name: "jobLog",
    data() {
        return {
            dateRange: [],
            typeOptions: [
                {value: "", label: "全部"},
                {value: "error", label: "下载错误"},
                {value: "warning", label: "写入错误"},
                {value: "info", label: "下载中"},
                {value: "success", label: "下载成功"}],


            statusOptions: [
                {value: "1", label: "成功"},
                {value: "0", label: "失败"},
                {value: "", label: "未知"}
            ],
            downloadTypeOptions: [
                {value: "", label: "全部"},
                {value: "auto", label: "自动"},
                {value: "manual", label: "手动"}],
            queryParams: {
                page: 1,
                limit: 10,
                type: "",
                downloadType: ''
            },

            taskTypeMap: {
                "download": "数据下载",
                "update": "数据更新",
            },
            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.queryParams.page = 1;
            this.getList();
        },

        getStatus(type) {
            return this.selectDictLabel(this.statusOptions, type);
        },

        getType(type) {
            return this.selectDictLabel(this.typeOptions, type);
        },
        getDownloadType(type) {
            return this.selectDictLabel(this.downloadTypeOptions, type);
        },
        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(row) {
            if (row.downloadDate == null || row.downloadDate == "") {
                this.$message.error("日期为空，无法下载！");
                return;
            }
            let _this = this;
            this.$confirm("是否重新下载日期:" + row.downloadDate + "的数据?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    downloadByDay(row.downloadDate, row.id).then(res => {
                        let msg = '已发送下载指令，请稍等查看日志,防止同时下载！';
                        _this.$message({
                            type: 'info',
                            message: msg
                        });
                    });

                })
                .catch(() => {
                    _this.$message({
                        type: 'error',
                        message: "发生错误！"
                    });
                });
        },
        deleteClick(row) {
            let _this = this;
            let dlQuery = {jobId: row.id};
            this.$confirm("是否删除此次任务记录", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    deleteLog(dlQuery).then(res => {
                        if (res.code == 20000) {
                            this.$message.success(res.data);
                        }
                        this.getList();
                    });

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

        continueDownload(row) {
            let _this = this;
            let dlQuery = {jobId: row.id};
            this.$confirm("是否从上次下载位置继续下载", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    goOnDlDi(dlQuery).then(res => {
                        let msg = '已发送下载指令，请稍等查看日志,防止同时下载！';
                        _this.$message({
                            type: 'info',
                            message: msg
                        });
                    });

                })
                .catch(() => {
                    _this.$message({
                        type: 'error',
                        message: "发生错误！"
                    });
                });
        },
        rowStyle({row, rowIndex}) {
            let rowBackground = {};
            if (row.type == 'error') {
                //rowBackground.background="#f6f5ee";
                rowBackground.color = '#f60303';
            }

            return rowBackground;
        },
        statusFilterType(status) {
            const statusMap = {
                'info': "info",
                'warning': "warning",
                'error': "danger",
                'success': "success",
            };
            return statusMap[status];
        },
    }
}
</script>

<style scoped>

</style>
