<template>
  <div class="admin-page">
    <ILayoutPage ref="pageRef" :config="config">
      <template #listActions="{ type, refresh }">
        <div class="page-header">
          <h2>
            管理员账号列表
          </h2>
          <a-button type="primary" @click="addClick">
            <template #icon><plus-outlined /></template>
            添加
          </a-button>
        </div>
      </template>

      <template #bodyCell="{ column, record, type }">
        <template v-if="column.dataIndex === 'recordDetail'">
          <div>
            <div style="font-size: 14px; color: #333;">
              {{ record.nickname }}
              <a-tag v-if="record.optStatus === 0" style="margin-left: 10px" color="#FF0000">
                <span>停用</span>
              </a-tag>
            </div>
            <div class="item-detail">
              <span class="item-title">账号</span>
              <span class="item-value">{{ record.username }}</span>
            </div>
            <div class="item-detail">
              <span class="item-title">角色</span>
              <span class="item-value">{{ record.optSuper === 1 ? '超级管理员' : '普通管理员' }}</span>
            </div>
            <div v-if="!!record.optRemark" class="item-detail">
              <span class="item-title">备注</span>
              <span class="item-value">{{ record.optRemark }}</span>
            </div>
          </div>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-button size="small" type="default" @click="editClick(record)">
            编辑
          </a-button>
          <IViewConfirm importance="normal" :title="`确认要删除管理员吗？`" @confirm="() => deleteClick(record)">
            <a-button size="small" danger> 删除 </a-button>
          </IViewConfirm>
        </template>
      </template>
    </ILayoutPage>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { showErrorTips, showSuccessTips } from "@/iview/utils/messageTips";
import { apiRequest } from "@/iview/utils/request";
import { PlusOutlined } from "@ant-design/icons-vue";
import ILayoutPage from "@/iview/ILayoutPage.vue";
import IViewConfirm from "@/iview/IViewConfirm.vue";

const pageRef = ref(null);

const refreshList = () => {
  pageRef.value.refreshList();
};

const addClick = () => {
  pageRef.value.showFormPage("create");
};

const editClick = (record) => {
  pageRef.value.showFormPage("update", record);
};

const deleteClick = async (record) => {
  try {
    record.actionLoading = true;
    await apiRequest("api.system.admin.info.delete", {
      guid: record.guid,
    });
    showSuccessTips("管理员删除成功");
    refreshList();
  } catch (error) {
    showErrorTips(error);
  } finally {
    record.actionLoading = false;
  }
};

const recordColumns = ref([
  {
    title: "管理员信息",
    dataIndex: "recordDetail",
    bodySlot: true,
    ellipsis: true,
  },
  {
    width: 160,
    title: "操作",
    dataIndex: "action",
    bodySlot: true,
    fixed: "right",
  },
]);

const formConfig = ref({
  extrasFields: ["guid"],
  fields: [
    {
      name: "username",
      label: "登录账号",
      type: "text",
      required: true,
      minLength: 3,
      maxlength: 20,
    },
    {
      name: "nickname",
      label: "账号昵称",
      type: "text",
      required: true,
      maxlength: 20,
    },
    {
      name: "password",
      label: "登录密码",
      type: "text",
      required: true,
      minLength: 6,
      placeholder: "密码长度至少6个字符",
      show: "!record.guid",
    },
    {
      name: "password",
      label: "重置密码",
      type: "text",
      placeholder: "不修改密码请留空",
      show: "!!record.guid",
    },
    {
      name: "optSuper",
      label: "超级管理员",
      type: "radio",
      options: [
        { value: 1, label: "是" },
        { value: 0, label: "否" },
      ],
      defaultValue: 0,
    },
    {
      name: "optRemark",
      label: "备注信息",
      type: "textarea",
      maxlength: 200,
      textRows: 2,
    },
    {
      name: "optStatus",
      label: "状态开关",
      type: "boolean",
      defaultValue: 1,
      falseValue: 0,
      trueValue: 1,
    },
  ],
});

const config = ref({
  listPages: [
    {
      title: "管理员列表",
      name: "recordList",
      api: "api.system.admin.info.list",
      rowKey: "guid",
      defaultParams: {},
      showFilter: false,
      isDefault: true,
      pagination: {
        pageSize: 10,
        showTotal: (total) => total > 0 ? `共 ${total} 条` : "暂无数据",
      },
      columns: recordColumns.value,
      filters: {
        fields: [],
      },
    },
  ],
  detailPages: [],
  formPages: [
    {
      name: "create",
      title: "添加管理员",
      submitApi: "api.system.admin.info.create",
      formConfig: formConfig.value,
    },
    {
      name: "update",
      title: "编辑管理员",
      paramsKey: ["guid"],
      recordApi: "api.system.admin.info.detail",
      submitApi: "api.system.admin.info.update",
      formConfig: formConfig.value,
    },
  ],
});
</script>

<style scoped>
.admin-page {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 10px;

  h2 {
    margin: 0;
    color: #d1d5db;
    font-size: 16px;
    font-weight: 500;
  }
}

.item-detail {
  color: #999;
  font-size: 12px;
  align-items: flex-start;
  display: flex;
}

.item-title {
  color: rgba(0, 0, 0, 0.45);
  font-size: 12px;
  line-height: 20px;
  margin-right: 8px;
  white-space: nowrap;
}

.item-value {
  color: rgba(0, 0, 0, 0.75);
  font-size: 13px;
  line-height: 20px;
  word-break: break-all;
  min-width: 0;
}
</style>
