<!--
 * @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="orderStatus" slot-scope="row">
                            <div v-if="row.row.orderStatus == 1" style="color: red;">
                                等待确认
                            </div>
                            <div v-if="row.row.orderStatus == 2" style="color: orange;">
                                处理中
                            </div>
                            <div v-if="row.row.orderStatus == 3" style="color: blue;">
                                已处理
                            </div>
                            <!-- <div v-if="row.row.orderStatus == 4" style="color: green;">
                                已完成
                            </div> -->
                            <div v-else style="color: red;">
                            </div>
                        </template>
                        <template slot-scope="scope" slot="menuLeft">
                            <div class="line">
                                <div class="lineText">
                                    <span class="rtuCount">测站数量: {{ countInfo.rtuCount }}</span>
                                </div>
                                <div class="lineText">
                                    <span class="orderCount">任务数量: {{ countInfo.orderCount }}</span>
                                </div>
                                <div class="lineText">
                                    <span class="spliteLine">/</span>
                                </div>
                                <div class="lineText">
                                    <span class="orderUnconfirm">未确认任务数量: {{
                                        countInfo.orderUnconfirm }}</span>
                                </div>
                                <div class="lineText">
                                    <span class="spliteLine">/</span>
                                </div>
                                <div class="lineText">
                                    <span class="orderProcessing">处理中任务数量: {{
                                        countInfo.orderProcessing }}</span>
                                </div>
                                <div class="lineText">
                                    <span class="spliteLine">/</span>
                                </div>
                                <div class="lineText">
                                    <span class="orderReport">已处理任务数量: {{
                                        countInfo.reportOrder }}</span>
                                </div>
                            </div>
                        </template>
                        <template slot-scope="{ row, index }" slot="menu">
                            <el-button type="text" size="small" @click="openWarnInfo(row)"
                                icon="el-icon-warning-outline">异常信息
                            </el-button>
                            <el-button type="text" size="small" @click="openProcessList(row)"
                                icon="el-icon-edit-outline">维修详情
                            </el-button>
                            <el-button v-if="permission.order_close && row.orderClose == 0" style="color:lightcoral"
                                type="text" size="small" @click="orderOff(row)" icon="el-icon-circle-close">
                                人工关闭
                            </el-button>
                            <el-button v-if="permission.order_change && row.orderClose == 0" style="color:orange"
                                type="text" size="small" @click="orderChange(row)" icon="el-icon-position">
                                任务转派
                            </el-button>
                            <el-button v-if="permission.order_confirm && row.orderConfirm == 0" style="color:orange"
                                type="text" size="small" @click="orderConfirm(row)" icon="el-icon-circle-check">
                                任务确认
                            </el-button>
                        </template>
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
        <el-drawer :visible.sync="warnInfoDialog" v-if="warnInfoDialog" title="设备异常信息" direction="rtl" size="50%"
            append-to-body="true">
            <div slot="title" class="header-title">
                <span class="name">{{ dialogTitle }}</span>
            </div>
            <rtuwarninglist :rtuCode="rtuCode" ref="rtuwarninglist"></rtuwarninglist>
        </el-drawer>
        <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>
        <el-dialog title="维修任务转派" :visible.sync="orderTaskChangeDialog" width="600px" :close-on-click-modal="false"
            append-to-body>
            <avue-form :option="orderTaskChangeFormOption" @submit="orderTaskChangeSubmit" v-model="orderTaskChangeForm">
            </avue-form>
        </el-dialog>
    </div>
</template>

<script>

import {
    getPage,
    orderClose,
    getDetail,
    orderChange,
    getCountInfo,
    orderConfirm
} from "@/api/business/order/order.js";

import { getTree } from "@/api/baseinfo/region.js";
import checkorderprocessdetail from "./checkorderprocessdetail.vue";
import rtuwarninglist from "../warning/rtuwarninglist.vue";
import { mapGetters } from "vuex";
export default {
    components: {
        checkorderprocessdetail,
        rtuwarninglist
    },
    name: 'checkordermanagepage',
    data() {
        return {
            dialogTitle: "维修任务处理记录",
            recordDialog: false,
            warnInfoDialog: false,
            id: "",
            rtuCode: '',
            form: {},
            selectionList: [],
            query: {},
            countInfo: {
                rtuCount: 0,
                orderCount: 0,
                orderUnconfirm: 0,
                orderProcessing: 0
            },
            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: 300,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                column: [
                    {
                        label: "测站编码",
                        prop: "rtuCode",
                        span: 24,
                        search: true,
                        width: 120,
                        searchSpan: 8,
                    },
                    {
                        label: "测站名称",
                        prop: "rtuName",
                        span: 24,
                        width: 120,
                        overHidden: true,
                        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: "isRain",
                        span: 24,
                        width: 160,
                        html: true,
                        formatter: (val) => {
                            let html = '';
                            if (val.isRain && val.isRain == 1) {
                                html = html + '<b style="color:grey">雨量站</b>'
                            }
                            if (val.isRiver && val.isRiver == 1) {
                                if (html.length > 0) {
                                    html = html + '<b style="color:grey"> / </b>'
                                }
                                html = html + '<b style="color:grey">水位站</b>'
                            } else if (val.isRes && val.isRes == 1) {
                                if (html.length > 0) {
                                    html = html + '<b style="color:grey"> / </b>'
                                }
                                html = html + '<b style="color:grey">水位站</b>'
                            }
                            if (val.isGround && val.isGround == 1) {
                                if (html.length > 0) {
                                    html = html + '<b style="color:grey"> / </b>'
                                }
                                html = html + '<b style="color:grey">墒情站</b>'
                            }
                            return html;
                        }
                    },
                    {
                        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: "areaName",
                        span: 12,
                        width: 160,
                        hide: true,
                        overHidden: true,
                        editDisplay: false,
                        addDisplay: false,
                    },
                    {
                        label: "运维单位",
                        prop: "manageCompany",
                        span: 12,
                        width: 160,
                        overHidden: true,
                    },
                    {
                        label: "异常类别",
                        prop: "warnKindInfo",
                        span: 12,
                        width: 120,
                    },
                    {
                        label: "维修状态",
                        prop: "orderStatus",
                        span: 24,
                        width: 120,
                        type: 'select',
                        search: true,
                        searchSpan: 8,
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        dicData: [{
                            name: '等待确认',
                            code: 1
                        }, {
                            name: '处理中',
                            code: 2
                        }, {
                            name: '已处理',
                            code: 3
                        }]
                    },
                    {
                        label: "任务确认时间",
                        prop: "orderConfirmTime",
                        span: 24,
                        width: 120,
                    },
                    {
                        label: "维修时长",
                        prop: "taskProcessDuration",
                        span: 24,
                        width: 120,
                    },
                ],
            },
            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: 'realName',
                            value: 'id'
                        },
                        dicUrl: '/api/galaxy-business/serviceperson/list',
                    }]
            },
            data: [],
        };
    },
    prop: {
        rtuCode: ["String"],
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {
                closeBtn: this.vaildData(this.permission.order_close, false),
                changeBtn: this.vaildData(this.permission.order_change, false),
                confirmBtn: this.vaildData(this.permission.order_confirm, false),
            };
        },
    },
    created() {
        this.initTree();
    },
    methods: {
        initTree() {
            this.treeData = [];
            getTree().then(res => {
                this.treeData = res.data.data.map(item => {
                    return {
                        ...item,
                        leaf: !item.hasChildren
                    }
                })
                this.treeCode = this.treeData[0].id;
                this.treeOption.defaultExpandedKeys.push(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;
            });
        },
        openWarnInfo(item) {
            window.console.log(JSON.stringify(item));
            this.rtuCode = item.rtuCode;
            this.dialogTitle = "异常信息[" + item.rtuName + item.rtuCode + "]";
            this.warnInfoDialog = true;
            this.$nextTick(() => {
                this.$refs["rtuwarninglist"].init();
            });
        },
        openProcessList(item) {
            this.recordDialog = true;
            this.dialogTitle = "维修任务详情[" + item.rtuName + "_" + item.rtuCode + "]";
            this.id = item.id;
            this.$nextTick(() => {
                this.$refs["checkorderprocessdetail"].init();
            });
        },
        orderOff(item) {
            this.$prompt("请输入处理意见", "是否确认关闭此工单？", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",

            })
                .then(({ value }) => {
                    orderClose({ id: item.id, orderDesc: value }).then((res) => {
                        if (res.data.code == 200) {
                            this.$message({
                                type: "success",
                                message: "操作成功",
                            });
                            this.refreshChange();
                        }
                    });
                })
                .catch(() => { });
        },
        orderChange(item) {
            if (item.isConfirm == 1) {
                this.orderTaskChangeForm.contactUserName = item.contactUserName;
            } else {
                this.orderTaskChangeForm.contactUserName = '无';
            }
            this.orderTaskChangeForm.orderId = item.id;
            this.orderTaskChangeDialog = true;
        },
        orderTaskChangeSubmit(orm, done) {
            console.log(JSON.stringify(orm))
            this.orderTaskChangeDialog = false;
            orderChange({ id: this.orderTaskChangeForm.orderId, servicePersonId: this.orderTaskChangeForm.servicePersonId }).then((res) => {
                if (res.data.success) {
                    this.$message({
                        type: "success",
                        message: "操作已成功！",
                    });
                    this.refreshChange();
                }
            });
            done();
        },
        orderConfirm(item) {
            orderConfirm({ id: item.id }).then((res) => {
                if (res.data.code == 200) {
                    this.$message({
                        type: "success",
                        message: "操作成功",
                    });
                    this.refreshChange();
                }
            });
        },
        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['orderClose'] = 0;
            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 .rtuCount {
    color: gray;
    font-size: 0.8rem;
}

.line .lineText .orderCount {
    color: green;
    font-size: 0.8rem;
}

.line .lineText .spliteLine {
    color: gray;
    font-size: 0.8rem;
}

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

.line .lineText .orderProcessing {
    color: dodgerblue;
    font-size: 0.8rem;
}

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