<template>
  <div class="app-container">
    <el-card shadow="never" body-class="search-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="租户编号" prop="tenantId">
          <el-input
            v-model="queryParams.tenantId"
            placeholder="请输入租户编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="统计小时" prop="statHour">
          <el-input
            v-model="queryParams.statHour"
            placeholder="请输入统计小时"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="成功次数" prop="successCount">
          <el-input
            v-model="queryParams.successCount"
            placeholder="请输入成功次数"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="失败次数" prop="failedCount">
          <el-input
            v-model="queryParams.failedCount"
            placeholder="请输入失败次数"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="刷新次数" prop="refreshCount">
          <el-input
            v-model="queryParams.refreshCount"
            placeholder="请输入刷新次数"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="吊销次数" prop="revokeCount">
          <el-input
            v-model="queryParams.revokeCount"
            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-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" class="mt10">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['auth:statistics:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['auth:statistics:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['auth:statistics:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['auth:statistics:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="统计ID" align="center" prop="statId" />
      <el-table-column label="租户编号" align="center" prop="tenantId" />
      <el-table-column label="统计日期" align="center" prop="statDate" />
      <el-table-column label="统计小时" align="center" prop="statHour" />
      <el-table-column label="登录类型" align="center" prop="loginType" />
      <el-table-column label="成功次数" align="center" prop="successCount" />
      <el-table-column label="失败次数" align="center" prop="failedCount" />
      <el-table-column label="刷新次数" align="center" prop="refreshCount" />
      <el-table-column label="吊销次数" align="center" prop="revokeCount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['auth:statistics:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['auth:statistics:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改登录统计对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="statisticsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户编号" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户编号" />
        </el-form-item>
        <el-form-item label="统计小时" prop="statHour">
          <el-input v-model="form.statHour" placeholder="请输入统计小时" />
        </el-form-item>
        <el-form-item label="登录类型" prop="loginType">
          <el-select v-model="form.loginType" multiple filterable remote reserve-keyword remote-show-suffix
            placeholder="请选择登录类型"
            :remote-method="remoteMethodLoginType"
            :loading="loadingLoginType"
          >
            <el-option v-for="item in optionsLoginType" :key="item.value"
              :label="item.label" :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="成功次数" prop="successCount">
          <el-input v-model="form.successCount" placeholder="请输入成功次数" />
        </el-form-item>
        <el-form-item label="失败次数" prop="failedCount">
          <el-input v-model="form.failedCount" placeholder="请输入失败次数" />
        </el-form-item>
        <el-form-item label="刷新次数" prop="refreshCount">
          <el-input v-model="form.refreshCount" placeholder="请输入刷新次数" />
        </el-form-item>
        <el-form-item label="吊销次数" prop="revokeCount">
          <el-input v-model="form.revokeCount" placeholder="请输入吊销次数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Statistics">
import { listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics } from "@/api/auth/statistics";

const { proxy } = getCurrentInstance();

const statisticsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tenantId: null,
    statDate: null,
    statHour: null,
    loginType: null,
    successCount: null,
    failedCount: null,
    refreshCount: null,
    revokeCount: null,
  },
  rules: {
    statDate: [
      { required: true, message: "统计日期不能为空", trigger: "blur" }
    ],
    loginType: [
      { required: true, message: "登录类型不能为空", trigger: "change" }
    ],
    successCount: [
      { required: true, message: "成功次数不能为空", trigger: "blur" }
    ],
    failedCount: [
      { required: true, message: "失败次数不能为空", trigger: "blur" }
    ],
    refreshCount: [
      { required: true, message: "刷新次数不能为空", trigger: "blur" }
    ],
    revokeCount: [
      { required: true, message: "吊销次数不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询登录统计列表 */
function getList() {
  loading.value = true;
  listStatistics(queryParams.value).then(response => {
    statisticsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    statId: null,
    tenantId: null,
    statDate: null,
    statHour: null,
    loginType: null,
    successCount: null,
    failedCount: null,
    refreshCount: null,
    revokeCount: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("statisticsRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.statId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加登录统计";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _statId = row.statId || ids.value
  getStatistics(_statId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改登录统计";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["statisticsRef"].validate(valid => {
    if (valid) {
      if (form.value.statId != null) {
        updateStatistics(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStatistics(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _statIds = row.statId || ids.value;
  proxy.$modal.confirm('是否确认删除登录统计编号为"' + _statIds + '"的数据项？').then(function() {
    return delStatistics(_statIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('auth/statistics/export', {
    ...queryParams.value
  }, `statistics_${new Date().getTime()}.xlsx`)
}

getList();
</script>
