<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.trim="queryParams.orgName" placeholder="请输入企业名称" clearable @keyup.enter="handleQuery" @keydown.enter.prevent style="width: 150px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="Plus" @click="handleShowFormDialog(null)" v-hasPermi="['business:tenant:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table v-loading="loading" :data="tenantList" height="100%">
          <el-table-column label="Logo" align="center" width="200">
            <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" width="140">
            <template #default="scope">
              <el-button link type="warning" icon="Edit" @click="handleShowFormDialog(scope.row.id)" v-hasPermi="['business:tenant:edit']">
                编辑
              </el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:tenant:remove']">删除</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" @handleSaveSuccess="getList" />
  </div>
</template>

<script setup name="Tenant">
import tenantForm from './form.vue'
import { listTenant, delTenant } from '@/api/business/tenant'
const { proxy } = getCurrentInstance()
/**厂别变量 */
const tenantList = ref([])
const loading = 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 handleShowFormDialog(id) {
  proxy.$refs.tenantRef.open(id)
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('确定删除选中项？')
    .then(() => {
      return delTenant(row.id)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功！')
    })
    .catch(() => {})
}

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