<template>
    <div id="deviceList">
        <saga-title :mess="mess"></saga-title>
        <div class="table-main">
            <div class="search-header">
                <floor-cascader @change="changeFloor"></floor-cascader>
                <my-cascader ref="cascader" @change="changeDevice"></my-cascader>
                <el-tooltip class="item" effect="dark" content="按照本地名称 | 本地编码 | 图纸编码 | 交付编码关键字搜索" placement="top-start">
                    <el-input
                        v-show="param.deviceId"
                        v-model="param.keyWord"
                        @keyup.enter.native="handleChangeKeyWord"
                        @clear="handleChangeKeyWord"
                        clearable
                        placeholder="请输入关键字"
                        prefix-icon="el-icon-search"
                        style="width: 250px; margin-left: 10px"
                    ></el-input>
                </el-tooltip>
                <!-- <el-button size="small" @click="handleLinkAssets" class="iconfont icon-batchassociation">批量关联资产</el-button> -->
                <!-- <el-tooltip class="item" effect="dark" content="请选择建筑楼层和设备类" placement="top-start"> -->
                <el-dropdown @command="handleCommand" class="deviceList-excel">
                    <el-button type="primary"> 数据导入<i class="el-icon-arrow-down el-icon--right"></i> </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="dynamic">深化点表导入</el-dropdown-item>
                        <el-dropdown-item command="static">台账信息导入</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <!-- </el-tooltip> -->
            </div>
            <hanson-table @close="close" :graphyId="graphyId" ref="tableMain"></hanson-table>
        </div>
        <!-- <table-transfers></table-transfers> -->
        <linkassets-dialog :dialog="dialog"></linkassets-dialog>
        <!-- 深化点表导入 -->
        <dynamic-dialog ref="dynamicDialog" @upDataDevice="upDataDevice"></dynamic-dialog>
        <!-- 台账信息导入 -->
        <static-dialog ref="staticDialog" @upDataDevice="upDataDevice"></static-dialog>
    </div>
</template>
<script>
import sagaTitle from "@/components/ledger/lib/title";
import floorCascader from "@/components/ledger/lib/floorCascader";
import myCascader from "@/components/ledger/lib/equipCascader";
import hansonTable from "@/components/ledger/handsontables/device";
import tableTransfers from "@/components/ledger/tableTransfers";
import linkassetsDialog from "@/components/ledger/lib/linkassets";
import { countEquip } from "@/api/object/equip";
import dynamicDialog from "@/components/ledger/lib/dynamicDialog";
import staticDialog from "@/components/ledger/lib/staticDialog";
import { mapGetters } from "vuex";

export default {
    components: {
        sagaTitle,
        floorCascader,
        myCascader,
        hansonTable,
        tableTransfers,
        linkassetsDialog,
        staticDialog,
        dynamicDialog,
    },
    data() {
        return {
            currentBuilding: "", // 当前建筑楼层信息
            mess: {
                perjectName: "",
                name: [
                    {
                        key: "设备",
                        num: 0,
                    },
                    {
                        key: "资产",
                        num: 0,
                    },
                ],
            },
            param: {
                buildId: "",
                floorId: "",
                deviceId: "",
                keyWord: "", //检索关键字
                ProjId: "",
                secret: "",
            },
            assetGroupList: [],
            graphyId: null,
            dialog: {
                linkAssets: false,
            },
        };
    },
    computed: {
        ...mapGetters("layout", ["projectId", "projects", "secret", "userId"]),
    },
    created() {
        this.param.ProjId = this.projectId;
        this.param.secret = this.secret;
        this.getProjName();
        this.getCount();
        // let deviceId = this.$route.params.deviceId;
        // if (deviceId) {
        //     this.$nextTick(() => {
        //         this.$refs.cascader.setValue(deviceId);
        //     });
        // }
    },
    activated() {
        if (!this.$route.meta.isBack) {
            this.param.ProjId = this.projectId;
            this.param.secret = this.secret;
            this.getProjName();
            this.getCount();
            let deviceId = this.$route.params.deviceId;
            if (deviceId) {
                this.$nextTick(() => {
                    this.$refs.cascader.setValue(deviceId);
                });
            } else {
                this.$nextTick(() => {
                    this.$refs.cascader.setValue("");
                    this.changeDevice({ code: "", facility: "" });
                });
            }
        }
        this.$route.meta.isBack = false;
    },
    /**
     * 路由守卫
     * 从部件和详情页返回，设置页面缓存
     */
    beforeRouteEnter(to, from, next) {
        const routerList = ["/ledger/parts", "/ledger/partsmanage", "/ledger/deviceDetails"]
        if (routerList.includes(from.path)) {
            to.meta.isBack = true;
        } else {
            to.meta.isBack = false;
        }
        next();
    },
    watch: {
        projectId() {
            this.param.ProjId = this.projectId;
            this.param.secret = this.secret;
            this.getProjName();
            this.getCount();
        },
    },
    methods: {
        getProjName() {
            this.projects.forEach((item) => {
                if (item.id == this.projectId) this.mess.perjectName = item.name;
            });
            // this.getEqCode()
        },
        close(val) {
            this.getCount();
            // if (val && this.$refs.cascader) {
            //     this.$refs.cascader.changeVal(val.code);
            // }
            // this.$refs.cascader.getData(this.currentBuilding);
            this.$refs.tableMain.getTableData();
        },
        // getEqCode() {
        //   getEquipmentFamily(res => {
        //     debugger
        //     this.assetGroupList = res.Content
        //     this.getGraphyId()
        //   })
        // },
        //获取统计数量
        getCount() {
            let param = {
                filters: "not propertyId isnull",
            };
            countEquip({}, (res) => {
                this.mess.name[0].num = res.count;
            });
            countEquip(param, (res) => {
                this.mess.name[1].num = res.count;
            });
        },
        // getGraphyId() {
        //   let param = {
        //     ProjId: this.param.ProjId,
        //     secret: this.param.secret,
        //     type: "SystemEquip"
        //   }
        //   getGraphyId(param).then(res => {
        //     if (res.data.Result == "success") {
        //       debugger
        //       this.graphyId = res.data.graph_id
        //       if (this.$refs.tableMain)
        //         this.$refs.tableMain.setGraphyId(this.graphyId, this.assetGroupList);
        //     } else {
        //       this.$message.error("请求错误：" + res.data.ResultMsg)
        //     }
        //   }).catch(_ => {
        //     this.$message.error("请求错误")
        //   })
        // },
        //修改楼层
        changeFloor(value) {
            if (value[0]) {
                this.param.buildId = value[0];
            }
            if (value[1]) {
                this.param.floorId = value[1];
            } else {
                this.param.floorId = null;
            }
            if (!!this.param.deviceId) {
                if (this.$refs.tableMain) this.$refs.tableMain.getHeaderData(this.param);
            }
            this.currentBuilding = value;
            this.$refs.cascader.getData(value);
        },

        //修改设备族
        changeDevice(value) {
            this.param.deviceId = value.code || "";
            this.param.name = value.facility || "";
            this.param.keyWord = "";
            if (this.$refs.tableMain) this.$refs.tableMain.getHeaderData(this.param);
        },
        // 修改检索关键字
        handleChangeKeyWord() {
            if (this.$refs.tableMain) this.$refs.tableMain.getTableData(this.param.keyWord);
        },
        // 刷新数据
        upDataDevice() {
            this.$refs.tableMain.getTableData();
        },
        // 批量关联资产
        handleLinkAssets() {
            this.dialog.linkAssets = true;
        },
        /**
         * 选择下拉菜单
         * @param command   下拉选项标识
         */
        handleCommand(command) {
            // 选择深化点表导入
            if (command === "dynamic") {
                this.$refs.dynamicDialog.openDialog();
            } else if (command === "static") {
                // 选择台账信息导入
                this.$refs.staticDialog.openDialog();
            }
        },
    },
};
</script>
<style lang="less" scoped>
#deviceList {
    overflow: hidden;
    height: 100%;
    background-color: #fff;
    position: relative;

    .table-main {
        position: absolute;
        top: 87px;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: #fff;
        // border-radius: 5px;
        // padding: 10px 0;
    }

    .search-header {
        overflow: hidden;
        padding-bottom: 10px;
        margin: 0 10px;
        border-bottom: 1px solid #bcbcbc;

        .deviceList-excel {
            float: right;
            margin-right: 15px;
        }

        .icon-batchassociation {
            float: right;
            font-size: 12px;

            /deep/ span {
                margin-left: 5px;
            }
        }
    }
}
</style>
