<!--
 * @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="4">
                <div class="box">
                    <el-scrollbar>
                        <basic-container>
                            <avue-tree :option="treeOption" :data="treeData" @node-click="nodeClick">
                                <span class="el-tree-node__label" slot-scope="{ node, data }">
                                    <span>
                                        <i class="el-icon-office-building"></i>
                                        {{ (node || {}).label }}
                                    </span>
                                </span>
                            </avue-tree>
                        </basic-container>
                    </el-scrollbar>
                </div>
            </el-col>
            <el-col :span="20">
                <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" @sort-change="sortChange">
                        <!-- <template slot-scope="scope" slot="menuLeft">
                            <div class="line">

                                <div class="lineText">
                                    <span class="orderProcessing">完成任务数量: {{
                                        countInfo.reportOrderCount }}</span>
                                </div>
                            </div>
                        </template> -->

                        <template slot-scope="{ row, index }" slot="menu">
                            <el-button type="text" size="small" @click="openProcessList(row)"
                                icon="el-icon-edit-outline">维修历史
                            </el-button>
                        </template>
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
        <el-drawer :visible.sync="recordDialog" v-if="recordDialog" title="维修历史" direction="rtl" size="50%"
            append-to-body="true">
            <div slot="title" class="header-title">
                <span class="name">{{ dialogTitle }}</span>
            </div>
            <rtucheckorderhistorylistvue :rtuCode="rtuCode" ref="rtucheckorderhistorylistvue"></rtucheckorderhistorylistvue>
        </el-drawer>
    </div>
</template>

<script>
import {
    getHistroyPage,
    getDetail,
} from "@/api/business/order/order.js";
import { getTree } from "@/api/baseinfo/region.js";
import rtucheckorderhistorylistvue from "./rtucheckorderhistorylist.vue";
import { mapGetters } from "vuex";
export default {
    components: {
        rtucheckorderhistorylistvue,
    },
    name: 'checkorderhistory',
    data() {
        return {
            dialogTitle: "维修任务历史记录",
            recordDialog: false,
            rtuCode: '',
            form: {},
            selectionList: [],
            countInfo: {
                reportOrderCount: 0
            },
            query: {},
            sort: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            treeCode: '',
            treeParentCode: '',
            treeData: [],
            treeOption: {
                nodeKey: "id",
                defaultExpandedKeys: [],
                lazy: false,
                addBtn: false,
                menu: true,
                size: "small",
                props: {
                    labelText: "标题",
                    label: "title",
                    value: "value",
                    children: "children",
                },
            },
            option: {
                labelWidth: 120,
                viewLabelWidth: 100,
                searchlabelWidth: 160,
                height: "auto",
                calcHeight: 80,
                align: "center",
                headerAlign: "center",
                tip: false,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                addBtn: false,
                tree: true,
                border: true,
                index: true,
                editBtn: false,
                delBtn: false,
                menuWidth: 180,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                column: [
                    {
                        label: "测站编码",
                        prop: "rtuCode",
                        span: 24,
                        search: true,
                        width: 120,
                        searchSpan: 8,
                    },
                    {
                        label: "测站名称",
                        prop: "rtuName",
                        span: 24,
                        search: true,
                        searchSpan: 8,
                        // 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,
                        hide: true,
                        editDisplay: false,
                        addDisplay: false,
                        html: true,
                        formatter: (val) => {
                            if (val.adCity) {
                                return '<b>' + val.adCity + '</b>/' + '<b style="color:grey">' + val.adDist + '</b>'
                            }
                        }
                    },
                    {
                        label: "运维单位",
                        prop: "manageCompany",
                        span: 12,
                    },
                    {
                        label: "安装位置",
                        prop: "locationDesc",
                        span: 12,
                        overHidden: true,
                    },
                    // {
                    //     label: '异常类型',
                    //     prop: 'warnKind',
                    //     type: 'select',
                    //     width: 100,
                    //     hide: true,
                    //     editDisplay: false,
                    //     addDisplay: false,
                    //     search: true,
                    //     searchSpan: 8,
                    //     props: {
                    //         label: 'name',
                    //         value: 'code'
                    //     },
                    //     span: 24,
                    //     dicData: [{
                    //         name: '测站离线',
                    //         code: 1
                    //     }, {
                    //         name: '测站时钟异常',
                    //         code: 2
                    //     }, {
                    //         name: '测站雨量小时报漏报',
                    //         code: 3
                    //     }, {
                    //         name: '测站水位小时报漏报',
                    //         code: 4
                    //     }, {
                    //         name: '雨量5分钟上报延时',
                    //         code: 5
                    //     }, {
                    //         name: '雨量站小时上报延时',
                    //         code: 6
                    //     }, {
                    //         name: '水位站小时上报延时',
                    //         code: 7
                    //     }, {
                    //         name: '雨量疑似异常值',
                    //         code: 8
                    //     }]
                    // },
                    {
                        label: "关闭类别",
                        prop: "orderCloseType",
                        span: 24,
                        search: true,
                        searchSpan: 8,
                        type: 'select',
                        hide: true,
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        dicData: [{
                            name: '自动关闭',
                            code: 0
                        }, {
                            name: '人工关闭',
                            code: 1
                        }]
                    },
                    {
                        label: "是否维修",
                        prop: "orderReportStatus",
                        span: 24,
                        search: true,
                        searchSpan: 8,
                        type: 'select',
                        hide: true,
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        dicData: [{
                            name: '未维修',
                            code: 0
                        }, {
                            name: '已维修',
                            code: 1
                        }]
                    },
                    {
                        label: "维修任务次数",
                        prop: "orderCount",
                        span: 12,
                        sortable: 'custom',
                    },
                ],
            },
            data: [],
        };
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {
                // closeBtn: this.vaildData(this.permission.order_close, false),
            };
        },
    },
    created() {
        this.initTree();
    },
    methods: {
        initTree() {
            this.treeData = [];
            getTree().then(res => {
                this.treeData = res.data.data.map(item => {
                    return {
                        ...item,
                        leaf: !item.hasChildren
                    }
                })
                this.treeOption.defaultExpandedKeys.push(this.treeData[0].id);
                this.treeCode = this.treeData[0].id;
            });
        },
        nodeClick(data) {
            this.$refs["crud"].clearSort();
            this.treeCode = data.id;
            this.treeParentCode = data.parentId;
            this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            var params = {};
            this.onLoad(this.page, params);
        },
        sortChange(val) {
            console.log(JSON.stringify(val))
            //prop":"tm","order":"ascending"  descending
            let prop = val.prop;
            if (prop === 'orderCount') {
                this.page.currentPage = 1;
                var params = {};
                let order = val.order;
                if (order) {
                    params['order'] = order;
                    params['prop'] = prop;
                    this.sort = {};
                    Object.assign(this.sort, params)
                } else {
                    this.sort = {};
                }
                this.onLoad(this.page, params);
            }
        },
        openProcessList(item) {
            this.dialogTitle = "维修任务历史[" + item.rtuName + "_" + item.rtuCode + "]";
            this.rtuCode = item.rtuCode;
            this.recordDialog = true;
            this.$nextTick(() => {
                this.$refs["rtucheckorderhistorylistvue"].init();
            });
        },
        searchReset() {
            this.page.currentPage = 1;
            let params = {};
            this.onLoad(this.page, params);
        },
        searchChange(params, done) {
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            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() {
            this.page.currentPage = 1;
            var params = {};
            this.onLoad(this.page, params);
        },
        onLoad(page, params = {}) {
            this.loading = true;
            params['orderReportStatus'] = 1;
            console.log("load" + JSON.stringify(params))
            getHistroyPage(
                page.currentPage,
                page.pageSize,
                Object.assign(params, this.sort, 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>
.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 .orderProcessing {

    color: dodgerblue;
    font-size: 0.8rem;
}
</style>