<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" size="small" icon="Plus" @click="handleAdd"
        v-hasPermi="['business:tenant:add']">新增</el-button>
      <el-button type="warning" size="small" icon="Edit" :disabled="single" @click="handleUpdate"
        v-hasPermi="['business:tenant:edit']">修改</el-button>
      <!-- <el-button type="danger" size="small" icon="Delete" :disabled="multiple" @click="handleDelete"
                v-hasPermi="['business:tenant:remove']">删除</el-button>
            <el-button type="warning" size="small" icon="Download" @click="handleExport"
                v-hasPermi="['business:tenant:export']">导出</el-button> -->
    </div>
    <!-- 搜索区 -->
    <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true"
      label-width="68px">
      <el-form-item label="公司名称：" prop="companyName">
        <el-input v-model="queryParams.companyName" 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-table v-loading="loading" :data="tenantList" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="系统简称" align="center" prop="accountName" width="180" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag size="small" :type="checkStatusType(scope.row.status)">{{
            checkStatus(scope.row.status)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:tenant:edit']">查看</el-button>
          <el-button link type="primary" size="small" @click="handleInit(scope.row, 2)"
            v-hasPermi="['business:tenant:edit']">初始化系统</el-button>
          <el-button link type="primary" size="small" @click="handleGetIn(scope.row)"
            v-hasPermi="['business:tenant:getIn']">进入系统</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" />

    <!-- 表单 -->
    <tenant-form ref="tenantRef" :get-list="getList"></tenant-form>
    <tenant-init ref="tenantInitRef" :get-list="getList"></tenant-init>
  </div>
</template>

<script setup name="Tenant">
import { setTenant } from "@/utils/auth";
import tenantForm from "./form.vue";
import router from "@/router";
import { isHttp } from "@/utils/validate";
import tenantInit from "./init.vue";
import { listTenant, delTenant } from "@/api/business/tenant";
import usePermissionStore from "@/store/modules/permission";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { org_nature } = proxy.useDict("org_nature");
/** 查询 对象 */
const tenantList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const tenantInitRef = ref(null);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  orderByColumn: "create_time",
});

/***********************  方法区  ****************************/

/** 查询【请填写功能名称】列表 */
function getList() {
  loading.value = true;
  listTenant(queryParams.value).then((response) => {
    tenantList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs.tenantRef.open();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids.value;
  proxy.$refs.tenantRef.open(id);
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除选中的数据项？")
    .then(function () {
      return delTenant(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功！");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/tenant/export",
    {
      ...queryParams.value,
    },
    `tenant_${new Date().getTime()}.xlsx`
  );
}
function checkStatusType(status) {
  switch (status) {
    case 0:
    case 1:
      return "warning";
    case 2:
      return "success";
    case 4:
      return "danger";
    default:
      return "";
  }
}
function checkStatus(status) {
  switch (status) {
    case 0:
    case 1:
      return "待审核";
    case 2:
      return "使用中";
    case 4:
      return "已停用";
    default:
      return "";
  }
}
function handleInit(row) {
  const id = row.id || ids.value;
  proxy.$refs["tenantInitRef"].open(row.id);
}
function handleGetIn(row) {
  const tenantId = row.id;
  setTenant(tenantId);
  usePermissionStore()
    .generateRoutes()
    .then((accessRoutes) => {
      // router.reload({ path: '/index' })
      location.reload();
    });
}
getList();
</script>
