<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="status">
              <el-select v-model="queryParams.status" clearable placeholder="账号状态"
                class="filter-item">
                <el-option label="启用" :value="0" />
                <el-option label="禁用" :value="1" />
              </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">
            <div @click="handleStatusChange(scope.row)">
              <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ proxy.parseTime(scope.row.createTime) }}</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 { userQuery, userForm } from '@/api/userApp/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 total = ref(0);
const userInfoDialogRef = ref<ElTableInstance>();
const queryFormRef = ref<ElFormInstance>();

const data = reactive<PageData<userForm, userQuery>>({
  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: '',
    status: ''
  },
  rules: {}
});

const { queryParams } = 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();
  handleQuery()
};
/** 详细按钮操作 */
const handleView = (row: any) => {
  userInfoDialogRef.value.openDialog(row);
};
const handleStatusChange = (row: any) => {
  const text = row.status == 0 ? '启用' : '禁用'
  proxy?.$modal.confirm('此操作将 "' + text + '" ' + row.userName + ', 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    api.userStatus(row).then(res => {
      if (res.code == 200) {
        proxy?.$modal.msgSuccess(text + '成功')
        getList();
      }
    }).catch(() => {
    getList();
    })
  }).catch(() => {
  getList();
  })
};
onMounted(() => {
  getList();
});
</script>
