<template>
    <view>
        <view class="head">
            <view class="title u-line-1">{{ detail.storename || '' }}</view>
            <view class="content">
                <view class="left">
                    <view class="tag-box">
                        <view v-if="detail.storetype" class="storetype">
                            {{ detail.storetype }}
                        </view>
                        <view v-if="detail.markettype" class="markettype">
                            {{ detail.markettype }}
                        </view>
                    </view>
                    <view class="row">
                        <text class="iconfont icon-renyuan-hui" />
                        <text>
                            {{ detail.name }}
                        </text>
                        <text style="margin-left: 5px;">
                            {{ detail.phonenumber }}
                        </text>
                    </view>
                    <view class="row">
                        <text class="iconfont icon-dizhi-hui1" />
                        {{ getCity(detail) }}
                    </view>
                    <view class="row">
                        状态：<text :style="{ color: detail.color }">{{ detail.status }}</text>
                    </view>
                </view>
                <view class="right">
                    <u--image :src="detail.cover" :width="tovw(100)" :height="tovw(100)" radius="5">
                        <template v-slot:loading>
                            <u-loading-icon color="red"></u-loading-icon>
                        </template>
                    </u--image>
                </view>
            </view>
        </view>
        <u-tabs :scrollable="false" lineColor="#C30D23" :activeStyle="{ color: '#C30D23', fontWeight: 'bold' }" :list="tabs"
            @click="changeCurrent" :current="current" />
        <view v-show="current == 0">
            <displayCell :showList="showList" :detail="detail" />
            <view :style="{ height: detail.status == '审核' ? '30px' : '75px' }" />
        </view>

        <view v-show="current == 1">
            <view class="user-head">
                <view class="title">
                    门店人员
                </view>
                <view class="funs">
                    <view class="item" hover-class="navigator" @click="changeSearchShow">
                        <text class="iconfont icon-sousuo" />
                    </view>
                    <navigator v-if="authOption.includes('storeInsertUser')" class="item" @click="onInsert"
                        :url="'/store/insert/member?id=' + detail.sa_storeid">
                        <text class="iconfont icon-xinzeng" />
                    </navigator>
                </view>
            </view>
            <u-transition :show="searchShow">
                <view class="My_search-box">
                    <My_search :focus="searchShow" :value="content.where.condition" @onSearch="onSearch" />
                </view>
            </u-transition>
            <My_listbox ref="List" @getlist="getList" :bottomHeight="detail.status != '审核' ? 70 : 30">
                <user-list :list="list" @onClick="onClick" :isDelete="authOption.includes('storeDeleteUser')"
                    @onDelete="onDelete" />
            </My_listbox>
        </view>
        <view v-if="detail.status != '审核'" class="footer">
            <view class="insert" style="flex:1;" :class="detail.status == '新建' ? '' : 'forbidden'"
                @click="detail.status == '新建' ? toEdit() : ''">
                编辑门店信息
            </view>
            <view v-if="authOption.includes('storeComeUpForReview')" class="invite"
                @click="detail.status == '新建' ? submit() : revocation()">
                {{ detail.status == '新建' ? '提交门店信息' : '撤回' }}
            </view>
        </view>
    </view>
</template>

<script>
import userList from "../../components/userList";
export default {
    components: { userList },
    data() {
        return {
            current: 0,
            sa_storeid: 0,
            detail: {},
            tabs: [{ name: "门店详情" }, { name: "门店人员" }],
            showList: [],
            "content": {
                "where": {
                    "condition": ""
                }
            },
            list: null,
            searchShow: false,
            authOption: [],
        }
    },
    onLoad(options) {
        this.sa_storeid = options.id;
        this.content.sa_storeid = options.id;
        this.getDetail();
        uni.setNavigationBarTitle({
            title: '门店信息'
        });
        let auth = this.getApps('工作台', "/team/team/index");
        if (auth) this.authOption = auth.option;
    },
    onShow() {
        setTimeout(() => {
            this.$refs.List.setHeight();
        }, 300)
    },
    onUnload() {
        delete this.$Http.uploadStoreDetail;
        delete this.$Http.uploadUserList;
    },
    methods: {
        changeSearchShow() {
            this.searchShow = !this.searchShow;
            setTimeout(() => {
                this.$refs.List.setHeight();
            }, 300)
        },
        getDetail() {
            this.$Http.basic({
                "id": "20240410095402",
                "content": {
                    "sa_storeid": this.sa_storeid
                }
            }).then(res => {
                console.log("店铺详情", res)
                if (this.cutoff(res.msg)) return;

                switch (res.data.status) {
                    case '提交':
                        res.data.color = '#009270'
                        break;
                    case '审核':
                        res.data.color = '#095DE0'
                        break;
                    default:
                        res.data.color = '#333333'
                        break;
                }

                res.data.cover = res.data.attinfos.length ? this.getSpecifiedImage(res.data.attinfos[0], 'compressed') || uni.getStorageSync("site").logo : uni.getStorageSync("site").logo
                this.detail = res.data;
                this.showList = [{
                    label: "门店名称",
                    key: "storename"
                }, {
                    label: "面积(m²)",
                    key: "area"
                }, {
                    label: "负责人",
                    key: "name"
                }, {
                    label: "负责人电话",
                    key: "phonenumber",
                    funName: "callPhone"
                }, {
                    label: "负责人账号",
                    key: "accountno"
                }, {
                    label: "门店售前电话",
                    key: "sale_phonenmber",
                    funName: "callPhone"
                }, {
                    label: "门店售后电话",
                    key: "service_phonenmber",
                    funName: "callPhone"
                }, {
                    label: "门店类型",
                    key: 'storetype'
                }, {
                    label: "卖场类型",
                    key: "markettype"
                }, {
                    label: "门店地址",
                    value: this.getCity(res.data)
                }, {
                    label: "所属经销商",
                    key: "enterprisename"
                },]
            })
        },
        toEdit() {
            this.$Http.uploadStoreDetail = this.getDetail.bind(this);
            uni.navigateTo({
                url: "/store/insert/store?title=编辑门店信息&data=" + JSON.stringify(this.detail)
            })
        },
        getList(init = false) {
            if (this.paging(this.content, init)) return;
            this.$Http.basic({
                "id": "20240410150702",
                content: this.content
            }).then(res => {
                this.$refs.List.RefreshToComplete()
                console.log("获取门店人员列表", res)
                this.$refs.List.setHeight();
                if (this.cutoff(res.msg)) return;
                this.list = res.pageNumber == 1 ? res.data : this.list.concat(res.data);
                this.content = this.$refs.List.paging(this.content, res)
            })
        },
        submit() {
            let that = this;
            uni.showModal({
                title: '提交门店信息',
                content: '是否确定提交门店信息?',
                success: function ({ confirm }) {
                    if (confirm) that.$Http.basic({
                        "id": "20240410110302",
                        "content": {
                            "sa_storeid": that.detail.sa_storeid
                        }
                    }).then(res => {
                        console.log('提交门店信息', res)
                        if (that.cutoff(res.msg, '提交成功!')) return;
                        that.$Http.uploadStoreList && that.$Http.uploadStoreList();
                        that.getDetail();
                    })
                }
            });
        },
        revocation() {
            let that = this;
            uni.showModal({
                title: '提交门店信息',
                content: '是否确定撤回提交门店信息?',
                success: function ({ confirm }) {
                    if (confirm) that.$Http.basic({
                        "id": "20240410110802",
                        "content": {
                            "sa_storeid": that.detail.sa_storeid
                        }
                    }).then(res => {
                        console.log('撤回提交门店信息', res)
                        if (that.cutoff(res.msg, '撤回提交成功!')) return;
                        that.$Http.uploadStoreList && that.$Http.uploadStoreList();
                        that.getDetail();
                    })
                }
            });
        },
        changeCurrent({ index, name }) {
            this.current = index;
            if (name == '门店人员') {
                uni.pageScrollTo({
                    scrollTop: 0,
                    duration: 0,
                });
                if (this.list == null) {
                    this.getList(true);
                } else {
                    setTimeout(() => {
                        this.$refs.List.setHeight();
                    }, 300)
                }
            }
        },
        onClick(item) {
            uni.navigateTo({
                url: '/team/userCenter/personal?id=' + item.sys_enterprise_hrid
            });
            this.onInsert();
        },
        onDelete(item) {
            this.onInsert();
            let that = this;
            uni.showModal({
                title: '提示',
                content: `是否确定将“${item.name}”移除门店?`,
                success: ({ confirm }) => {
                    if (confirm) that.$Http.basic({
                        "id": "20240410153602",
                        "content": {
                            "sa_store_hrids": [
                                item.sa_store_hrid
                            ]
                        }
                    }).then(res => {
                        console.log("删除门店成员", res)
                        if (that.cutoff(res.msg, `移除成员成功`)) return;
                        that.$Http.uploadUserList();
                    })
                },
            })

        },
        onInsert() {
            this.$Http.uploadUserList = function () {
                if (this.content.pageNumber && this.content.pageNumber >= 2) {
                    let content = this.paging(this.content, true, true)
                    this.$Http.basic({
                        "id": "20240410150702",
                        content
                    }).then(res => {
                        console.log("更新人员列表", res)
                        if (this.cutoff(res.msg)) return;
                        this.$refs.List.setHeight()
                        this.list = res.data;
                        this.$refs.List.paging(content, res, true)
                    })
                }
            }.bind(this)
        },
        onSearch(condition) {
            this.content.where.condition = condition;
            this.getList(true)
        }
    },

}
</script>

<style lang="scss" scoped>
.head {
    width: 100vw;
    background: #FFFFFF;
    border-radius: 8px;
    padding: 10px;
    box-sizing: border-box;
    margin-bottom: 10px;

    .title {
        line-height: 24px;
        font-family: Source Han Sans SC, Source Han Sans SC;
        font-weight: bold;
        font-size: 16px;
        color: #333333;
    }

    .content {
        position: relative;

        .left {
            width: 220px;

            .tag-box {
                display: flex;

                .storetype,
                .markettype {
                    font-family: Source Han Sans SC, Source Han Sans SC;
                    font-size: 12px;
                    color: #FFFFFF;
                    height: 24px;
                    line-height: 24px;
                    padding: 0 4px;
                    border-radius: 2px;
                    margin-top: 5px;
                }

                .storetype {
                    background: #095DE0;
                }

                .markettype {
                    background: #FC9228;
                    margin-left: 5px;
                }
            }

            .row {
                line-height: 17px;
                font-family: Source Han Sans SC, Source Han Sans SC;
                font-size: 12px;
                color: #888888;
                margin-top: 5px;

                .iconfont {
                    margin-right: 5px;
                    font-size: 12px;
                }
            }
        }

        .right {
            position: absolute;
            right: 0;
            bottom: 0;
        }

    }
}

.user-head {
    height: 60px;
    width: 100vw;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px;
    box-sizing: border-box;

    .title {
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 16px;
        color: #333333;
    }

    .funs {
        display: flex;

        .item {
            width: 40px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background: #FFFFFF;
            border-radius: 4px;
            border: 1px solid #CCCCCC;
            font-size: 18px;
            color: #666666;
            margin-left: 10px;
        }
    }
}

.My_search-box {
    padding: 10px;
    padding-top: 0px;
}

.footer {
    position: fixed;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    width: 100vw;
    height: 65px;
    background: #FFFFFF;
    box-shadow: 0px -2px 6px 1px rgba(0, 0, 0, 0.16);
    padding: 5px 10px;
    box-sizing: border-box;

    .insert {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 168px;
        height: 45px;
        background: #FFFFFF;
        border-radius: 5px;
        border: 1px solid #999999;
        font-family: PingFang SC, PingFang SC;
        font-size: 16px;
        color: #666666;
        box-sizing: border-box;
    }

    .invite {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 177px;
        height: 45px;
        background: #C30D23;
        border-radius: 5px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 16px;
        color: #FFFFFF;
        margin-left: 10px;
    }


    .forbidden {
        opacity: .6;
    }
}
</style>