<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryFormRef"
        v-show="showSearch"
        :inline="true"
        @submit.native.prevent
    >
      <el-form-item label="导航名称:" prop="categoryId">
        <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
          :showSearch.sync="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 }">
          {{ getCategoryName(row.categoryId) }}
        </template
        >
      </el-table-column>
      <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="edit"
              @click="handleUpdate(row)"
          >编辑
          </el-button
          >
          <el-button
              size="small"
              type="text"
              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"
        destroy-on-close
        width="40%"
    >
      <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="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择">
            <el-option
                v-for="item in categoryList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务标题:" prop="title">
          <el-input v-model="form.title" placeholder="请输入服务标题"/>
        </el-form-item>
        <el-form-item label="服务简述:" prop="description">
          <el-input
              v-model="form.description"
              placeholder="请输入服务简述"
              maxlength="20"
          />
        </el-form-item>
        <el-form-item label="服务描述:" prop="content">
          <!--          <WangEditor v-model="form.content" minHeight="150px"></WangEditor>-->
          <el-input
              type="textarea"
              v-model="form.content"
              placeholder="请输入服务描述"
              :autosize="{ minRows: 6, maxRows: 8 }"
          ></el-input>
        </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="Service">
import {
  serviceList,
  serviceAdd,
  serviceDetail,
  serviceEdit,
  serviceDelete,
  navigationList,
} from "@/api/website/innovate";
import {tenantSelect} from "@/api/subPlatform/tenant";
import WangEditor from "@/components/WangEditor/index.vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {reactive, ref} from "vue";
import SiteOptions from "@/views/components/SiteOptions";

const loading = ref(true); // 是否正在加载
const showSearch = ref(true); // 是否显示搜索栏
const categoryList = ref([]); //导航列表
const queryFormRef = ref(null);
const formRef = ref(null);
const total = ref(0);
const dataList = ref([]);
const siteList = ref([]);

// 弹出层标题
const title = ref("");
// 是否显示弹出层
const showEditDialog = ref(false);

const data = reactive({
  queryParams: {
    categoryId: undefined,
    pageNum: 1,
    pageSize: 10,
  },

  // 表单参数
  form: {},
  rules: {
    title: [{required: true, message: "数据名称不能为空", trigger: "blur"}],
    description: [
      {required: true, message: "服务简述不能为空", trigger: "blur"},
    ],
    content: [{required: true, message: "服务描述不能为空", trigger: "blur"}],
    tenantId: [
      {
        required: true,
        message: "所属站点不能为空",
        trigger: ["blur", "change"],
      },
    ],
    categoryId: [
      {required: true, message: "导航名称不能为空", trigger: "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 serviceList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};

// 获取分类名称
const getCategoryName = (categoryId) => {
  const item = categoryList.value.find((el) => {
    return el.id === categoryId;
  });
  return item ? item.title : "无";
};

// 获取导航列表
const getcategoryList = async () => {
  const resp = await navigationList();
  categoryList.value = resp.rows;
};

// 获取站点列表
const getSiteList = async () => {
  const {rows} = await tenantSelect();
  siteList.value = rows;
};
// 取消按钮
const cancel = () => {
  showEditDialog.value = false;
  // memberOpen.value = false;
  reset();
};

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

const handleAdd = () => {
  reset();
  showEditDialog.value = true;
  title.value = "新增";
};

const handleUpdate = async (row) => {
  reset();
  const resp = await serviceDetail(row.id);
  form.value = resp.data;
  showEditDialog.value = true;
  title.value = "修改";
};

const submitForm = async () => {
  await formRef.value.validate();
  if (form.value.id != undefined) {
    await serviceEdit(form.value);
    ElMessage.success("修改成功");
    showEditDialog.value = false;
    getList();
  } else {
    await serviceAdd(form.value);
    ElMessage.success("新增成功");
    showEditDialog.value = false;
    getList();
  }
};

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

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