<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('addUser') }}
        </el-button>
      </div>

      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form :inline="true" :model="userTable.searchParam" ref="searchFormRef">

          <!--          <el-form-item :label="t('siteId')" prop="site_id">-->
          <!--            <el-select class="w-[280px]" v-model="userTable.searchParam.site_id" clearable-->
          <!--                       :placeholder="t('siteIdPlaceholder')">-->
          <!--              <el-option label="全部" value=""></el-option>-->
          <!--              <el-option-->
          <!--                  v-for="(item, index) in site_idList"-->
          <!--                  :key="index"-->
          <!--                  :label="item.name"-->
          <!--                  :value="item.value"-->
          <!--              />-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->

          <el-form-item :label="t('tel')" prop="tel">
            <el-input v-model="userTable.searchParam.tel" :placeholder="t('telPlaceholder')"/>
          </el-form-item>

          <el-form-item :label="t('name')" prop="name">
            <el-input v-model="userTable.searchParam.name" :placeholder="t('namePlaceholder')"/>
          </el-form-item>

          <!--          <el-form-item :label="t('password')" prop="password">-->
          <!--            <el-input v-model="userTable.searchParam.password" :placeholder="t('passwordPlaceholder')"/>-->
          <!--          </el-form-item>-->

          <el-form-item :label="t('sex')" prop="sex">
            <el-select class="w-[280px]" v-model="userTable.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('birthday')" prop="birthday">
            <el-date-picker v-model="userTable.searchParam.birthday" type="daterange"
                            value-format="YYYY-MM-DD"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')"/>
          </el-form-item>


          <el-form-item :label="t('isShow')" prop="is_show">
            <el-select class="w-[280px]" v-model="userTable.searchParam.is_show" clearable
                       :placeholder="t('isShowPlaceholder')">
              <el-option label="全部" value=""></el-option>
              <el-option
                  v-for="(item, index) in is_showList"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loadUserList()">{{ 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="userTable.data" size="large" v-loading="userTable.loading">
          <template #empty>
            <span>{{ !userTable.loading ? t('emptyData') : '' }}</span>
          </template>
          <el-table-column prop="id" :label="t('id')" min-width="60" :show-overflow-tooltip="true"/>

          <el-table-column prop="tel" :label="t('tel')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column prop="name" :label="t('name')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column :label="t('pic')" width="100" align="left">
            <template #default="{ row }">
              <el-avatar v-if="row.pic" :src="img(row.pic)"/>
              <el-avatar v-else icon="UserFilled"/>
            </template>
          </el-table-column>
          <!--          <el-table-column prop="password" :label="t('password')" min-width="120" :show-overflow-tooltip="true"/>-->

          <el-table-column :label="t('pid')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              {{ row.user && row.user.name ? row.user.name + `#` + row.user.id : '顶级' }}
            </template>
          </el-table-column>

          <el-table-column prop="level" :label="t('level')" 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="wx_unionid" :label="t('wxUnionid')" 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 prop="birthday" :label="t('birthday')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column :label="t('isShow')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              <div v-for="(item, index) in is_showList">
                <div v-if="item.value == row.is_show">{{ item.name }}</div>
              </div>
            </template>
          </el-table-column>

          <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('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.id)">{{ t('delete') }}</el-button>
            </template>
          </el-table-column>

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

      <edit ref="editUserDialog" @complete="loadUserList"/>
    </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 {getUserList, deleteUser,} from '@/addon/hygl/api/user'
import {img} from '@/utils/common'
import {ElMessageBox, FormInstance} from 'element-plus'
import Edit from '@/addon/hygl/views/user/components/user-edit.vue'
import {useRoute} from 'vue-router'

const route = useRoute()
const pageName = route.meta.title;

let userTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    "site_id": "",
    "tel": "",
    "name": "",
    "password": "",
    "sex": "",
    "birthday": [],
    "is_show": ""
  }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
const site_idList = ref([] as any[])
const site_idDictList = async () => {
  site_idList.value = await (await useDictionary('placeholder_')).data.dictionary
}
site_idDictList();
const sexList = ref([] as any[])
const sexDictList = async () => {
  sexList.value = await (await useDictionary('sex_radio')).data.dictionary
}
sexDictList();
const is_showList = ref([] as any[])
const is_showDictList = async () => {
  is_showList.value = await (await useDictionary('is_show_radio')).data.dictionary
}
is_showDictList();

/**
 * 获取会员管理列表
 */
const loadUserList = (page: number = 1) => {
  userTable.loading = true
  userTable.page = page

  getUserList({
    page: userTable.page,
    limit: userTable.limit,
    ...userTable.searchParam
  }).then(res => {
    userTable.loading = false
    userTable.data = res.data.data
    userTable.total = res.data.total
  }).catch(() => {
    userTable.loading = false
  })
}
loadUserList()

const editUserDialog: Record<string, any> | null = ref(null)

/**
 * 添加会员管理
 */
const addEvent = () => {
  editUserDialog.value.setFormData()
  editUserDialog.value.showDialog = true
}

/**
 * 编辑会员管理
 * @param data
 */
const editEvent = (data: any) => {
  editUserDialog.value.setFormData(data)
  editUserDialog.value.showDialog = true
}

/**
 * 删除会员管理
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('userDeleteTips'), t('warning'),
      {
        confirmButtonText: t('confirm'),
        cancelButtonText: t('cancel'),
        type: 'warning',
      }
  ).then(() => {
    deleteUser(id).then(() => {
      loadUserList()
    }).catch(() => {
    })
  })
}


const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadUserList()
}
</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>
