<!--
 * @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">
                        <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)">维修详情
                            </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>
            <checkorderprocessdetail :orderId="id" ref="checkorderprocessdetail"></checkorderprocessdetail>
        </el-drawer>
    </div>
</template>

<script>
import {
    getPage,
    getDetail,
    getCountInfo
} from "@/api/business/order/order.js";
import { getTree } from "@/api/baseinfo/region.js";
import checkorderprocessdetail from "./checkorderprocessdetail.vue";
import { mapGetters } from "vuex";
export default {
    components: {
        checkorderprocessdetail,
    },
    name: 'checkorderhistory',
    data() {
        return {
            dialogTitle: "维修任务历史记录",
            recordDialog: false,
            id: "",
            form: {},
            selectionList: [],
            countInfo: {
                reportOrderCount: 0
            },
            query: {},
            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: 100,
                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: 200,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                column: [
                    {
                        label: "测站名称",
                        prop: "rtuName",
                        span: 12,
                        search: true,
                        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,
                        hide: true,
                    },
                    {
                        label: "行政区",
                        prop: "areaName",
                        span: 12,
                        editDisplay: false,
                        addDisplay: false,
                    },
                    {
                        label: "运维单位",
                        prop: "manageCompany",
                        span: 12,
                    },
                    // {
                    //     search: true,
                    //     label: "运维单位",
                    //     prop: "orgId",
                    //     hide: true,
                    //     type: 'select',
                    //     span: 12,
                    //     props: {
                    //         label: 'dictValue',
                    //         value: 'id'
                    //     },
                    //     dicUrl: '/api/galaxy-business/baseinfo/org/list',
                    // },
                    // {
                    //     label: "维修状态",
                    //     prop: "orderStatusKey",
                    //     span: 12,
                    //     props: {
                    //         label: 'dictValue',
                    //         value: 'id'
                    //     },
                    //     dicUrl: '/api/galaxy-system/dict-biz/dictionary?code=rtu_check_order_status',
                    // },
                    // {
                    //     label: "预警类别",
                    //     prop: "warnKind",
                    //     span: 12,
                    //     props: {
                    //         label: 'dictValue',
                    //         value: 'id'
                    //     },
                    //     dicUrl: '/api/galaxy-system/dict-biz/dictionary?code=warn_kinds',
                    // },
                    // {
                    //     label: "工单说明",
                    //     prop: "orderDesc",
                    //     span: 12,
                    // },
                    // {
                    //     label: "处理人",
                    //     prop: "processorName",
                    //     span: 12,
                    // },
                    {
                        label: "创建时间",
                        prop: "createTime",
                        span: 12,
                    },
                    {
                        label: "关闭时间",
                        prop: "updateTime",
                        span: 12,
                    },
                ],
            },
            orderTaskChangeDialog: false,
            orderTaskChangeForm: {
                orderId: '',
                servicePersonId: '',
                contactUserName: '',
            },
            orderTaskChangeFormOption: {
                column: [
                    {
                        label: "当前任务负责人",
                        prop: "contactUserName",
                        labelWidth: 130,
                        span: 24,
                        disabled: true,
                    },
                    {
                        span: 24,
                        labelWidth: 130,
                        label: '指派到',
                        prop: 'servicePersonId',
                        type: 'select',
                        props: {
                            label: 'personName',
                            value: 'id'
                        },
                        dicUrl: '/api/galaxy-business/project/person/list',
                    }]
            },
            data: [],
        };
    },
    prop: {
        rtuCode: ["String"],
    },
    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;
                this.getCountInfo(this.treeCode);
            });
        },
        nodeClick(data) {
            this.treeCode = data.id;
            this.treeParentCode = data.parentId;
            this.query = {};
            this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            var params = {};
            this.onLoad(this.page, params);
            this.getCountInfo(this.treeCode);
        },
        getCountInfo(adCode) {
            getCountInfo(
                adCode
            ).then((res) => {
                this.countInfo = res.data.data;
            });
        },
        openProcessList(item) {
            this.recordDialog = true;
            this.dialogTitle = "维修任务详情[" + item.rtuName + "_" + item.rtuCode + "]";
            this.id = item.id;
            this.$nextTick(() => {
                this.$refs["checkorderprocessdetail"].init();
            });
        },
        orderChange(item) {
            if (item.isConfirm == 1) {
                this.orderTaskChangeForm.contactUserName = item.contactUserName;
            } else {
                this.orderTaskChangeForm.contactUserName = '无';
            }
            this.orderTaskChangeForm.orderId = item.id;
            this.orderTaskChangeDialog = true;
        },
        searchReset() {
            this.query = {};
            this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            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() {
            var params = {};
            this.onLoad(this.page, params);
        },
        onLoad(page, params = {}) {
            this.loading = true;
            params['orderConfirm'] = 1;
            params['orderClose'] = 1;
            getPage(
                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>
.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>