<!--
 * @Title: 
 * @Description: 巡检遗留问题列表
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<template>
    <div>
        <el-row>
            <el-col :span="24">
                <basic-container>
                    <avue-crud :option="option" :table-loading="loading" :page.sync="page" :data="data" ref="crud"
                        v-model="form" :permission="permissionList" @row-del="rowDel" @row-update="rowUpdate"
                        @row-save="rowSave" @search-change="searchChange" @search-reset="searchReset"
                        @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
                        @refresh-change="refreshChange" @on-load="onLoad">
                        <template slot-scope="scope" slot="menuLeft">
                            <el-button type="primary" icon="el-icon-download" size="small"
                                @click="monthRemainingProblemsExportHandle">导出当前汛期巡检遗留问题表</el-button>
                            <el-button type="primary" icon="el-icon-download" size="small"
                                @click="uncloseRemainingProblemsExportHandle">导出当前汛期未关闭巡检遗留问题表</el-button>
                        </template>
                        <template slot-scope="{ row, index }" slot="menu">
                            <el-button type="text" size="small" @click="openReportDetail(row)"
                                icon="el-icon-edit-outline">填报详情
                            </el-button>
                            <el-button v-if="permission.remainingproblems_close == 1 && row.remainQuestionState == 1"
                                type="text" size="small" style="color:lightcoral" @click="closeRemainingProblem(row)"
                                icon="el-icon-circle-close">关闭问题
                            </el-button>
                        </template>
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
        <el-drawer title="巡检填报信息" :visible.sync="reportDialog" v-if="reportDialog" :size="drawerSize" :direction="direction"
            :close-on-click-modal="false" append-to-body>
            <inspectionreportdetail :id="id" ref="inspectionreportdetail"></inspectionreportdetail>
        </el-drawer>
    </div>
</template>
<script>

import {
    getRemainingproblemsPage,
    closeRemainingProblem,
    monthRemainingProblemsExport,
    uncloseRemainingProblemsExport
} from "@/api/business/inspection/inspection.js";
import { mapGetters } from "vuex";
import inspectionreportdetail from "../inspectionreportdetail.vue";

export default {
    components: {
        inspectionreportdetail,
    },
    data() {
        return {
            id: '',
            drawerSize: '50%',
            reportDialog: false,
            form: {},
            selectionList: [],
            query: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            option: {
                labelWidth: 120,
                viewLabelWidth: 100,
                searchlabelWidth: 100,
                height: "auto",
                calcHeight: 80,
                align: "center",
                headerAlign: "center",
                tip: false,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                addBtn: false,
                border: true,
                index: true,
                editBtn: false,
                delBtn: false,
                viewBtn: false,
                menuWidth: 200,
                dialogType: "drawer",
                columnBtn: false,
                dialogClickModal: false,
                column: [
                    {
                        label: "测站编码",
                        prop: "rtuCode",
                        span: 12,
                        search: true,
                        searchSpan: 4,
                        width: 120,
                    },
                    {
                        label: "测站名称",
                        prop: "rtuName",
                        span: 12,
                        search: true,
                        searchSpan: 4,
                        width: 160,
                        // html: true,
                        // formatter: (val) => {
                        //     if (val.rtuName) {
                        //         return '<b>' + val.rtuName + '</b>/' + '<b style="color:grey">' + val.rtuCode + '</b>'
                        //     } else {
                        //         return '<b>' + val.rtuName + '</b>'
                        //     }
                        // }
                    },
                    {
                        label: "行政区",
                        prop: "adCity",
                        span: 12,
                        width: 160,
                        editDisplay: false,
                        addDisplay: false,
                        html: true,
                        formatter: (val) => {
                            if (val.adCity && val.adDist) {
                                return '<b>' + val.adCity + '</b>/' + '<b>' + val.adDist + '</b>'
                            } else {
                                return '<b>' + val.adCity + '</b>'
                            }
                        }
                    },
                    {
                        label: "巡检年度",
                        prop: "inspectionYear",
                        span: 24,
                        hide: true,
                        search: true,
                        searchSpan: 4,
                        type: "year",
                        rules: [{
                            required: true,
                        }],
                        format: 'yyyy年',
                        valueFormat: 'yyyy-MM-dd',
                    },
                    {
                        search: true,
                        label: "汛期",
                        prop: "rainSeasonKind",
                        type: 'select',
                        span: 24,
                        width: 160,
                        searchSpan: 4,
                        props: {
                            label: 'dictValue',
                            value: 'id'
                        },
                        dicData: [{
                            dictValue: '汛前巡检',
                            id: 1,
                        },
                        {
                            dictValue: '汛中（一）巡检',
                            id: 2,
                        },
                        {
                            dictValue: '汛中（二）巡检',
                            id: 3,
                        }],
                    },

                    // {
                    //     label: "测站类型",
                    //     prop: "rtuKindDesc",
                    //     span: 24,
                    //     width: 160,
                    // },
                    // {
                    //     label: "测站状态",
                    //     prop: "runStatusId",
                    //     span: 12,
                    //     slot: true,
                    //     width: 100,
                    //     html: true,
                    //     formatter: (val) => {
                    //         if (val.runStatusId == 0) {

                    //             return '<b style="color:blue">正常</b>'
                    //         } else {

                    //             return '<b style="color:orangered">疑似故障</b>'
                    //         }
                    //     }
                    // },
                    {
                        label: "填报总结说明",
                        prop: "reportDesc",
                        span: 12,
                        hide: true,
                    },
                    {
                        label: "填报人",
                        prop: "servicePersonName",
                        span: 12,
                        width: 120,
                    },
                    {
                        label: "填报时间",
                        prop: "createTime",
                        span: 12,
                        width: 140,
                    },
                    {
                        label: "遗留问题",
                        prop: "remainQuestion",
                        span: 24,
                        overHidden: true,
                        minWidth: 160,
                    },
                    {
                        label: "问题处理状态",
                        prop: "remainQuestionState",
                        span: 12,
                        slot: true,
                        width: 120,
                        html: true,
                        formatter: (val) => {
                            if (val.remainQuestionState == 1) {
                                return '<b style="color:red">待处理</b>'
                            } else if (val.remainQuestionState == 2) {
                                return '<b style="color:blue">已完成</b>'
                            }
                        }
                    },
                    {
                        label: "遗留问题处理意见",
                        prop: "remainQuestionResolution",
                        span: 24,
                        overHidden: true,
                        minWidth: 160,
                    },
                ],
            },
            data: [],
        };
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {
                remainingproblemsCloseBtn: this.vaildData(this.permission.remainingproblems_close, false),
            };
        },
    },
    created() {

    },
    watch: {
        // 'editForm.planId': {
        //     handler(val) {
        //         if (val != '' && val != undefined && val != null) {
        //             this.planId = val;
        //              this.$message.success(this.planId)
        //         }
        //     },
        //     immediate: true
        // }
    },
    methods: {
        toOss(path) {
            console.log("oss " + path);
            return this.website.ossServer + path;
        },
        closeRemainingProblem(item) {
            this.$prompt("请输入处理意见", "确认关闭此遗留问题？", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
            })
                .then(({ value }) => {
                    closeRemainingProblem({ id: item.id, remainQuestionResolution: value }).then((res) => {
                        if (res.data.code == 200) {
                            this.$message({
                                type: "success",
                                message: "操作成功",
                            });
                            this.refreshChange();
                        }
                    });
                })
                .catch(() => { });
        },
        monthRemainingProblemsExportHandle() {
            let that = this;
            this.$confirm("确认导出当前汛期遗留问题信息表？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    monthRemainingProblemsExport().then(
                        (res) => {
                            window.console.log(JSON.stringify(res));
                            that.exportDialog = false;
                            if (res.data.data.status == 1) {
                                this.$message({
                                    type: "success",
                                    message: "导出成功!",
                                });
                                var url = res.data.data.url;
                                var filename = res.data.data.filename;
                                that.getBlob(this.toOss(url)).then(res => {
                                    that.saveAs(res, filename + ".xlsx");
                                });
                            } else {
                                that.$message({
                                    type: "fail",
                                    message: "操作失败!",
                                });
                            }
                        },
                        (error) => {
                            that.exportDialog = false;
                            window.console.log(error);
                            this.$message({
                                type: "fail",
                                message: "操作失败!",
                            });
                        }
                    );
                })
                .catch(() => { });
        },
        uncloseRemainingProblemsExportHandle() {
            let that = this;
            this.$confirm("确认导出当前汛期未关闭的遗留问题信息表？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    uncloseRemainingProblemsExport().then(
                        (res) => {
                            window.console.log(JSON.stringify(res));
                            that.exportDialog = false;
                            if (res.data.data.status == 1) {
                                this.$message({
                                    type: "success",
                                    message: "导出成功!",
                                });
                                var url = res.data.data.url;
                                var filename = res.data.data.filename;
                                that.getBlob(this.toOss(url)).then(res => {
                                    that.saveAs(res, filename + ".xlsx");
                                });
                            } else {
                                that.$message({
                                    type: "fail",
                                    message: "操作失败!",
                                });
                            }
                        },
                        (error) => {
                            that.exportDialog = false;
                            window.console.log(error);
                            this.$message({
                                type: "fail",
                                message: "操作失败!",
                            });
                        }
                    );
                })
                .catch(() => { });
        },
        getBlob(url) {
            return new Promise(resolve => {
                const xhr = new XMLHttpRequest();
                xhr.open("GET", url, true);
                xhr.responseType = "blob";
                xhr.crossOrigin = "*";
                xhr.onload = () => {
                    if (xhr.status === 200) {
                        resolve(xhr.response);
                    }
                };
                xhr.send();
            })
        },
        saveAs(blob, filename) {
            const link = document.createElement('a');
            const body = document.querySelector("body");
            link.href = window.URL.createObjectURL(blob);
            link.download = filename;
            link.style.display = "none";
            body.appendChild(link);
            link.click();
            body.removeChild(link);
            window.URL.revokeObjectURL(link.href);
        },
        openReportDetail(item) {
            this.reportDialog = true;
            this.id = item.id;
            this.$nextTick(() => {
                this.$refs["inspectionreportdetail"].init();
            });
        },
        searchReset() {
            this.query = {};
            this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            console.log(JSON.stringify(params))
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        beforeOpen(done, type) {
            if (["edit", "view"].includes(type)) {
                // getDetail(this.form.id).then((res) => {
                //     this.form = res.data.data;
                // });
            }
            done();
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            var params = {};
            this.onLoad(this.page, params);
        },
        onLoad(page, params = {}) {
            this.loading = true;
            getRemainingproblemsPage(
                page.currentPage,
                page.pageSize,
                Object.assign(params, this.query)
            ).then((res) => {
                const data = res.data.data;
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        },
    },
};
</script>

<style  lang="scss" scoped>
.common-dialog {
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;

    .el-dialog {
        margin: 0 auto !important;
        position: relative;
        overflow: hidden;

        .el-dialog__header {
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            width: 100%;
            height: 60px;
            z-index: 1;
        }

        .el-dialog__body {
            position: relative;
            left: 0;
            top: 60px;
            bottom: 0;
            right: 0;
            padding: 0;
            width: 100%;
            overflow: hidden;
            overflow-y: auto;
            max-height: 90vh;
            padding-top: 80px;
            padding-bottom: 100px;
            z-index: 1;
        }

        .el-dialog__footer {
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            width: 100%;
            height: 80px;
            z-index: 1;
        }
    }
}
</style>
