<template>
    <div id="CompanySelect">
        <el-dropdown
            trigger="click"
            @command="selectCompanyInfo"
            v-on:visible-change="companyListShow"
        >
            <span class="el-dropdown-link">
                {{ companyName
                }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu
                slot="dropdown"
                style="width: 300px; height: 350px; overflow: auto"
            >
                <el-input
                    v-model="searchEntName"
                    placeholder="搜索"
                    v-on:input="companyNameChange"
                    :clearable="true"
                    style="padding: 10px 10px"
                ></el-input>
                <el-dropdown-item
                    :command="l.entTaxId"
                    v-bind:key="l.entTaxId"
                    v-for="l in companyList"
                    style="font-size: 1rem; padding: 2px 10px"
                    >{{ l.entName }}
                </el-dropdown-item>
            </el-dropdown-menu>
        </el-dropdown>
    </div>
</template>

<script>
import cache from "@/utils/cache";
import { AxiosRequestData } from "../../../data/axios_request";

export default {
    name: "index",
    data() {
        return {
            companyName: "",
            companyList: {},
            searchEntName: "",
            timer: "",
        };
    },
    created() {
        console.log("企业 created");
        this.getDefaultCompany();
        this.getCustomerData();
    },
    methods: {
        // 获取默认企业信息
        async getDefaultCompany() {
            let user = JSON.parse(cache.getSession("user"));
            let token = cache.getToken();
            if (user && token) {
                const url = "/auth/comm/user/findDefaultChoose";
                let response = await AxiosRequestData(
                    url,
                    token,
                    {
                        mobile: user.account,
                    },
                    false,
                    2
                );
                if (response) {
                    if (response.code === 0) {
                        console.log("默认企业信息");
                        console.log(response);
                        this.companyName = response.data.entName;
                        cache.setSession("company", response.data);
                    }
                }
            }
        },
        // 获取企业列表
        async getCustomerData() {
            let user = JSON.parse(cache.getSession("user"));
            let token = cache.getToken();
            if (user && token) {
                const url = "/sys/entInfo/findPageByMobile";
                let response = await AxiosRequestData(
                    url,
                    token,
                    {
                        userMobile: user.account,
                        reqChannel: 4,
                        entName: this.searchEntName,
                        pageNum: 1,
                        pageSize: 100,
                    },
                    false,
                    2
                );
                if (response) {
                    if (response.code === 0) {
                        // let listDatas = response.data.records.map((_, i) => ({
                        //     entTaxId: response.data.records[i].entTaxId,
                        //     entName: response.data.records[i].entName,
                        // }));
                        // serviceStatus 表示企业下服务异常，无开票设备
                        let listDatas = response.data.records.filter(
                            (item) => item.serviceStatus != 4
                        );
                        this.companyList = listDatas;

                        console.log("企业列表：");
                        console.log(response.data.records);
                    }
                }
            }
        },
        // 设置默认企业信息
        async setDefaultCompany(taxId) {
            let user = JSON.parse(cache.getSession("user"));
            let token = cache.getToken();
            if (user && token) {
                const url = "/auth/comm/user/setDefaultChoose";
                let response = await AxiosRequestData(
                    url,
                    token,
                    {
                        mobile: user.account,
                        defaultChoose: taxId,
                        reqChannel: 4,
                    },
                    false,
                    1
                );
                if (response) {
                    if (response.code === 0) {
                        console.log("设置默认企业 返回结果：");
                        console.log(response);

                        this.companyName = response.data.chooseEntity.entName;
                        cache.setSession("company", response.data.chooseEntity);

                        // 新增
                        console.log("切换企业，设置token：");
                        console.log(response.data.token);
                        cache.setToken(response.data.token);

                        location.reload();
                        console.log("信息确认");
                        console.log(cache.getSession("user"));
                    } else {
                        this.$message.error(response.msg + "，请联系服务商！");
                    }
                }
            }
        },
        // 切换默认企业信息
        selectCompanyInfo(command) {
            console.log("切换默认企业");
            console.log(command);
            this.setDefaultCompany(command);
        },
        // 搜索企业名称
        companyNameChange() {
            let _this = this;
            clearTimeout(_this.timer);
            _this.timer = setTimeout(() => {
                _this.getCustomerData();
            }, 1000);
        },
        // 判断企业列表是否显示
        companyListShow(isShow) {
            if (!isShow) {
                this.searchEntName = "";
            }
        },
    },
};
</script>

<style scoped>
.el-dropdown-link {
    cursor: pointer;
    /* color: #409EFF; */
    color: #004080;
    font-size: 1.2rem;
    font-weight: bold;
}

.el-icon-arrow-down {
    font-size: 12px;
}
</style>
