<template>
  <div class="app-container">
    <el-form
      v-if="is_super"
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="所属站点" prop="tenant_id">
        <SiteOptions v-model="queryParams" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @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="el-icon-plus"
          size="mini"
          @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="link"
        label="链接"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="area"
        label="站点"
        align="center"
      ></el-table-column>
      <el-table-column prop="updated_at" label="更新时间" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updated_at) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.parent_id == ''"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            >新增子导航</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改导航对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属站点:" prop="tenant_id">
          <SiteOptions v-model="form" />
        </el-form-item>
        <el-form-item label="上级导航" prop="parent_id">
          <treeselect
            v-model="form.parent_id"
            :options="menuOptions"
            :normalizer="normalizer"
            :show-count="true"
            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="link">
          <el-input v-model="form.link" 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>
import {
  navigationList,
  navigationAdd,
  navigationEdit,
  navigationDelete,
} from "@/api/website/siteSet";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: { Treeselect },
  data() {
    return {
      is_super: this.$store.getters.is_super,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 导航表格树数据
      dataList: [],
      // 导航树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        tenant_id: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tenant_id: [
          {
            required: true,
            message: "所属站点不能为空",
            trigger: ["blur", "change"],
          },
        ],
        title: [
          { required: true, message: "导航名称不能为空", trigger: "blur" },
        ],
        link: [
          { required: true, message: "导航链接不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询导航列表 */
    getList() {
      this.loading = true;
      navigationList(this.queryParams).then((response) => {
        this.getTreeselect(response.data);
        this.dataList = response.data;
        this.loading = false;
      });
    },
    /** 转换导航数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children,
      };
    },
    /** 查询导航下拉树结构 */
    getTreeselect(options) {
      const arr = JSON.parse(JSON.stringify(options));
      arr.forEach((item) => {
        delete item.children;
      });
      this.menuOptions = [];
      const menu = { id: "", title: "主类目", children: [] };
      menu.children = arr;
      this.menuOptions.push(menu);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tenant_id: undefined,
        parent_id: undefined,
        title: undefined,
        link: undefined,
        status: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != null && row.parent_id == "") {
        this.form.parent_id = row.id;
      } else {
        this.form.parent_id = "";
      }
      this.open = true;
      this.title = "添加导航";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = Object.assign({}, row);
      this.open = true;
      this.title = "修改导航";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            navigationEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            navigationAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除编号为"' + row.id + '"的数据项？')
        .then(function () {
          return navigationDelete({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
