<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="所属站点" prop="tenantId">
        <SiteOptions v-model="queryParams.tenantId" :site-list="siteList" />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="search"
          size="default"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="default" @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
          icon="plus"
          size="default"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" prop="id" align="center" />
      <el-table-column label="导航名称" prop="title" width="120" />
      <el-table-column label="所属站点" align="center">
        <template #default="{ row }">
          {{ siteList.find((el) => el.id === row.tenantId)?.name ?? "无" }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="{ row }">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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="title" v-model="showEditDialog" width="500px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属站点" prop="tenantId">
          <SiteOptions v-model="form.tenantId" :site-list="siteList" />
          <!-- <el-select
            v-model="form.tenantId"
            placeholder="请选择，支持搜索"
            size="default"
            filterable
          >
            <el-option
              v-for="item in siteList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select> -->
        </el-form-item>
        <el-form-item label="导航名称:" prop="title">
          <el-input v-model="form.title" placeholder="请输入导航名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { reactive, ref, toRefs } from "vue";
import {
  navigationList,
  navigationAdd,
  navigationEdit,
  navigationDelete,
} from "@/api/website/innovate";
import { cloneDeep } from "lodash";
import { ElMessage, ElMessageBox } from "element-plus";
import { tenantSelect } from "@/api/subPlatform/tenant";
import SiteOptions from "@/views/components/SiteOptions";

const queryFormRef = ref(null);
const dataList = ref([]);
const loading = ref(true);
const total = ref(0);
const showEditDialog = ref(false);
const formRef = ref(null);
const siteList = ref([]); // 站点列表
const showSearch = ref(true);
const title = ref("");

const data = reactive({
  queryParams: {
    title: undefined,
    tenantId: undefined,
    pageNum: 1,
    pageSize: 10,
  },
  form: {},
  rules: {
    title: [{ required: true, message: "导航名称不能为空", trigger: "blur" }],
    tenantId: [
      {
        required: true,
        message: "所属站点不能为空",
        trigger: ["blur", "change"],
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

const resetQuery = () => {
  if (queryFormRef.value) {
    queryFormRef.value.resetFields();
  }
  handleQuery();
};

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

// 获取数据列表
const getList = async () => {
  loading.value = true;
  const resp = await navigationList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};

// 获取站点列表
const getSiteList = async () => {
  const { rows } = await tenantSelect();
  siteList.value = rows;
};

// 取消按钮
const cancel = () => {
  showEditDialog.value = false;
  //   this.memberOpen = false;
  reset();
};
// 表单重置
const reset = () => {
  form.value = {
    id: undefined,
    tenantId: undefined,
    title: undefined,
  };
  if (formRef.value) {
    formRef.value.resetFields();
  }
};

// 处理添加
const handleAdd = () => {
  reset();
  showEditDialog.value = true;
  title.value = "新增";
};

// 处理更新
const handleUpdate = (row) => {
  reset();
  form.value = cloneDeep(row);
  showEditDialog.value = true;
  title.value = "修改";
};

// 提交表单
const submitForm = async () => {
  await formRef.value.validate();
  if (form.value.id != undefined) {
    await navigationEdit(form.value);
    ElMessage.success("修改成功");
    showEditDialog.value = false;
    getList();
  } else {
    await navigationAdd(form.value);
    ElMessage.success("新增成功");
    showEditDialog.value = false;
    getList();
  }
};

/** 删除按钮操作 */
const handleDelete = (row) => {
  ElMessageBox.confirm(`是否确认删除名为${row.title}的数据项？`)
    .then(async () => {
      await navigationDelete(row.id);
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};

getList();
getSiteList();
</script>
