<template>
  <div class="app-container enterprise">
    <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">
        <template #default="{row}">
          {{ row.province }} {{ row.city }} {{ row.region }} {{ row.address }}
        </template>
      </el-table-column>
      <el-table-column label="企业介绍" prop="descr" />
      <el-table-column label="logo" prop="logo">
        <template #default="{row}">
          <el-image
            style="width: 50px;height: 50px;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,
                regionCode: row.regionCode,
                area: [row.province,row.city,row.region],
                address: row.address,
                descr: row.descr,
                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="480px" append-to-body :close-on-click-modal="false">
      <el-form label-width="100px" style="padding-right: 15px;" ref="addRef" :model="openForm" :rules="rules">
        <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="area">
            <el-cascader
                v-model="openForm.area"
                :options="areaList"
                @change="(e)=>{console.log(e)}"
                style="width: 100%"
                :props="{ value: 'label' }"
            />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="openForm.address" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="企业介绍" prop="descr">
          <el-input v-model="openForm.descr" placeholder="请输入" type="textarea" rows="3"/>
        </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: 100px;display: block;margin-top: 10px;"
                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 @click="open = false">取 消</el-button>
          <el-button type="primary" @click="submit">提 交</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";
import { areaList,getAreaText } from '@/utils/area.js'
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: '',
})
const rules = ref({
  name: [{ required: true, message: "请输入企业名称", trigger: "blur" }],
  regionCode: [{ required: true, message: "请输入邮政编码", trigger: "blur" }],
  area: [{ required: true, message: "请选择地址", trigger: "blur" }],
  address: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
  descr: [{ required: true, message: "请输入企业介绍", trigger: "blur" }],
  logo: [{ required: true, message: "请上传logo", trigger: "blur" }]
})
// 上传文件
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() {
  proxy.$refs.addRef.validate(valid => {
    if (valid){
      const submitForm = {...openForm.value}
      submitForm.province = submitForm.area[0]
      submitForm.city = submitForm.area[1]
      submitForm.region = submitForm.area[2]
      delete submitForm.area
      orgSave(submitForm).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>