<template>
  <div class="account-list">
    <el-card shadow="never">
      <el-form :inline="true" :model="filters" class="filter-form">
        <el-form-item :label="$t('label.accountType')">
          <el-select v-model="filters.type" :placeholder="$t('common.all')" clearable style="width:140px">
            <el-option label="管理员" value="Admin" />
            <el-option label="医院端" value="Hospital" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('label.nameAccount')">
          <el-input v-model="filters.keyword" :placeholder="$t('placeholder.nameOrAccount')" clearable style="width:200px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">{{ $t('btn.query') }}</el-button>
          <el-button @click="onReset">{{ $t('btn.reset') }}</el-button>
          <el-button type="success" @click="goAdd">{{ $t('common.add') }}</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="list" v-loading="loading" border stripe>
        <!-- <el-table-column prop="id" label="ID" width="80" /> -->
        <el-table-column :label="$t('table.account')" prop="username" width="140" />
        <el-table-column :label="$t('table.name')" prop="name" width="120" />
        <el-table-column :label="$t('label.accountType')" width="100">
          <template #default="{ row }">
            <el-tag :type="row.userType === 'Admin' ? 'danger' : 'primary'" size="small">
              {{ row.userType === "Admin" ? "管理员" : "医院端" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.role')" prop="role" width="120" />
        <el-table-column :label="$t('table.hospital')" prop="hospitalName" min-width="160" show-overflow-tooltip />
        <el-table-column :label="$t('label.status')" width="100">
          <template #default="{ row }">
            <el-switch
              :model-value="row.status === '启用' ? true : false"
              @change="toggleStatus(row)"
              :loading="row.__loading"
              :active-text="$t('account.enable')"
              :inactive-text="$t('account.disable')"
              inline-prompt
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.action')" width="200" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="goEdit(row.id)">{{ $t('btn.edit') }}</el-button>
            <el-button link type="warning" @click="resetPwd(row)">{{ $t('account.resetPassword') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="page"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="loadList"
          @current-change="loadList"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { useI18n } from "vue-i18n";
import {
  getAdminAccounts,
  toggleAdminAccountStatus,
  resetAdminAccountPassword,
} from "@/service/modular/admin";

const { t: $t } = useI18n();
const router = useRouter();
const list = ref([]);
const total = ref(0);
const page = ref(1);
const pageSize = ref(10);
const loading = ref(false);
const filters = reactive({ type: "", keyword: "" });

const loadList = async () => {
  loading.value = true;
  try {
    const res = await getAdminAccounts({
      page: page.value,
      pageSize: pageSize.value,
      ...filters,
    });
    list.value = res?.list || [];
    total.value = res?.total || 0;
  } finally {
    loading.value = false;
  }
};

const onSearch = () => {
  page.value = 1;
  loadList();
};
const onReset = () => {
  filters.type = "";
  filters.keyword = "";
  onSearch();
};

const goAdd = () => router.push("/admin/accounts/edit");
const goEdit = (id) => router.push(`/admin/accounts/edit/${id}`);

const toggleStatus = async (row) => {
  row.__loading = true;
  try {
    await toggleAdminAccountStatus(row.id);
    ElMessage.success($t('msg.statusChangeSuccess'));
    loadList();
  } finally {
    row.__loading = false;
  }
};

const resetPwd = async (row) => {
  try {
    await ElMessageBox.confirm(
      `${row.username} - ${$t('account.defaultPassword')}`,
      $t('common.confirm'),
      { type: "warning" },
    );
    await resetAdminAccountPassword(row.id);
    ElMessage.success($t('msg.passwordResetSuccess'));
  } catch (_) {
    /* canceled */
  }
};

onMounted(loadList);
</script>

<style scoped lang="scss">
.filter-form {
  margin-bottom: 12px;
}
.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
