<template>
    <div class="left">
        <div class="name" :style="!switchvalue ? 'opacity: 0.6;' : ''">
            <el-tooltip class="btn" effect="light" :content="'科室名称' + '(' + hospitalAgencyName + ')'"
                placement="bottom">
                <el-button @click="opentrue">科室名称({{ hospitalAgencyName }})</el-button>
            </el-tooltip>
        </div>
        <div class="select" v-if="roleKey == 'admin'">
            <el-switch v-model="switchvalue" active-color="#1890ff" inactive-color="#DCDFE6" @change="changeswitch" />
        </div>
        <div class="input">
            <el-input v-model="querydepartmen.departmentName" placeholder="请输入科室名称" clearable
                @keyup.enter.native="Departmentlist" />
        </div>
        <div class="listitem" @click="itemdata" :style="!switchvalue ? 'opacity: 0.6;' : ''">
            <div :class="itemid == null ? 'allactive' : 'all'">
                全部
            </div>
            <span class="count">{{ count }}</span>
        </div>
        <div class="listitem" :style="!switchvalue ? 'opacity: 0.6;' : ''" v-for="(item, index) in DepartmentoList"
            :key="index" @click="itemdata(item)">
            <div :class="itemid == item.id ? 'allactive' : 'all'">
                {{ item.departmentName }}
            </div>
            <span class="count">{{ item.countNum }}</span>
        </div>
        <el-dialog title="切换医院" :visible.sync="open" width="50%">
            <div class="opentable">
                <el-form :model="opentablequery" label-width="80px" :inline="true">
                    <el-form-item label="科室名称" prop="hospitalAgencyName" label-width="120">
                        <el-input v-model="opentablequery.hospitalAgencyName" placeholder="请输入科室名称" clearable />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handquery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="queryreset">重置</el-button>
                    </el-form-item>
                </el-form>
                <el-table :data="HOSPITALlist" style="width: 100%" @cell-dblclick="nurseclick">
                    <el-table-column label="请选择" width="100" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" style="width: 15px; height: 15px"
                                v-if="querydepartmen.hospitalAgencyId == scope.row.id" circle
                                @click="nurseclick(scope.row)"></el-button>
                            <el-button v-else style="width: 15px; height: 15px" circle
                                @click="nurseclick(scope.row)"></el-button>
                        </template>
                    </el-table-column>
                    <el-table-column prop="agencyName" align="center" label="机构名称" />
                    <el-table-column prop="agencyCode" align="center" label="机构代码" />
                </el-table>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listOperationNum } from "@/api/operationInfo/operationInfo";
import {
    listWechatTemplateNum,
} from "@/api/manage/template";
import { getDepartmentList } from "@/api/system/question";
import { listScriptNum } from "@/api/manage/script";
import { listMessageNum } from "@/api/manage/message";
import { selectNumByDept } from "@/api/manage/propaganda";
import { listServicePackageNum } from "@/api/manage/servicepackage";
export default {
    props: ['methods'],
    name: "DepartmentList",
    data() {
        return {
            roleKey: localStorage.getItem('roleKey'),
            open: false,
            switchvalue: true,
            //校验
            itemid: null,
            // 表格数据
            DepartmentoList: [],
            count: '',
            // 查询参数
            querydepartmen: {
                departmentName: "",
                hospitalAgencyId: '',
            },
            opentablequery: {
                hospitalAgencyName: '',
            },
            //医院名称
            hospitalAgencyName: '',
            HOSPITALlist: JSON.parse(localStorage.getItem('HOSPITALlist'))
        };
    },
    watch: {
        'querydepartmen.departmentName'(val) {
            this.querydepartmen.departmentName = val
            this.Departmentlist();
        },
    },
    created() {
        this.querydepartmen.hospitalAgencyId = JSON.parse(localStorage.getItem('user')).hospitalAgencyId
        this.Departmentlist();
    },
    methods: {
        opentrue() {
            if (this.roleKey != 'admin') return
            this.open = true
        },
        handquery() {
            this.HOSPITALlist = JSON.parse(localStorage.getItem('HOSPITALlist')).filter(item => item.agencyName.includes(this.opentablequery.hospitalAgencyName));
        },
        queryreset() {
            this.opentablequery.hospitalAgencyName = ''
            this.HOSPITALlist = JSON.parse(localStorage.getItem('HOSPITALlist'))
        },
        nurseclick(item) {
            this.itemid = null
            this.querydepartmen.hospitalAgencyId = item.id
            this.Departmentlist();
            this.open = false
        },
        changeswitch(e) {
            let hospitalAgencyId = ''
            let hospitalAgencyName = ''
            if (e) {
                hospitalAgencyId = this.querydepartmen.hospitalAgencyId
                hospitalAgencyName = this.hospitalAgencyName
            }
            this.itemid = ''
            this.$emit("clickdepartment", {
                hospitalAgencyId: hospitalAgencyId,
                itemid: '',
                itemName: '',
                hospitalAgencyName: hospitalAgencyName
            });
        },
        resetQuery() {
            this.itemid = null
            this.switchvalue = true
            this.querydepartmen.departmentName = ''
            this.querydepartmen.hospitalAgencyId = JSON.parse(localStorage.getItem('user')).hospitalAgencyId
            this.Departmentlist();
        },
        // 左侧科室
        itemdata(item) {
            if (!this.switchvalue) return
            let obj
            if (item) {
                this.itemid = item.id
                obj = {
                    itemid: this.itemid,
                    itemName: item.departmentName,
                    hospitalAgencyId: this.querydepartmen.hospitalAgencyId,
                    hospitalAgencyName: this.hospitalAgencyName
                }
            } else {
                this.itemid = null
                obj = {
                    itemid: '',
                    itemName: '',
                    hospitalAgencyId: this.querydepartmen.hospitalAgencyId,
                    hospitalAgencyName: this.hospitalAgencyName
                }
            }
            this.$emit("clickdepartment", obj);
        },
        // 科室列表
        Departmentlist() {
            let method = undefined
            if (this.methods == 'listOperationNum') {
                method = listOperationNum(this.querydepartmen)
            } else if (this.methods == 'listScriptNum') {
                method = listScriptNum(this.querydepartmen)
            } else if (this.methods == 'listWechatTemplateNum') {
                method = listWechatTemplateNum(this.querydepartmen)
            } else if (this.methods == 'listMessageNum') {
                method = listMessageNum(this.querydepartmen)
            } else if (this.methods == 'selectNumByDept') {
                method = selectNumByDept(this.querydepartmen)
            } else if (this.methods == 'listServicePackageNum') {
                method = listServicePackageNum(this.querydepartmen)
            } else if (this.methods == 'getDepartmentList') {
                method = getDepartmentList(this.querydepartmen)
            }
            method.then(response => {
                console.log(response)
                this.DepartmentoList = response.data.deptNumList;
                this.hospitalAgencyName = response.data.hospitalAgencyName
                this.count = 0;
                this.DepartmentoList.forEach((item) => {
                    if (item.countNum != null) {
                        this.count += item.countNum;
                    }
                });
                if (this.methods == 'getDepartmentList') {
                    this.$emit("clickdepartment", {
                        hospitalAgencyId: this.querydepartmen.hospitalAgencyId,
                        itemid: '',
                        itemName: '',
                        hospitalAgencyName: this.hospitalAgencyName
                    });
                }
                this.$emit("clickdepartment", {
                    hospitalAgencyId: this.querydepartmen.hospitalAgencyId,
                    itemid: '',
                    itemName: '',
                    hospitalAgencyName: this.hospitalAgencyName
                });
            });
        },
    }
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
    text-align: left;
}

.opentable {
    height: 500px;
    overflow: auto;
}

.left {
    // height: calc(100vh - 119px);
    height: 100%;
    overflow: auto;
    position: relative;
    padding-right: 10px;

    .select {
        position: absolute;
        right: 10px;
        height: 40px;
        line-height: 40px;
        top: 0;
    }

    .btn:hover {
        color: #1890ff !important;
        background: none !important;
    }

    .btn:focus {
        color: #1890ff !important;
        background: none !important;
    }

    .btn {
        font-size: 16px !important;
        color: black !important;
        padding-left: 6px !important;
        text-align: left;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        width: 80% !important;
        border: none !important;
    }

    .input {
        margin-bottom: 10px;
    }

    .name {
        font-weight: 700;
        margin-bottom: 10px;
        line-height: 40px;
    }

    .listitem {
        width: 100%;
        height: 50px;
        border-bottom: 1px solid #dcdfe6;
        display: flex;
        align-items: center;
        position: relative;

        .count {
            display: inline-block;
            position: absolute;
            color: #a4a6aa;
            font-size: 13px;
            right: 15px;
        }

        .all {
            height: 50px;
            line-height: 50px;
            padding-left: 13px;
            font-size: 15px;
        }

        .allactive {
            background: #e8f4ff;
            height: 50px;
            width: 100%;
            line-height: 50px;
            padding-left: 13px;
            border-left: 3px solid #4d9de7;
        }
    }
}
</style>