<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="企业名称">
        <el-input placeholder="请输入" v-model="queryParams.name" />
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
              type="primary"
              plain
              @click="open = true;openForm = {name: ''};"
              icon="Plus"
          >新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" show-overflow-tooltip style="width: 100%;">
      <el-table-column label="企业名称" prop="name" />
      <el-table-column label="邮政编号" prop="regionCode" />
      <el-table-column label="地址" prop="address" />
      <el-table-column label="logo" prop="logo">
        <template #default="{row}">
          <el-image
            style="width: 80px;height: 80px;display: block;"
            v-if="row.logo"
            :src="row.logo"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"/>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width" >
        <template #default="{row}">
          <el-button link type="primary" icon="Edit" @click="open=true;openForm={
                name: row.name,
                logo: row.logo,
                id: row.id,
            }">编辑</el-button>
          <el-button link type="primary" icon="Delete" @click="del(row)">删除</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" />
    <!-- 添加或修改账号问答框 -->
    <el-dialog title="新增企业" v-model="open" width="400px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="openForm.name" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="邮政编号" prop="regionCode">
          <el-input v-model="openForm.regionCode" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="openForm.address" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="企业LOGO" prop="logo">
          <div>
            <el-button type="primary" @click="$refs.fileInput.click()">上传图片</el-button>
            <el-image
                style="width: 200px;display: block;"
                v-if="openForm.logo"
                :src="openForm.logo"
            />
          </div>
          <input type="file" ref="fileInput" @change="handleFileChange" style="display: none;" accept='image/*' />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submit">提 交</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { orgList,orgDel,orgSave } from "@/api/enterprise";
import { ref } from "vue";
import { uploadFileToOss } from "@/utils/oss";
const { proxy } = getCurrentInstance();
const list = ref([]);
const loading = ref(false);
const total = ref(0);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
})
const open = ref(false);
const openForm = ref({
  name: '',
  logo: '',
})
// 上传文件
function handleFileChange(e) {
  if (e.target.files.length > 0) {
    const file = e.target.files[0];
    uploadFileToOss(file).then((url) => {
      openForm.value.logo = url
    });
  }
  proxy.$refs.fileInput.value = null;
}
/** 查询列表 */
function getList() {
  const form = { ...queryParams.value }
  loading.value = true;
  orgList(form).then(res => {
    list.value = res.data.list;
    total.value = res.data.total;
    loading.value = false;
  });
}
function submit() {
  if (!openForm.value.name) {
    return proxy.$modal.msgError("请输入企业名称");
  }
  orgSave(openForm.value).then(res => {
    proxy.$modal.msgSuccess("操作成功");
    open.value = false;
    getList();
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
  }
  handleQuery();
}
function del(row) {
    proxy.$modal.confirm('您确定要删除吗？').then(function() {
      return orgDel({
        id: row.id,
      });
    }).then((res) => {
      getList();
      proxy.$modal.msgSuccess('删除成功');
    }).catch(() => {});
}
getList();
</script>
<style lang="scss" scoped>
.el-dialog .el-dialog__body {
  max-height: 650px;
}
</style>