<!--
 * @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">

                            <div class="line">
                                <el-button type="primary" icon="el-icon-download" size="small"
                                    @click="unfinishedInspectionExportHandle">导出未巡检设备清单表</el-button>

                                <div class="lineText">
                                    <span
                                        class="rainFirstInspectionCount">默认显示当前年度当前汛期未完成巡检站点信息，如需要查询其他历史数据，可通过设置年度及汛期进行查询</span>
                                </div>
                            </div>
                        </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 {
    getUnfinishedInspectionPage,
    unfinishedInspectionExport,
} 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,
                menu: 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: "rtuName",
                        span: 12,
                        search: true,
                        searchSpan: 4,
                        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: "rtuCode",
                        span: 12,
                        search: true,
                        searchSpan: 4,
                        hide: true,
                    },
                    {
                        label: "行政区",
                        prop: "adCity",
                        span: 12,
                        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: "rtuKindDesc",
                        span: 24,
                        editDisplay: false,
                        addDisplay: false,
                        overHidden: true,
                    },
                    {
                        label: "测站位置",
                        prop: "locationDesc",
                        span: 24,
                        width: 180,
                        editDisplay: false,
                        addDisplay: false,
                        overHidden: true,
                    },
                    {
                        label: "经度",
                        prop: "lng",
                        span: 24,
                        width: 120,
                        editDisplay: false,
                        addDisplay: false,
                    },
                    {
                        label: "纬度",
                        prop: "lat",
                        span: 24,
                        width: 120,
                        editDisplay: false,
                        addDisplay: false,
                    },
                    {
                        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",
                        hide: true,
                        type: 'select',
                        span: 24,
                        searchSpan: 4,
                        props: {
                            label: 'dictValue',
                            value: 'id'
                        },
                        dicData: [{
                            dictValue: '汛前巡检',
                            id: 1,
                        },
                        {
                            dictValue: '汛中（一）巡检',
                            id: 2,
                        },
                        {
                            dictValue: '汛中（二）巡检',
                            id: 3,
                        }],
                    },
                    {
                        label: "汛前巡检",
                        prop: "beforeRainSeasonStatus",
                        span: 24,
                        html: true,
                        formatter: (val) => {
                            if (val.beforeRainSeasonStatus == 1) {
                                return '<b style="color:darkcyan;">已填报</b>'
                            } else {
                                return '<b style="color:darkorange;">未填报</b>'
                            }
                        }
                    },
                    {
                        label: "汛中巡检(一)",
                        prop: "rainSeasonFirstStatus",
                        span: 24,
                        html: true,
                        formatter: (val) => {
                            if (val.rainSeasonFirstStatus == 1) {
                                return '<b style="color:darkcyan;">已填报</b>'
                            } else {
                                return '<b style="color:darkorange;">未填报</b>'
                            }
                        }
                    },
                    {
                        label: "汛中巡检(二)",
                        prop: "rainSeasonSecondStatus",
                        span: 24,
                        html: true,
                        formatter: (val) => {
                            if (val.rainSeasonSecondStatus == 1) {
                                return '<b style="color:darkcyan;">已填报</b>'
                            } else {
                                return '<b style="color:darkorange;">未填报</b>'
                            }
                        }
                    },
                ],
            },
            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;
        },
        unfinishedInspectionExportHandle() {
            let that = this;
            this.$confirm("确认导出未完成巡检设备清单信息表？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    unfinishedInspectionExport(this.query).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);
        },
        searchReset() {
            this.query = {};
            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();
        },
        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;
            getUnfinishedInspectionPage(
                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;
        }
    }
}

.line {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
}

.line .lineText {
    margin-left: 10px;
    margin-right: 0px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.line .lineText .inspectionCount {
    color: dodgerblue;
    font-size: 0.7rem;
}

.line .lineText .beforeRainInspectionCount {
    color: darkorange;
    font-size: 0.8rem;
}

.line .lineText .rainFirstInspectionCount {
    color: lightcoral;
    font-size: 0.8rem;
}

.line .lineText .rainSencodInspectionCount {
    color: darkcyan;
    font-size: 0.8rem;
}
</style>
