<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]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="用户昵称" prop="contentId">
              <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label=" 状态">
              <el-select v-model="queryParams.handlingStatus" placeholder="请选择状态" clearable>
                <el-option label="未处理" value="0" />
                <el-option label="已处理" value="1" />
              </el-select>
            </el-form-item>
            <el-form-item label="性别">
              <el-select 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="会员等级">
              <el-select v-model="queryParams.levelId" placeholder="请选择状态" clearable>
                <el-option v-for="dict in dy_banned_handling_status" :key="dict.id" :label="dict.levelName" :value="dict.id" />
              </el-select>
            </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-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-radio-group v-model="queryParams.type" size="large" @change="handleQuery">
            <el-radio-button v-for="item in tablist" :key="item.value" :label="item.name" :value="item.value" />
          </el-radio-group>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="bannedList">
        <el-table-column v-if="queryParams.type == 1" label="笔记名称" align="center" prop="contentId">
          <template #default="scope">
            <el-button type="primary" link :disabled="scope.row.handlingStatus == 1">{{ scope.row.contentName }}</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="queryParams.type == 2" label="活动名称" align="center" prop="contentId">
          <template #default="scope">
            <el-button type="primary" link :disabled="scope.row.handlingStatus == 1">{{ scope.row.contentName }}</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="queryParams.type == 3" label="评论内容" align="center" prop="contentId">
          <template #default="scope">
            <el-button type="primary" link :disabled="scope.row.handlingStatus == 1">{{ scope.row.contentName }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="用户ID" align="center" prop="reportUserId" />
        <el-table-column label="会员等级" align="center" prop="userLevel">
          <template #default="scope">
            <div>{{ formatlevel(scope.row.levelId) }}</div>
          </template>
        </el-table-column>
        <el-table-column 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="avatar" />
        <el-table-column label="举报理由" align="center" prop="reportReason" width="150px" show-overflow-tooltip />
        <el-table-column label="举报人" align="center" prop="informerUserName" />
        <el-table-column label="举报时间" align="center" prop="createTime" width="200px" />
        <el-table-column label="操作时间" align="center" prop="updateTime" width="200px" />
        <el-table-column label="操作人" align="center" prop="updateName" width="150px" />
        <el-table-column label="用户状态" align="center" prop="status" width="150px">
          <template #default="scope">
            <el-tag v-if="scope.row.status == 1" type="info">禁言</el-tag>
            <el-tag v-if="scope.row.status == 2" type="warning">封禁</el-tag>
            <el-tag v-if="scope.row.status == 0" type="primary">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="handlingStatus">
          <template #default="scope">
            <el-tag v-if="scope.row.handlingStatus == 0" type="info">未处理</el-tag>
            <el-tag v-if="scope.row.handlingStatus == 1" type="warning">已处理</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px">
          <template #default="scope">
            <el-button v-hasPermi="['manage:report:edit']" link type="primary" :disabled="scope.row.status != 0" @click="handleUpdate(scope.row)"
              >封禁</el-button
            >
            <el-button
              v-if="queryParams.type == 1"
              v-hasPermi="['manage:report:remove']"
              link
              type="primary"
              :disabled="scope.row.handlingStatus == 1"
              @click="handleDelete(scope.row)"
              >删除笔记</el-button
            >
            <el-button
              v-if="queryParams.type == 2"
              v-hasPermi="['manage:report:remove']"
              link
              type="primary"
              :disabled="scope.row.handlingStatus == 1"
              @click="handleDelete(scope.row)"
              >删除活动</el-button
            >
            <el-button
              v-if="queryParams.type == 3"
              v-hasPermi="['manage:report:remove']"
              link
              type="primary"
              :disabled="scope.row.handlingStatus == 1"
              @click="handleDelete(scope.row)"
              >删除评论</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改举报对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="bannedFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="封禁原因" prop="reason">
          <el-select v-model="form.reason" placeholder="请选择封禁理由" clearable>
            <el-option v-for="dict in dy_banned_ban_reason" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理方式" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio value="1" size="large">禁言</el-radio>
            <el-radio value="2" size="large">封禁</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理天数" prop="banDays">
          <el-select v-model="form.banDays" placeholder="请选择封禁天数" clearable>
            <el-option v-for="dict in dy_user_ban_days" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Banned" lang="ts">
import { listBanned, getBanned, delBanned, addBanned, updatemanage, userLevelall } from '@/api/manage/banned';
import { BannedVO, BannedQuery, BannedForm } from '@/api/manage/banned/types';
import { delNotebook } from '@/api/manage/notebook';

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

const bannedList = ref<BannedVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const dy_banned_handling_status = ref([]);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const radio1 = ref('笔记');
const tablist = ref([
  { value: 1, name: '笔记' },
  { value: 2, name: '活动' },
  { value: 3, name: '评论' }
]);
const queryFormRef = ref<ElFormInstance>();
const bannedFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BannedForm = {
  id: undefined,
  contentId: undefined,
  reportUserId: undefined,
  reportReason: undefined,
  reportDetails: undefined,
  informerUserId: undefined,
  banReason: undefined,
  banDays: undefined,
  handlingMethod: undefined,
  handlingStatus: undefined,
  type: 1
};
const data = reactive<PageData<BannedForm, BannedQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nickName: undefined,
    contentId: undefined,
    reportUserId: undefined,
    reportReason: undefined,
    reportDetails: undefined,
    informerUserId: undefined,
    banReason: undefined,
    banDays: undefined,
    handlingMethod: undefined,
    handlingStatus: undefined,
    type: 1,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '唯一标识ID不能为空', trigger: 'blur' }],
    contentId: [{ required: true, message: '被举报内容id不能为空', trigger: 'blur' }],
    reportUserId: [{ required: true, message: '被举报用户ID不能为空', trigger: 'blur' }],
    reason: [{ required: true, message: '封禁理由不能为空', trigger: 'blur' }],
    reportDetails: [{ required: true, message: '举报详细信息不能为空', trigger: 'blur' }],
    informerUserId: [{ required: true, message: '举报人id不能为空', trigger: 'blur' }],
    banReason: [{ required: true, message: '封禁理由不能为空', trigger: 'blur' }],
    banDays: [{ required: true, message: '封禁天数不能为空', trigger: 'blur' }],
    handlingMethod: [{ required: true, message: '处理方式(0=未处理,1=已下架)不能为空', trigger: 'change' }],
    status: [{ required: true, message: '处理方式不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询举报列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBanned(queryParams.value);
  bannedList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.nickName = '';
  queryParams.value.sex = '';
  queryParams.value.handlingStatus = '';
  queryParams.value.levelId = '';
  handleQuery();
};

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加举报';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: BannedVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  // const res = await getBanned(_id);
  // Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = `封禁【${row.nickName}】`;
};

/** 提交按钮 */
const submitForm = () => {
  bannedFormRef.value?.validate(async (valid: boolean) => {
    const arr = {
      ...form.value,
      type: queryParams.value.type,
      message: 0
    }
    if (valid) {
      buttonLoading.value = true;
      await updatemanage(form.value).finally(() => (buttonLoading.value = false));
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: BannedVO) => {
  const _ids = row?.contentId;
  const _idsname = row?.contentName;
  const arr = {
    id: row?.contentId,
    type: queryParams.value.type,
    handlingStatus: 1,
    message: 1
  };
  const text = queryParams.value.type == 1 ? '笔记名称为“' : queryParams.value.type == 2 ? '活动名称为“' : '评论内容为“';
  await proxy?.$modal.confirm('是否确认删除' + text + _idsname + '"的数据项？').finally(() => (loading.value = false));
  await updatemanage(arr);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'manage/banned/export',
    {
      ...queryParams.value
    },
    `banned_${new Date().getTime()}.xlsx`
  );
};
// 等级列表
const getuserLevelall = async () => {
  const res = await userLevelall();
  dy_banned_handling_status.value = res;
  console.log(res, 'res');
};
// 等级格式化
const formatlevel = (tagId: string | null) => {
  let tagString = '';
  for (let i = 0; i < dy_banned_handling_status.value.length; i++) {
    const element = dy_banned_handling_status.value[i];
    if (tagId == element.id) {
      tagString = element.levelName;
    }
  }
  return tagString;
};

onMounted(() => {
  getList();
  getuserLevelall();
});
</script>
