<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="场地编号" prop="areaNum">
        <el-input
          v-model="queryForm.areaNum"
          placeholder="请输入场地编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="场地名称" prop="areaName">
        <el-input
          v-model="queryForm.areaName"
          placeholder="请输入场地名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="场地类型" prop="areaType">
        <el-select v-model="queryForm.areaType" style="width: 100%" placeholder="请选择场地类型">
          <el-option
            v-for="dict in dict.type.area_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否开放" prop="areaOpen">
        <el-select v-model="queryForm.areaOpen" style="width: 100%" placeholder="请选择场所开放状态">
          <el-option
            v-for="dict in dict.type.area_open_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </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>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      <el-button
        type="info"
        plain
        icon="el-icon-sort"
        size="mini"
        @click="toggleExpandAll"
      >展开/折叠
      </el-button>
    </el-row>
    <br/>
    <el-table
      v-if="refreshTable"
      :data="areaList"
      v-loading="loading"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="场地编号" align="center" v-if="columns[0].visible" prop="areaNum"></el-table-column>
      <el-table-column label="场地名称" align="center" v-if="columns[1].visible" prop="areaName"></el-table-column>
      <el-table-column label="场地类型" align="center" v-if="columns[2].visible" prop="areaType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.area_type" :value="scope.row.areaType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否开放" align="center" v-if="columns[3].visible" prop="areaOpen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.area_open_state" :value="scope.row.areaOpen"/>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" show-overflow-tooltip v-if="columns[4].visible"
                       prop="areaIntroduce"></el-table-column>
      <el-table-column label="操作" align="center" v-if="columns[5].visible">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:schoolTimeTable:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:schoolTimeTable:remove']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schoolTimeTable:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="cancel">
      <el-form ref="postForm" :model="postForm" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级场地" prop="pid">
              <treeselect
                v-model="postForm.pid"
                :options="areaOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="场地名称" prop="areaName">
              <el-input v-model="postForm.areaName" placeholder="请输入场地名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="场地编号" prop="areaNum">
              <el-input v-model="postForm.areaNum" placeholder="请输入场地编号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="场地类型" prop="areaType">
              <el-select v-model="postForm.areaType" style="width: 100%" placeholder="请选择场地类型">
                <el-option
                  v-for="dict in dict.type.area_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否开放" prop="areaOpen">
              <el-select v-model="postForm.areaOpen" style="width: 100%" placeholder="请选择场所开放状态">
                <el-option
                  v-for="dict in dict.type.area_open_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="场地简介" prop="areaIntroduce">
          <el-input v-model="postForm.areaIntroduce" type="textarea"></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>
import {getAreaList, delArea, addArea, updateArea, getArea} from "@/api/smartSchool/schoolManage/areaManage/index";
import {AreaStatus} from '@/enums/schedule/areaManage'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AreaManage",
  dicts: ['area_type', 'area_open_state'],
  components: {Treeselect},
  data() {
    return {
      // 菜单树选项
      //menuOptions: [],
      // 查询表单
      queryForm: {
        areaNum: '',
        areaName: '',
        areaType: '',
        areaIntroduce: '',
        areaOpen: '',
      },
      // 遮罩层
      loading: false,
      // 场地信息列表
      areaList: [],
      // 菜单树选项
      areaOptions: [],
      // 新增/修改表单
      postForm: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 场地选项
      areaTypeList: [],
      // 校验
      rules: {
        pid: [
          {required: true, message: "上级场地不能为空", trigger: "change"},
        ],
        areaName: [
          {required: true, message: "场地名称不能为空", trigger: "blur"},
        ],
        areaNum: [
          {required: true, message: "场地编号不能为空", trigger: "blur"},
        ],
        areaType: [
          {required: true, message: "场地类型不能为空", trigger: "change"},
        ],
        areaOpen: [
          {required: true, message: "请选择是否开放", trigger: "change"},
        ],
      },
      // 开放状态
      AreaStatus,
      // 列表信息
      columns: [
        {key: 0, label: `场地编号`, visible: true},
        {key: 1, label: `场地名称`, visible: true},
        {key: 2, label: `场地类型`, visible: true},
        {key: 3, label: `是否开放`, visible: true},
        {key: 4, label: `简介`, visible: true},
        {key: 5, label: `操作`, visible: true},
      ],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
    }
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表数据
    getList() {
      this.loading = true; // 加载效果
      getAreaList(this.queryForm).then(response => {
        // const menu = {id: 0, areaName: '东营市第二中学', children: []};
        // menu.children = this.handleTree(response.data, "id", 'pid', 'children');
        // this.areaList.push(menu);
        console.log(response);
        this.areaList = this.handleTree(response.data, "id", 'pid', 'children');
        this.loading = false;
      })
    },
    // 展开/折叠
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },

    // 搜索
    handleQuery() {
      this.getList();
    },
    // 新增
    handleAdd(row) {
      console.log('row', row)
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.postForm.pid = row.id;
      } else {
        this.postForm.pid = 0;
      }
      this.open = true;
      this.title = "添加场地"
    },
    // 修改
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      getArea(row.id).then(response => {
        this.postForm = response.data;
        this.open = true;
        this.title = "修改场地信息";
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal.confirm(`是否确认删除此数据？`).then(() => {
        this.loading = true;
        return delArea(row.id);
      }).then(() => {
        this.getList();
        this.loading = false;
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.loading = false
      });
    },
    // 确定
    submitForm() {
      this.$refs["postForm"].validate(valid => {
        if (valid) {
          if (this.postForm.id != null) {
            updateArea(this.postForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.postForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置提交表单
    reset() {
      this.postForm = {
        id: undefined,
        pid: undefined,
        parentName: undefined,
        areaNum: undefined,
        areaName: undefined,
        areaType: undefined,
        areaOpen: undefined,
        areaIntroduce: undefined,

      };
      this.resetForm('postForm')
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.areaName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      getAreaList().then(response => {
        this.areaOptions = [];
        const menu = {id: 0, areaName: '东营市第二中学', children: []};
        menu.children = this.handleTree(response.data, "id", 'pid', 'children');
        this.areaOptions.push(menu);
      });
    },

  }
}
</script>
