<!--专区综合（）-->
<template>
    <div id="specialArea">
        <Nav :title="navTitle"></Nav>
        <div class="search">
            <van-search v-model="searchValue" @search="onSearch" shape="round" :placeholder="$t('specialArea.searchPlaceholder')" />
        </div>
        <div class="content">
            <div class="rankBox">
                <template v-if="rankList.length">
                    <div class="cell" v-for="item,index in rankList" :key="index" @click="getCommodity(item)">
                        <div class="left">
                            <!-- <div class="tagBox">
                                <span>{{index+1}}</span>
                            </div> -->
                            <img :src="requestUrl+item.faceImg" alt="" srcset="">
                        </div>
                        <div class="right">
                            <div>
                                <div class="center">
                                    <span>{{item.commodityName}}</span>
                                </div>

                            </div>
                            <div class="pricebox">
                                <span>¥{{item.childPrice}} - ¥{{item.adultPrice}}</span>

                            </div>
                            <div>
                                <span>{{item.fwb}}</span>
                            </div>
                        </div>
                    </div>
                </template>
                <template v-else>
                    <van-empty :description="$t('specialArea.none')"></van-empty>
                </template>
            </div>
        </div>
        <van-overlay :show="loading" @click="loading = false">
            <div class="wrapper" @click.stop>
                <div class="block">
                    <van-loading color="#1989fa" size="50" />
                </div>
            </div>
        </van-overlay>
    </div>
</template>

<script>
import Nav from "@/components/pages/nav";
import { selectTypeList, selectPreferredList } from "@/api/comp/home.js";
export default {
    data() {
        return {
            navTitle: "",
            rankList: [],
            loading: true,
            productListData: {},
            pageType: null,
            searchValue: "",
            requestUrl: process.env.VUE_APP_BASE_API,
        };
    },
    created() {
        this.productListData = this.$store.state.home.productListData;
        this.pageType = this.productListData.type;
        switch (Number(this.pageType)) {
            case 1:
                //搜索
                this.navTitle = this.$t("specialArea.title1");
                //获取全局搜索字段
                let commodityName = this.$store.state.home.searchValue;
                this.searchValue = commodityName;
                this.getSearchList();
                break;
            case 2:
                //优选专区
                this.navTitle = this.$t("specialArea.title2");
                this.selectExcellentList();
                break;
            case 3:
                //专区分类
                this.navTitle = this.productListData.title;
                this.selectClassificationList();
                break;
        }
        let that = this;
        setTimeout(() => {
            that.loading = false;
        }, 1000);
    },
    methods: {
        //搜索
        onSearch(value) {
            switch (Number(this.pageType)) {
                case 1:
                    //搜索
                    this.getSearchList(value);
                    break;
                case 2:
                    //优选专区
                    this.selectExcellentList(value);
                    break;
                case 3:
                    //专区分类
                    this.selectClassificationList(value);
                    break;
            }
        },
        getCommodity(data) {
            console.log(data, "获得的商品数据");
            this.$store.commit("home/SET_PRODUCT", data);
            this.$router.push("/productDetails");
        },
        //搜索状态列表赋值
        getSearchList() {
            this.loading = true;
            //获取全局deptId
            let deptId = this.$store.state.user.companyinfo.deptId;
            //拿到数据前做清空
            this.rankList = [];
            let request = {
                commodityName: this.searchValue,
                deptId: deptId,
            };
            selectPreferredList(request).then((res) => {
                if (res.code == 200) {
                    this.rankList = res.data;
                    console.log(this.rankList, "rankList");
                    //从富文本里抓取一段字用于显示说明
                    for (let i = 0; i < this.rankList.length; i++) {
                        let text = "";
                        let temp = document.createElement("div");
                        temp.innerHTML = this.rankList[i].attention;
                        for (let j = 0; j < temp.children.length; j++) {
                            if (!temp.children[j].children.length) {
                                if (!temp.children[j].children.length) {
                                    if (j == 0) {
                                        text += temp.children[j].innerText;
                                    } else {
                                        text +=
                                            "," + temp.children[j].innerText;
                                    }
                                } else {
                                    //这里是为一个诡异的数据单独加的  不排除有多个诡异数据格式 个人认为此值不应放在这里
                                    for (
                                        let k = 0;
                                        k < temp.children[j].children.length;
                                        k++
                                    ) {
                                        if (
                                            temp.children[j].children[k]
                                                .tagName == "STRONG"
                                        ) {
                                            for (
                                                let o = 0;
                                                o <
                                                temp.children[j].children[k]
                                                    .children.length;
                                                o++
                                            ) {
                                                if (
                                                    temp.children[j].children[k]
                                                        .children[o].tagName ==
                                                    "EM"
                                                ) {
                                                    if (j == 0) {
                                                        text +=
                                                            temp.children[j]
                                                                .children[k]
                                                                .children[o]
                                                                .innerText;
                                                    } else {
                                                        text +=
                                                            "," +
                                                            temp.children[j]
                                                                .children[k]
                                                                .children[o]
                                                                .innerText;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        this.rankList[i].fwb = text;
                    }
                    this.loading = false;
                }
            });
        },
        //查询优选列表
        selectExcellentList() {
            this.loading = true;
            //获取全局
            let deptId = this.$store.state.user.companyinfo.deptId;
            //拿到数据前做清空
            this.rankList = [];
            let request = {
                isPreferred: 1,
                commodityName: this.searchValue,
                deptId: deptId,
            };
            selectPreferredList(request).then((res) => {
                if (res.code == 200) {
                    //插入数据
                    this.rankList = res.data;
                    //从富文本里抓取一段字用于显示说明
                    for (let i = 0; i < this.rankList.length; i++) {
                        let text = "";
                        let temp = document.createElement("div");
                        temp.innerHTML = this.rankList[i].attention;
                        for (let j = 0; j < temp.children.length; j++) {
                            if (!temp.children[j].children.length) {
                                if (j == 0) {
                                    text += temp.children[j].innerText;
                                } else {
                                    text += "," + temp.children[j].innerText;
                                }
                            }
                        }
                        this.rankList[i].fwb = text;
                    }
                    this.loading = false;
                }
            });
        },
        //查询分类专区
        selectClassificationList() {
            this.loading = true;
            //获取全局
            let deptId = this.$store.state.user.companyinfo.deptId;
            //拿到数据前做清空
            this.rankList = [];
            let request = {
                typeId: this.productListData.typeId,
                commodityName: this.searchValue,
                deptId: deptId,
            };
            selectPreferredList(request).then((res) => {
                if (res.code == 200) {
                    //插入数据
                    this.rankList = res.data;
                    //从富文本里抓取一段字用于显示说明
                    for (let i = 0; i < this.rankList.length; i++) {
                        let text = "";
                        let temp = document.createElement("div");
                        temp.innerHTML = this.rankList[i].attention;
                        for (let j = 0; j < temp.children.length; j++) {
                            if (!temp.children[j].children.length) {
                                if (j == 0) {
                                    text += temp.children[j].innerText;
                                } else {
                                    text += "," + temp.children[j].innerText;
                                }
                            }
                        }
                        this.rankList[i].fwb = text;
                    }
                    this.loading = false;
                }
            });
        },
    },
    components: {
        Nav,
    },
};
</script>

<style lang="scss" scoped>
#specialArea {
    .content {
        padding: 0.12rem /* 12/100 */;
        background: #f9f9f9;
        box-sizing: border-box;
        min-height: calc(100vh - 0.44rem /* 128/100 */);
        .rankBox {
            padding: 0.05rem /* 5/100 */ 0 /* 15/100 */;
            .cell {
                width: 100%;
                height: 1.39rem /* 139/100 */;
                display: flex;
                padding: 0.12rem /* 12/100 */;
                margin-bottom: 0.15rem /* 15/100 */;
                box-sizing: border-box;
                border-radius: 0.05rem /* 5/100 */;
                color: #333333;
                background-color: #fff;
                box-shadow: 0px 0px 5px #e9e9e9;
                .left {
                    width: 38%;
                    height:1rem;
                    border-radius: 0.08rem;
                    overflow: hidden;
                    position: relative;
                    img {
                        width: 100%;
                        height: 100%;
                        object-fit: cover;
                    }
                    .tagBox {
                        width: 32px;
                        height: 32px;
                        background: url("../../../../../public/static/images/tagBg.png");
                        position: absolute;
                        left: 0;
                        top: 0;
                        z-index: 1;
                        font-size: 0.14rem /* 16/100 */;
                        text-align: left;
                        span {
                            margin-top: 0.05rem /* 5/100 */;
                            margin-left: 0.05rem /* 5/100 */;
                            display: inline-block;
                            color: #fff;
                        }
                    }
                }
                .right {
                    padding-top: 0.03rem /* 3/100 */;
                    width: 72%;
                    font-size: 0.13rem /* 15/100 */;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-between;
                    > div:nth-child(1) {
                        width: 100%;
                        // height: 0.63rem;
                        max-height: 0.63rem;
                        display: flex;
                        padding: 0 0 0 0.12rem;

                        box-sizing: border-box;
                        > div:nth-child(1) {
                            text-align: justify;
                            /*color: #38d0c9;*/
                            width: 55%;
                            width:100%;
                            overflow: hidden;
                            display: -webkit-box; /*盒子模型微弹性伸缩模型*/
                            -webkit-box-orient: vertical; /*伸缩盒子的子元素垂直排列*/
                            -webkit-line-clamp: 2; /*文本显示3行*/
                        }
                        > div:nth-child(2) {
                            color: #e94225;
                            width: 45%;
                            overflow: hidden;
                            display: -webkit-box; /*盒子模型微弹性伸缩模型*/
                            -webkit-box-orient: vertical; /*伸缩盒子的子元素垂直排列*/
                            -webkit-line-clamp: 3; /*文本显示3行*/
                        }
                    }
                    > div:nth-child(2) {
                        margin-top: 0.05rem;
                        width: 100%;

                        text-align: left;
                        padding: 0 0 0 0.12rem;
                        color: #bb271a;
                        font-weight:600;
                        font-size:0.15rem;
                        box-sizing: border-box;
                        overflow: hidden; /*文本超出隐藏*/
                        display: -webkit-box; /*盒子模型微弹性伸缩模型*/
                        -webkit-box-orient: vertical; /*伸缩盒子的子元素垂直排列*/
                        -webkit-line-clamp: 2; /*文本显示3行*/
                    }
                    > div:nth-child(3) {
                        margin-top: 0.05rem;
                        width: 100%;
                        // height: 0.37rem;
                        max-height: 0.37rem;
                        text-align: left;
                        padding: 0 0 0 0.12rem;
                        color: #999999;
                        box-sizing: border-box;
                        overflow: hidden; /*文本超出隐藏*/
                        display: -webkit-box; /*盒子模型微弹性伸缩模型*/
                        -webkit-box-orient: vertical; /*伸缩盒子的子元素垂直排列*/
                        -webkit-line-clamp: 2; /*文本显示3行*/
                    }
                }
            }
        }
    }
}
</style>
<style>
#specialArea .van-search__content {
    /*border: 1px solid #6acdc8;*/
    background-color:#f6f6f6;
}
#specialArea .content .rankBox .cell .right > div:nth-child(3) > span {
    font-size: 0.12rem;
    line-height: 1.2;
}
</style>