<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-select
          v-model="queryParams.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>
        <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>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="dataList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="title" label="导航名称"></el-table-column>
      <el-table-column prop="area" 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.updateTime }}</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="edit"
            @click="handleUpdate(row)"
            >修改</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="delete"
            @click="handleDelete(row)"
            >删除</el-button
          >
          <el-button
            v-if="row.parentId == ''"
            size="small"
            type="text"
            icon="plus"
            @click="handleAdd(row)"
            >新增子导航</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改导航对话框 -->
    <el-dialog
      :title="title"
      v-model="showEditDialog"
      destroy-on-close
      width="680px"
      append-to-body
    >
      <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-form-item>
        <el-form-item label="上级导航" prop="parentId">
          <TreeSelect
            :objMap="{
              value: 'id', // ID字段名
              label: 'title', // 显示名称
              children: 'children', // 子级字段名
            }"
            :options="menuOptions"
            v-model:value="form.parentId"
            placeholder="选择上级领域"
          />
        </el-form-item>
        <el-form-item label="导航名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入导航名称" />
        </el-form-item>
        <el-form-item label="内容:" prop="content">
          <WangEditor v-model="form.content" minHeight="150px" />
        </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 name="AboutUs">
import {
  aboutList,
  aboutAdd,
  aboutDetail,
  aboutEdit,
  aboutDelete,
} from "@/api/website/about";
import { cloneDeep } from "lodash";
import { tenantSelect } from "@/api/subPlatform/tenant";
import { ElMessage, ElMessageBox } from "element-plus";
import WangEditor from "@/components/WangEditor/index.vue";
import SiteOptions from "@/views/components/SiteOptions";

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

const data = reactive({
  // 查询参数
  queryParams: {
    tenantId: undefined,
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {
    tenantId: [
      {
        required: true,
        message: "所属站点不能为空",
        trigger: ["blur", "change"],
      },
    ],
    title: [{ required: true, message: "导航名称不能为空", trigger: "blur" }],
    content: [
      {
        required: true,
        message: "内容不能为空",
        trigger: "blur",
      },
    ],
  },
});
const { queryParams, form, rules } = toRefs(data);

/** 查询导航列表 */
const getList = async () => {
  loading.value = true;
  const resp = await aboutList(queryParams.value);
  getTreeselect(resp.data);
  // console.log(resp);
  dataList.value = resp.data;
  loading.value = false;
};

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

/** 转换导航数据结构 */
const normalizer = (node) => {
  if (node.children && !node.children.length) {
    delete node.children;
  }
  return {
    id: node.id,
    label: node.title,
    children: node.children,
  };
};

/** 查询导航下拉树结构 */
const getTreeselect = (options) => {
  const arr = cloneDeep(options);
  // const arr = JSON.parse(JSON.stringify(options));
  arr.forEach((item) => {
    delete item.children;
  });
  menuOptions.value = [];
  const menu = { id: 0, title: "主类目", children: [] };
  menu.children = arr;
  menuOptions.value.push(menu);
};

// 取消按钮
const cancel = () => {
  showEditDialog.value = false;
  reset();
};

// 表单重置
const reset = () => {
  form.value = {
    tenantId: undefined,
    parentId: undefined,
    title: undefined,
    content: undefined,
  };
  if (formRef.value) {
    formRef.value.resetFields();
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  if (queryFormRef.value) {
    queryFormRef.value.resetFields();
  }
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = (row) => {
  reset();
  if (row != null && row.parentId == "") {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = "";
  }
  showEditDialog.value = true;
  title.value = "添加导航";
};

/** 修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const resp = await aboutDetail(row.id);
  form.value = resp.data;
  showEditDialog.value = true;
  title.value = "修改导航";
};

/** 提交按钮 */
const submitForm = async () => {
  await formRef.value.validate();
  if (form.value.id) {
    await aboutEdit(form.value);
    ElMessage.success("修改成功");
    showEditDialog.value = false;
    getList();
  } else {
    console.log(form.value);
    await aboutAdd(form.value);
    ElMessage.success("新增成功");
    showEditDialog.value = false;
    getList();
  }
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  ElMessageBox.confirm(`是否确认删除编号为${row.id}的数据项？`)
    .then(async () => {
      await aboutDelete(row.id);
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};
getList();
getSiteList();
</script>

<!-- <script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import { reactive, toRefs } from "vue";

export default {
  components: { Treeselect, IconSelect },
  data() {
    return {
      is_super: this.$store.getters.is_super,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 导航表格树数据
      dataList: [],
      // 导航树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
    };
  },
  created() {
    this.getList();
  },
  methods: {},
};
</script> -->
