<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="downloadAll">下载全部</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="tbName">
                    <template slot-scope="scope">
                        <span>{{getTbName(scope.row.tbName)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="日期" prop="actionDate"/>
                <el-table-column label="执行行数" prop="totalCount"/>

                <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
                            v-if="scope.row.downloadType != 'import'"
                            type="text"
                            size="small"
                            @click.native.s.stop="downloadData(scope.row.tbName)">
                            重新下载
                        </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>
    </div>
</template>

<script>
import {GetList,UdplatSync} from "@/api/udplat/udplatLog";
import myMinix from './minixs/commMinixs';

export default {
    name: "udplatLog",
    mixins: [myMinix],
    data() {
        return {
            typeOptions: [{value: "", label: "全部"}, {value: "error", label: "错误信息"}, {value: "warning",label: "警告信息"}, {value: "info", label: "正常信息"}],
            downloadTypeOptions: [{value: "", label: "全部"}, {value: "auto", label: "自动"}, {value: "manual",label: "手动"},{value: "import",label: "导入"}],
            tbNameOption:[{value:"udplat_distributor",label:"配送企业"},{value:"udplat_goods",label:"产品目录"},{value:"udplat_hospital",label:"医疗机构"},{value:"udplat_manufacture",label:"生产企业"}],
            queryParams: {
                page: 1,
                limit: 10,
                type: "",
                downloadType: ''
            },
            dialog: {
                visible: false,
                type:"",
                msg:"",
                content: ""
            },
        }
    },
    methods:{
        getList() {
            this.loading = true;
            GetList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
                this.loading = false;
                this.list = res.data.list || [];
                this.total = res.data.total || 0;
            }).catch(error => {
                this.$message.error(error);
                this.loading = false;
            })
        },
        getType(type) {
            return  this.selectDictLabel(this.typeOptions,type);
        },
        getDownloadType(type){
            return this.selectDictLabel(this.downloadTypeOptions,type);
        },
        rowStyle({row,rowIndex}){
            let rowBackground={};
            if(row.type == 'error'){
                //rowBackground.background="#f6f5ee";
                rowBackground.color='#f60303';
            }

            return rowBackground;
        },
        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(tbName){
            let _this=this;
            this.$confirm("是否重新下载当前表:"+tbName+"的数据?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    UdplatSync(tbName).then(res=>{
                        console.log(res);
                        _this.$message({
                            type: 'info',
                            message:res.message
                        });
                    });

                })
                .catch(() => {
                    _this.$message({
                        type: 'error',
                        message:"发生错误！"
                    });
                });
        },
        getTbName(tbName){
            return  this.selectDictLabel(this.tbNameOption,tbName);
        },
        downloadAll() {
            let _this=this;
            this.$confirm("是否重新下载全部的数据?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    UdplatSync("").then(res=>{
                        console.log(res);
                        _this.$message({
                            type: 'info',
                            message:res.message
                        });
                    });

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

<style scoped>

</style>
