<!-- 用户列表 -->
<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]" id="search_div">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80px">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input class="inputWidth" v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号码" prop="phone">
              <el-input class="inputWidth" v-model="queryParams.phone" placeholder="请输入手机号码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户性别" prop="sex">
              <el-select class="inputWidth" v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
                <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="帐号状态" prop="status">
              <el-select class="inputWidth" v-model="queryParams.status" placeholder="请选择帐号状态" clearable>
                <el-option v-for="dict in dy_user_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="会员等级" prop="leveId">
              <el-input class="inputWidth" v-model="queryParams.leveId" placeholder="请输入会员等级" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              <!-- <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['manage:dyUser:edit']"
                >封禁</el-button
              > -->
              <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['manage:dyUser:remove']"
                >注销</el-button
              >
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <!-- <template #header id="table_top">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['manage:dyUser:edit']"
              >封禁</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['manage:dyUser:remove']"
              >注销</el-button
            >
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template> -->

      <el-table :height="autoTableHeight" v-loading="loading" :data="dyUserList" @selection-change="handleSelectionChange" style="width: 100%" border>
        <el-table-column fixed type="selection" width="55" align="center" />
        <el-table-column fixed label="用户ID" align="center" prop="id" v-if="true" />
        <el-table-column fixed label="帐号状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="dy_user_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column fixed label="会员等级" align="center" prop="leveId" />
        <el-table-column fixed label="昵称" align="center" prop="nickName" />
        <el-table-column label="用户性别" align="center" prop="sex">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
          </template>
        </el-table-column>
        <el-table-column label="头像" align="center" prop="">
          <template #default="scope">
            <img src="" width="30" height="30" lt="" />
          </template>
        </el-table-column>
        <el-table-column label="背景图" align="center" prop="">
          <template #default="scope">
            <img src="" width="30" height="30" lt="" />
          </template>
        </el-table-column>
        <el-table-column width="120" label="手机号码" align="center" prop="phone" />
        <el-table-column width="130" label="绑定微信账号" align="center" prop="openId" />
        <el-table-column label="生日" align="center" prop="birthday" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column width="55" label="年龄" align="center" prop="age" />
        <el-table-column width="150" label="常住地" align="center" prop="address" />
        <el-table-column width="55" label="星座" align="center" prop="constellation" />
        <el-table-column width="100" label="职业" align="center" prop="occupation" />
        <el-table-column width="120" label="简介" align="center" prop="remark" />
        <el-table-column width="120" label="IP形象" align="center" prop="" />
        <el-table-column width="120" label="发布的笔记" align="center" prop="" />
        <el-table-column width="120" label="发布的活动" align="center" prop="" />
        <el-table-column width="120" label="关注的人" align="center" prop="" />
        <el-table-column width="120" label="粉丝数" align="center" prop="" />
        <el-table-column width="120" label="到过的景点" align="center" prop="" />
        <el-table-column width="120" label="点亮的城市" align="center" prop="" />
        <el-table-column width="120" label="获得的明信片" align="center" prop="" />
        <el-table-column width="120" label="我的愿望" align="center" prop="" />
        <el-table-column width="120" label="我的背包" align="center" prop="" />
        <el-table-column width="120" label="获得的物品" align="center" prop="" />

        <el-table-column width="120" label="积分" align="center" prop="score" />
        <el-table-column width="120" label="收藏" align="center" prop="" />
        <el-table-column width="120" label="收货地址" align="center" prop="" />
        <el-table-column width="120" label="我的订单" align="center" prop="" />
        <el-table-column width="120" label="邀请人数" align="center" prop="" />
        <el-table-column width="120" label="处理方式" align="center" prop="" />
        <el-table-column width="130" label="封禁理由" align="center" prop="reason" show-overflow-tooltip />
        <el-table-column width="100" label="封禁天数" align="center" prop="banDays">
          <template #default="scope">
            <dict-tag :options="dy_user_ban_days" :value="scope.row.banDays" />
          </template>
        </el-table-column>
        <el-table-column width="120" label="操作时间" align="center" prop="" />
        <el-table-column width="120" label="操作人" align="center" prop="" />

        <!-- <el-table-column label="最后登录IP" width="100" align="center" prop="loginIp" />
        <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="openbanned(scope.row)">封禁</el-button>
            <el-button link type="primary" size="small">权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        id="table_page"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改C端用户信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body center>
      <el-form ref="dyUserFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="封禁原因" prop="reason">
          <el-select v-model="form.reason" placeholder="请选择封禁原因">
            <el-option v-for="dict in dy_user_ban_mode" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理方式" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dy_user_status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理天数" prop="banDays">
          <el-select v-model="form.banDays" placeholder="请选择封禁天数">
            <el-option v-for="dict in dy_user_ban_days" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="bannedUser">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DyUser" lang="ts">
import { listDyUser, getDyUser, delDyUser, addDyUser, updateDyUser } from '@/api/manage/dyUser';
import { DyUserVO, DyUserQuery, DyUserForm } from '@/api/manage/dyUser/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { dy_user_ban_days, dy_user_status, sys_user_sex, dy_user_ban_mode } = toRefs<any>(
  proxy?.useDict('dy_user_ban_days', 'dy_user_status', 'sys_user_sex', 'dy_user_ban_mode')
);

const dyUserList = ref<DyUserVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const autoTableHeight = ref<number>(750);

const queryFormRef = ref<ElFormInstance>();
const dyUserFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  rowData: {} as DyUserVO,
  visible: false,
  title: ''
});
const initFormData: DyUserForm = {
  id: undefined,
  nickName: undefined,
  phone: undefined,
  sex: undefined,
  password: undefined,
  status: undefined,
  loginIp: undefined,
  loginDate: undefined,
  remark: undefined,
  leveId: undefined,
  birthday: undefined,
  age: undefined,
  address: undefined,
  constellation: undefined,
  occupation: undefined,
  openId: undefined,
  wxImgUrl: undefined,
  reason: undefined,
  banDays: undefined,
  longitude: undefined,
  latitude: undefined
};
const data = reactive<PageData<DyUserForm, DyUserQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nickName: undefined,
    phone: undefined,
    sex: undefined,
    password: undefined,
    status: undefined,
    loginIp: undefined,
    loginDate: undefined,
    leveId: undefined,
    birthday: undefined,
    age: undefined,
    address: undefined,
    constellation: undefined,
    occupation: undefined,
    openId: undefined,
    wxImgUrl: undefined,
    reason: undefined,
    banDays: undefined,
    longitude: undefined,
    latitude: undefined,
    params: {}
  },
  rules: {
    reason: [{ required: true, message: '请选择封禁原因', trigger: 'blur' }],
    status: [{ required: true, message: '请选择处理方式', trigger: 'blur' }],
    banDays: [{ required: true, message: '请选择处理天数', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);
onMounted(() => {
  getList();
  nextTick(() => {
    autoTableHeight.value = proxy?.autoTableHeight();
  });
  window.onresize = () => {
    autoTableHeight.value = proxy?.autoTableHeight();
  };
});
/** 查询C端用户信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDyUser(queryParams.value);
  dyUserList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  dyUserFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: DyUserVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: DyUserVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getDyUser(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '封禁用户';
};

/** 提交按钮 */
const submitForm = () => {
  dyUserFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateDyUser(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addDyUser(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: DyUserVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除C端用户信息编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delDyUser(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};
// 列表 操作项
//打开封禁弹层
const openbanned = async (row: DyUserVO) => {
  dialog.title = `封禁【${row.nickName}】`;
  dialog.visible = true;
  dialog.rowData = row;
};
/**
 * 封禁用户
 */
const bannedUser = async () => {
  dyUserFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      form.value.id = dialog.rowData.id;
      await updateDyUser(form.value).finally(() => (buttonLoading.value = false));
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};
</script>
<style scoped lang="scss">
.el-table .cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.inputWidth {
  width: 130px;
}
.el-card :deep(.el-card__body) {
  padding-bottom: 0px !important;
}
#table_page {
  height: 50px !important;
  margin-top: 10px !important;
  padding-bottom: 10px !important;
}
</style>
