<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <span class="text-lg">{{pageName}}</span>
                <el-button type="primary" @click="addEvent">
                    {{ t('addUsers') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="usersTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('username')" prop="username">
                        <el-input v-model="usersTable.searchParam.username" :placeholder="t('usernamePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('mobile')" prop="mobile">
                        <el-input v-model="usersTable.searchParam.mobile" :placeholder="t('mobilePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('nickname')" prop="nickname">
                        <el-input v-model="usersTable.searchParam.nickname" :placeholder="t('nicknamePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('memberLevel')" prop="member_level">
                        <el-input v-model="usersTable.searchParam.member_level" :placeholder="t('memberLevelPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('memberLabel')" prop="member_label">
                        <el-input v-model="usersTable.searchParam.member_label" :placeholder="t('memberLabelPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('wxOpenid')" prop="wx_openid">
                        <el-input v-model="usersTable.searchParam.wx_openid" :placeholder="t('wxOpenidPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('weappOpenid')" prop="weapp_openid">
                        <el-input v-model="usersTable.searchParam.weapp_openid" :placeholder="t('weappOpenidPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('wxUnionid')" prop="wx_unionid">
                        <el-input v-model="usersTable.searchParam.wx_unionid" :placeholder="t('wxUnionidPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('aliOpenid')" prop="ali_openid">
                        <el-input v-model="usersTable.searchParam.ali_openid" :placeholder="t('aliOpenidPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('douyinOpenid')" prop="douyin_openid">
                        <el-input v-model="usersTable.searchParam.douyin_openid" :placeholder="t('douyinOpenidPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('registerChannel')" prop="register_channel">
                        <el-input v-model="usersTable.searchParam.register_channel" :placeholder="t('registerChannelPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('registerType')" prop="register_type">
                        <el-input v-model="usersTable.searchParam.register_type" :placeholder="t('registerTypePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('loginIp')" prop="login_ip">
                        <el-input v-model="usersTable.searchParam.login_ip" :placeholder="t('loginIpPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('loginType')" prop="login_type">
                        <el-input v-model="usersTable.searchParam.login_type" :placeholder="t('loginTypePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('loginChannel')" prop="login_channel">
                        <el-input v-model="usersTable.searchParam.login_channel" :placeholder="t('loginChannelPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('loginCount')" prop="login_count">
                        <el-input v-model="usersTable.searchParam.login_count" :placeholder="t('loginCountPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('loginTime')" prop="login_time">
                        <el-input v-model="usersTable.searchParam.login_time" :placeholder="t('loginTimePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('lastVisitTime')" prop="last_visit_time">
                        <el-input v-model="usersTable.searchParam.last_visit_time" :placeholder="t('lastVisitTimePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('lastConsumTime')" prop="last_consum_time">
                        <el-input v-model="usersTable.searchParam.last_consum_time" :placeholder="t('lastConsumTimePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('sex')" prop="sex">
                        <el-select class="w-[280px]" v-model="usersTable.searchParam.sex" clearable :placeholder="t('sexPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in sexList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="usersTable.searchParam.status" clearable :placeholder="t('statusPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in statusList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('birthday')" prop="birthday">
                        <el-date-picker v-model="usersTable.searchParam.birthday" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('point')" prop="point">
                        <el-input v-model="usersTable.searchParam.point" :placeholder="t('pointPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('pointGet')" prop="point_get">
                        <el-input v-model="usersTable.searchParam.point_get" :placeholder="t('pointGetPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('balance')" prop="balance">
                        <el-input v-model="usersTable.searchParam.balance" :placeholder="t('balancePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('balanceGet')" prop="balance_get">
                        <el-input v-model="usersTable.searchParam.balance_get" :placeholder="t('balanceGetPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('money')" prop="money">
                        <el-input v-model="usersTable.searchParam.money" :placeholder="t('moneyPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('moneyGet')" prop="money_get">
                        <el-input v-model="usersTable.searchParam.money_get" :placeholder="t('moneyGetPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('moneyCashOuting')" prop="money_cash_outing">
                        <el-input v-model="usersTable.searchParam.money_cash_outing" :placeholder="t('moneyCashOutingPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('growth')" prop="growth">
                        <el-input v-model="usersTable.searchParam.growth" :placeholder="t('growthPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('growthGet')" prop="growth_get">
                        <el-input v-model="usersTable.searchParam.growth_get" :placeholder="t('growthGetPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('commission')" prop="commission">
                        <el-input v-model="usersTable.searchParam.commission" :placeholder="t('commissionPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('commissionGet')" prop="commission_get">
                        <el-input v-model="usersTable.searchParam.commission_get" :placeholder="t('commissionGetPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('commissionCashOuting')" prop="commission_cash_outing">
                        <el-input v-model="usersTable.searchParam.commission_cash_outing" :placeholder="t('commissionCashOutingPlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('isMember')" prop="is_member">
                        <el-select class="w-[280px]" v-model="usersTable.searchParam.is_member" clearable :placeholder="t('isMemberPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in is_memberList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('memberTime')" prop="member_time">
                        <el-date-picker v-model="usersTable.searchParam.member_time" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('createTime')" prop="create_time">
                        <el-date-picker v-model="usersTable.searchParam.create_time" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadUsersList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card>

            <div class="mt-[10px]">
                <el-table :data="usersTable.data" size="large" v-loading="usersTable.loading">
                    <template #empty>
                        <span>{{ !usersTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="member_id" :label="t('memberId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="member_no" :label="t('memberNo')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="pid_name" :label="t('pid')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="username" :label="t('username')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="mobile" :label="t('mobile')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="password" :label="t('password')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="nickname" :label="t('nickname')" min-width="120" :show-overflow-tooltip="true"/>
                    
                     <el-table-column :label="t('headimg')" width="100" align="left">
                        <template #default="{ row }">
                            <el-avatar v-if="row.headimg" :src="img(row.headimg)" />
                            <el-avatar v-else icon="UserFilled" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="member_level" :label="t('memberLevel')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="member_label" :label="t('memberLabel')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="wx_openid" :label="t('wxOpenid')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="weapp_openid" :label="t('weappOpenid')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="wx_unionid" :label="t('wxUnionid')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="ali_openid" :label="t('aliOpenid')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="douyin_openid" :label="t('douyinOpenid')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="register_channel" :label="t('registerChannel')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="register_type" :label="t('registerType')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="login_ip" :label="t('loginIp')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="login_type" :label="t('loginType')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="login_channel" :label="t('loginChannel')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="login_count" :label="t('loginCount')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="login_time" :label="t('loginTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="last_visit_time" :label="t('lastVisitTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="last_consum_time" :label="t('lastConsumTime')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('sex')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in sexList">
                                <div v-if="item.value == row.sex">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column :label="t('status')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in statusList">
                                <div v-if="item.value == row.status">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="birthday" :label="t('birthday')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="point" :label="t('point')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="point_get" :label="t('pointGet')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="balance" :label="t('balance')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="balance_get" :label="t('balanceGet')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="money" :label="t('money')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="money_get" :label="t('moneyGet')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="money_cash_outing" :label="t('moneyCashOuting')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="growth" :label="t('growth')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="growth_get" :label="t('growthGet')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="commission" :label="t('commission')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="commission_get" :label="t('commissionGet')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="commission_cash_outing" :label="t('commissionCashOuting')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('isMember')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in is_memberList">
                                <div v-if="item.value == row.is_member">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('memberTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.member_time || '' }}
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="address" :label="t('address')" min-width="120" :show-overflow-tooltip="true"/>
                    
                     <el-table-column :label="t('createTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.create_time || '' }}
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('updateTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.update_time || '' }}
                        </template>
                    </el-table-column>
                    

                    <el-table-column :label="t('operation')" fixed="right" min-width="120">
                       <template #default="{ row }">
                           <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                           <el-button type="primary" link @click="deleteEvent(row.member_id)">{{ t('delete') }}</el-button>
                       </template>
                    </el-table-column>

                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="usersTable.page" v-model:page-size="usersTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="usersTable.total"
                        @size-change="loadUsersList()" @current-change="loadUsersList" />
                </div>
            </div>

            
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getUsersList, deleteUsers, getWithUsersList } from '@/addon/zhjw/api/users'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let usersTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "username":"",
      "mobile":"",
      "nickname":"",
      "member_level":"",
      "member_label":"",
      "wx_openid":"",
      "weapp_openid":"",
      "wx_unionid":"",
      "ali_openid":"",
      "douyin_openid":"",
      "register_channel":"",
      "register_type":"",
      "login_ip":"",
      "login_type":"",
      "login_channel":"",
      "login_count":"",
      "login_time":"",
      "last_visit_time":"",
      "last_consum_time":"",
      "sex":"",
      "status":"",
      "birthday":[],
      "point":"",
      "point_get":"",
      "balance":"",
      "balance_get":"",
      "money":"",
      "money_get":"",
      "money_cash_outing":"",
      "growth":"",
      "growth_get":"",
      "commission":"",
      "commission_get":"",
      "commission_cash_outing":"",
      "is_member":"",
      "member_time":[],
      "create_time":""
    }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    const sexList = ref([] as any[])
    const sexDictList = async () => {
    sexList.value = await (await useDictionary('users_sex')).data.dictionary
    }
    sexDictList();
    const statusList = ref([] as any[])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    statusDictList();
    const is_memberList = ref([] as any[])
    const is_memberDictList = async () => {
    is_memberList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    is_memberDictList();
    const is_delList = ref([] as any[])
    const is_delDictList = async () => {
    is_delList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    is_delDictList();


/**
 * 获取用户管理列表
 */
const loadUsersList = (page: number = 1) => {
    usersTable.loading = true
    usersTable.page = page

    getUsersList({
        page: usersTable.page,
        limit: usersTable.limit,
         ...usersTable.searchParam
    }).then(res => {
        usersTable.loading = false
        usersTable.data = res.data.data
        usersTable.total = res.data.total
    }).catch(() => {
        usersTable.loading = false
    })
}
loadUsersList()

const router = useRouter()

/**
 * 添加用户管理
 */
const addEvent = () => {
    router.push('/users/users_edit')
}

/**
 * 编辑用户管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/users/users_edit?id='+data.member_id)
}

/**
 * 删除用户管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('usersDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteUsers(id).then(() => {
            loadUsersList()
        }).catch(() => {
        })
    })
}

    
    const pidList = ref([])
    const setPidList = async () => {
    pidList.value = await (await getWithUsersList({})).data
    }
    setPidList()

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadUsersList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>
