<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="APP账号" prop="username">
              <el-input v-model="queryParams.username" placeholder="请输入APP账号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="账号状态" prop="enabled">
              <el-select v-model="queryParams.enabled" clearable placeholder="账号状态" style="width: 120px"
                class="filter-item">
                <el-option label="启用" :value="true" />
                <el-option label="禁用" :value="false" />
              </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="hover">
      <el-table ref="operLogTableRef" v-loading="loading" :data="operlogList" border>
        <el-table-column label="APP账号" align="center" prop="userName" />
        <el-table-column label="账号状态" align="center" prop="status">
          <template #default="scope">
            <el-switch v-model="scope.row.status" active-color="#409EFF" inactive-color="#F56C6C" />
          </template>
          <!-- @change="handleStatusChange(scope.row)"  -->
        </el-table-column>

        <el-table-column label="创建时间" align="center" prop="loginDate" width="180">
          <template #default="scope">
            <span>{{ proxy.parseTime(scope.row.loginDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template #default="scope">
            <el-tooltip content="查看绑定设备" placement="top">
              <el-button link type="primary" icon="View" @click="handleView(scope.row)"> </el-button>
            </el-tooltip>
          </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>
    <!-- 操作日志详细 -->
    <userInfoDialog ref="userInfoDialogRef" />
  </div>
</template>

<script setup name="userApp" lang="ts">
import api from '@/api/userApp';
import { OperLogQuery } from '@/api/monitor/operlog/types';
import UserInfoDialog from './user-info-dialog.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const operlogList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<number | string>>([]);
const total = ref(0);
const userInfoDialogRef = ref<ElTableInstance>();
const queryFormRef = ref<ElFormInstance>();

const data = reactive<PageData<OperLogForm, OperLogQuery>>({
  form: {
    operId: undefined,
    tenantId: undefined,
    title: '',
    businessType: 0,
    businessTypes: undefined,
    method: '',
    requestMethod: '',
    operatorType: 0,
    operName: '',
    deptName: '',
    operUrl: '',
    operIp: '',
    operLocation: '',
    operParam: '',
    jsonResult: '',
    status: 0,
    errorMsg: '',
    operTime: '',
    costTime: 0
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: '',
    enabled: ''
  },
  rules: {}
});

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

/** 查询登录日志 */
const getList = async () => {
  loading.value = true;
  const res = await api.userList(queryParams.value);
  operlogList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
};
/** 详细按钮操作 */
const handleView = (row: any) => {
  userInfoDialogRef.value.openDialog(row);
};
const handleStatusChange = (row) => {
  const text = row.enabled ? '启用' : '禁用'
  proxy?.$modal.confirm('此操作将 "' + text + '" ' + row.username + ', 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    api.edit(row).then(res => {
      proxy?.$modal.msgSuccess(text + '成功')
    }).catch(() => {
      row.enabled = !row.enabled
    })
  }).catch(() => {
    row.enabled = !row.enabled
  })
};
  onMounted(() => {
    getList();
  });
</script>
