<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-item>
                    <el-button @click="dialog2.visible = true">按时间段下载</el-button>
                </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">
                    <template slot-scope="scope">
                        <span>{{ getType(scope.row.type) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="下载类型" prop="downloadType">
                    <template slot-scope="scope">
                        <span>{{ getDownloadType(scope.row.downloadType) }}</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
                :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>
        <el-dialog title="重新下载"
                   :visible.sync="dialog2.visible"
                   width="650px">
            <el-form label-width="60px">
                <el-form-item>
            <span style="color: #000; margin-left: 10px; margin-right: 6px"
            >日期:</span>
                    <el-date-picker
                        v-model="dialog2.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>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="downloadDatas">确 定</el-button>
                <el-button @click="dialog2.visible=false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

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

export default {
    name: "jobLog",
    data() {
        return {
            dateRange: [],
            typeOptions: [{value: "", label: "全部"}, {value: "error", label: "错误信息"}, {
                value: "warning",
                label: "警告信息"
            }, {value: "info", label: "正常信息"}],
            downloadTypeOptions:[{value:"",label:"全部"},{value:"auto",label:"自动"},{value:"manual",label:"手动"}],
            queryParams: {
                page: 1,
                limit: 10,
                type: "",
                downloadType:''
            },
            loading: false,
            list: [],
            total: 0,
            dialog: {
                visible: false,
                type:"",
                msg:"",
                content: ""
            },
            dialog2:{
                visible: false,
                dateRange: [],
            },
            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();
        },
        getType(type) {
            return  this.selectDictLabel(this.typeOptions,type);
           /* for (let i = 0; i < this.typeOptions.length; i++) {
                if (this.typeOptions[i].value == type)
                    return this.typeOptions[i].label;
            }
            return "未知";*/
        },
        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(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:"发生错误！"
                    });
                });
        },
        downloadDatas(){
            let query=this.addDateRange({},this.dialog2.dateRange);
            downloadByDays(query);
            this.$message.info("已发送下载指令，请稍等查看日志,防止同时下载！");
            this.dialog2.visible = false;
        },
        rowStyle({row,rowIndex}){
            let rowBackground={};
            if(row.type == 'error'){
                //rowBackground.background="#f6f5ee";
                rowBackground.color='#f60303';
            }

            return rowBackground;
        }
    }
}
</script>

<style scoped>

</style>
