<template>
    <view class="list-index">
        <view class="li-header">
            <input-bar></input-bar>
            <check-status-bar @click="accountCheck"></check-status-bar>
        </view>
        <view class="li-list">
            <block v-for="(e, i) in userLs" :key="i">
                <list-item
                    :name="e.actual_name"
                    :account="e.username"
                    :time="e.created_at"
                    @click:message="messageSubscription"
                    @click:account="accountAuthority(e)"
                    @click:delete="itemDeleteBtn(e, i)"
                ></list-item>
                <view style="height: 24rpx;"></view>
            </block>
        </view>
    </view>
</template>

<script>
import inputBar from './modules/input_bar.vue';
import checkStatusBar from './modules/check_status_bar.vue';
import listItem from './modules/item.vue';
import { routeTo, showModal, showLoad, hideLoad, showNone } from "@/utils/util.js";
import { ACCOUNT_API } from "../../js/api.js";
import server from "../../js/server.js";
import { userDelete } from "../../js/handle.js";

export default {
    components: {
        'input-bar': inputBar,
        'check-status-bar': checkStatusBar,
        'list-item': listItem
    },
    data(){
        return {
            brand_id: '', 
            userLs: [],
            page: 1
        }
    },
    onLoad(options){
        let _bid = options?.brand_id ?? '';
        this.getUserLs({ brand_id: _bid });
        this.brand_id =  _bid;
    },
    onReachBottom(){
        this.getUserLs({ brand_id: this.brand_id, page: this.page + 1 });
    },
    methods: {
        messageSubscription(){
            routeTo(`/subpackage/account/pages/message/info?brand_id=${this.brand_id}`, 'nT');
        },
        accountAuthority(e){
            routeTo(`/subpackage/account/pages/permission/info?brand_id=${this.brand_id}&id=${e.id}`, 'nT');
        },
        accountCheck(){
            routeTo(`/subpackage/account/pages/manage/check?brand_id=${this.brand_id}`, 'nT');
        },
        itemDeleteBtn(e, idx){
            showModal({ 
                content: '是否删除该账号？',
                showCancel: true,
                success: async res => {
                    if(res.confirm){
                        showLoad();
                        let _delRes = await userDelete({ id: e?.id ?? '' });
                        hideLoad();
                        if(_delRes === 'success'){
                            showNone('操作成功！');
                            this.userLs.splice(idx, 1);
                        }
                    }
                }
            });
        },
        // 权限管理-获取管理员
        getUserLs({ brand_id, page = 1, limit = 10 }){
            showLoad();
            return server.get({
                url: ACCOUNT_API.userRest,
                data: { brand_id, 'filter[role]': `ADMIN-BRAND-${brand_id}`, page, limit },
                isDefaultGet: false,
            })
            .then(res => {
                hideLoad();
                let _data = res?.data || {};
                if(_data.code === 0){
                    let _ls = _data?.data?.data ?? [];
                    if(page === 1)return this.userLs = _ls;
                    if(_ls.length <= 0)return showNone('暂无更多！');
                    this.page = page;
                    return this.userLs = [ ...this.userLs, ..._ls ];
                }else{
                    return Promise.reject(_data);
                }
            })
            .catch(err => {
                showModal({
                    title: '提示',
                    content: err.message || '加载用户失败！'
                })
                console.warn('subpackage account pages list index getUserLs err --->', err);
                // return Promise.reject(err);
            })
        },
    }
}
</script>

<style lang="scss">
    .li-list{
        padding: 24upx;
    }
</style>