<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item class="section-title" label="租户管理" />
      <el-form-item label="租户名称：" prop="orgName">
        <el-input v-model="queryParams.orgName" placeholder="请输入租户名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['business:tenant:add']">新增</el-button>
      <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['business:tenant:edit']">编辑</el-button>
      <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:tenant:remove']">删除</el-button>
      <!-- <el-button type="warning" icon="Download" @click="handleExport" v-hasPermi="['business:tenant:export']">导出</el-button> -->
    </div>

    <!-- 列表区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table v-loading="loading" :data="tenantList" height="100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="40" align="center" />
          <el-table-column label="Logo" align="center" width="100">
            <template #default="scope">
              <image-preview style="width: 60px; height: 60px" :src="scope.row.logo" />
            </template>
          </el-table-column>
          <el-table-column label="企业名称" align="center" prop="orgName" />
          <el-table-column label="统一社会信用编码" align="center" prop="orgCrtCode" width="180" />
          <el-table-column label="企业法人" align="center" prop="legalName" width="100" />
          <el-table-column label="企业性质" align="center" prop="orgNature" width="100">
            <template #default="scope">
              <dict-tag :options="org_nature" :value="scope.row.orgNature" />
            </template>
          </el-table-column>
          <el-table-column label="详细地址" align="center" prop="orgAddr" />
          <el-table-column label="操作" align="center" width="100">
            <template #default="scope">
              <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)" v-hasPermi="['business:tenant:edit']">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 分页 -->
    <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>
  </div>
</template>

<script setup name="Tenant">
import tenantForm from './form.vue'
import { listTenant, delTenant } from '@/api/business/tenant'
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 queryParams = ref({
  pageNum: 1,
  pageSize: 10
})

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

/** 查询【请填写功能名称】列表 */
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`
  )
}

onMounted(() => {
  getList()
})
</script>
