<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--树结构数据-->
      <el-col :span="4" :xs="24">
        <!-- <div class="head-container">
          搜索栏按钮
        </div>-->
        <div class="head-container">
          <el-tree
            :data="regionalismOptions"
            node-key="id"
            :default-expanded-keys="defalutExpanded"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            @node-click="nodeClick"
            ref="tree"
          />
        </div>
      </el-col>
      <!--列表结构数据-->
      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              class="filter-item"
              type="primary"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:regionalism:add']"
            >添加</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              size="mini"
              type="primary"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:regionalism:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button size="mini" type="primary">显示子级</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button size="mini">显示所有子级</el-button>
          </el-col>
        </el-row>
         <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form
              :model="queryParams"
              ref="queryForm"
              :inline="true"
              label-width="96px"
            >
              <el-form-item label="行政区划名称" prop="regionalismName">
                <el-input
                  v-model="queryParams.regionalismName"
                  placeholder="请输入行政区划名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  class="filter-item"
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                  >搜索</el-button
                >
                <el-button
                  class="filter-item"
                  type="primary"
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['system:regionalism:add']"
                  >新增</el-button
                >
              </el-form-item>
            </el-form>
          </el-col>
          </el-row>-->
          <!-- <el-table
          v-loading="loading"
          :data="regionalismList"
          row-key="regionalismId"
          default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          >-->
        </el-row>

        <el-table
          v-loading="loading"
          :data="regionalismList"
          row-key="regionalismId"
          default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <!--<el-table
          v-loading="loading"
          :data="regionalismList"
          @selection-change="handleSelectionChange"
          >-->
          <el-table-column label="行政区划" align="left" prop="regionalismName" />
          <el-table-column label="行政区划代码" align="left" prop="regionalismCode" />
          <el-table-column label="驻地经度" align="left" prop="stationLng" />
          <el-table-column label="驻地纬度" align="left" prop="stationLat" />

          <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)"
                v-hasPermi="['system:regionalism:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:regionalism:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!-- 添加或修改行政划分对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-form-item label="上级行政区划" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="regionalismOption"
              :normalizer="normalizer"
              placeholder="请选择上级行政区划"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="行政区划名称" prop="regionalismName">
              <el-input v-model="form.regionalismName" placeholder="请输入行政区划名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行政区划代码" prop="regionalismCode">
              <el-input v-model="form.regionalismCode" placeholder="请输入行政区划代码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="驻地经度" prop="stationLng">
              <el-input v-model="form.stationLng" placeholder="请输入驻地经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驻地纬度" prop="stationLat">
              <el-input v-model="form.stationLat" placeholder="请输入驻地纬度" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRegionalism,
  getRegionalism,
  delRegionalism,
  addRegionalism,
  updateRegionalism,
  treeListRegionalism
} from "@/api/system/regionalism";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Regionalism",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 行政划分表格数据
      regionalismList: [],
      // 行政划分树选项
      regionalismOption: [],
      // 行政划分树选项
      regionalismOptions: [],
      // 弹出层标题
      title: "",
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        regionalismName: undefined
      }, // 树形结构数据
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      defalutExpanded: []
    };
  },
  created() {
    // this.getList();
    this.getTreeList();
  },
  methods: {
    nodeClick(data) {
      this.queryParams.regionalismId = data.id;
      this.getList();
    },
    /** 查询行政划分列表 */
    getList() {
      this.loading = true;
      // 树形数据
      this.queryParams.parentId = 0;
      listRegionalism(this.queryParams).then(response => {
        if (response.data.length > 0) {
          response.data[0].parentId = 0;
        }
        this.regionalismList = this.handleTree(
          response.data,
          "regionalismId",
          "parentId"
        );
        this.loading = false;
      });
    },
    /** 转换行政划分数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.regionalismId,
        label: node.regionalismName,
        children: node.children
      };
    },
    /** 查询行政区划下拉树结构 */
    getTreeselect() {
      listRegionalism().then(response => {
        this.regionalismOption = [];
        const data = {
          regionalismId: 0,
          regionalismName: "中国",
          children: []
        };
        data.children = this.handleTree(
          response.data,
          "regionalismId",
          "parentId"
        );
        this.regionalismOption.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        regionalismId: undefined,
        parentId: undefined,
        ancestors: undefined,
        regionalismCode: undefined,
        regionalismName: undefined,
        stationLng: undefined,
        stationLat: undefined,
        tenantId: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.regionalismId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.getTreeList();
      this.open = true;
      this.title = "添加行政划分";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      this.getTreeList();
      if (row != undefined) {
        this.form.parentId = row.regionalismId;
      }
      getRegionalism(row.regionalismId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改行政划分";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.regionalismId != undefined) {
            updateRegionalism(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addRegionalism(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        // '是否确认删除行政划分编号为"' + row.regionalismId + '"的数据项?',
        "是否删除改选项",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delRegionalism(row.regionalismId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    // 行政区划下拉树结构
    getTreeList() {
      treeListRegionalism().then(response => {
        this.defalutExpanded = [];
        if (response.data.length > 0) {
          this.defalutExpanded.push(response.data[0].id);
          this.nodeClick(response.data[0]);
        }
        this.regionalismOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    }
  }
};
</script>
