<template>
  <div class="app-container">
    <div class="table_container">
      <el-row :gutter="20">
        <el-col :span="4" :xs="24">
          <div class="from_tree">
            <div class="head-container">
              <el-input
                v-model="deptName"
                placeholder="请输入部门名称"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
              />
            </div>
            <div class="head-container">
              <el-tree
                :data="deptOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                node-key="id"
                :default-expanded-keys="defaultKey"
                :current-node-key="defaultActive"
                highlight-current
                ref="tree"
                @node-click="handleNodeClick"
              />
              <!-- :default-checked-keys="defaultActive" -->
            </div>
          </div>
        </el-col>
        <el-col :span="20" :xs="24">
          <div class="from_table">
            <el-form
              :model="queryParams"
              ref="queryForm"
              :inline="true"
              v-show="showSearch"
            >
              <el-form-item label="名称" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  class="search_btn"
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                  >搜索</el-button
                >
                <el-button
                  class="agin_btn"
                  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="new"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['taicangpop:building:add']"
                  >新增</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="revise"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['taicangpop:building:edit']"
                  >修改</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="remove"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['taicangpop:building:remove']"
                  >删除</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="export"
                  size="mini"
                  :loading="exportLoading"
                  @click="handleExport"
                  v-hasPermi="['taicangpop:building:export']"
                  >导出</el-button
                >
              </el-col>
              <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
              ></right-toolbar>
            </el-row>
            <el-table
              v-loading="loading"
              :data="buildingList"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column
                label="组团名称"
                align="center"
                prop="deptname"
              />
              <el-table-column label="名称" align="center" prop="name" />
              <el-table-column label="层数" align="center" prop="floor" />
              <el-table-column label="每层单元数" align="center" prop="unit" />
              <el-table-column label="每层总户数" align="center" prop="house" />
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                width="200"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-zoom-in"
                    @click="handleInfo(scope.row)"
                    >查看</el-button
                  >
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['taicangpop:building:edit']"
                    >修改</el-button
                  >
                  <el-button
                    class="table_icon_text"
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['taicangpop:building:remove']"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total > 0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />

            <!-- 添加或修改楼栋管理对话框 -->
            <el-dialog
              :title="title"
              :visible.sync="open"
              width="500px"
              append-to-body
              :close-on-click-modal="false"
              :destroy-on-close="true"
            >
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="auto"
              >
                <el-form-item label="名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="归属部门" prop="deptId">
                  <treeselect
                    v-model="form.deptId"
                    :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择归属部门"
                  />
                </el-form-item>
                <el-form-item label="层数" prop="floor">
                  <el-input v-model="form.floor" placeholder="请输入层数" />
                </el-form-item>
                <el-form-item label="每层单元数" prop="unit">
                  <el-input
                    v-model="form.unit"
                    placeholder="请输入每层单元数"
                  />
                </el-form-item>
                <el-form-item label="每层总户数" prop="house">
                  <el-input
                    v-model="form.house"
                    placeholder="请输入每层总户数"
                  />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                  <el-input
                    v-model="form.remark"
                    type="textarea"
                    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>
            <!-- 详情样式 -->
            <el-dialog
              title="详情信息"
              :visible.sync="infoOpen"
              width="500px"
              append-to-body
              :close-on-click-modal="false"
              :destroy-on-close="true"
            >
              <el-descriptions
                title="楼幢详情"
                :column="1"
                border
                class="textData"
              >
                <el-descriptions-item label="苑名称">{{
                  form.deptname
                }}</el-descriptions-item>
                <el-descriptions-item label="楼名称">{{
                  form.name
                }}</el-descriptions-item>

                <el-descriptions-item label="层数">{{
                  form.floor
                }}</el-descriptions-item>
                <el-descriptions-item label="每层单元数">{{
                  form.unit
                }}</el-descriptions-item>
                <el-descriptions-item label="每层总户数">{{
                  form.house
                }}</el-descriptions-item>
                <el-descriptions-item label="备注">{{
                  form.remark
                }}</el-descriptions-item>
              </el-descriptions>
            </el-dialog>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  listBuilding,
  getBuilding,
  delBuilding,
  addBuilding,
  updateBuilding,
  exportBuilding,
} from "@/api/taicangpop/building";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
//部门
import { treeselect } from "@/api/system/dept";
//校正
import check from "@/utils/regex/index.js";
export default {
  name: "Building",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼栋管理表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 是否显示弹出层
      open: false,
      //详情弹出层
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        deptId: undefined,
      },
      defaultKey: [],
      defaultActive: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        deptId: [
          { required: true, message: "归属部门不能为空", trigger: "blur" },
        ],
      },
      autoWidth: {
        width: "",
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getTreeselect();
    // this.getList();
  },

  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;

        //默认展开项
        this.deptOptions.forEach((item) => {
          item.children.forEach((child) => {
            if (child.children) {
              child.children.forEach((childs) => {
                if (childs.children) {
                  childs.children.forEach((childss) => {
                    // console.warn("默认展开4", childss);
                    this.defaultKey.push(childss.id);
                  });
                }
              });
            }
          });
        });

        // 默认选中
        this.defaultActive =
          this.deptOptions[0].children[0].children[0].children[0].children[0].id;

        //需要注意的是setCurrentKey函数必须要放到$nextTick里面，要不然tree还未渲染上，是找不到defaultActive的
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.defaultActive);
        });

        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          name: null,
          deptId: this.defaultActive,
        };
        this.getList();
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (!data.children) {
        this.queryParams.deptId = data.id;
        this.getList();
      }
    },

    /** 查询楼栋管理列表 */
    getList() {
      this.loading = true;
      listBuilding(this.queryParams).then((response) => {
        this.buildingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        floor: null,
        unit: null,
        house: null,
        createId: null,
        createBy: null,
        createTime: null,
        updateId: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleLook(row) {
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼栋管理";
    },
    /**查看详情 */
    handleInfo(row) {
      this.reset();
      let id = row.id;
      getBuilding(id).then((response) => {
        this.form = response.data;
        this.infoOpen = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBuilding(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼栋管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.userId = JSON.parse(
              sessionStorage.getItem("USER_INFO")
            ).userId;
            addBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除楼栋管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBuilding(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有楼栋管理数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportBuilding(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-descriptions-row th {
  text-align: center !important;
  width: 130px !important;
}
::v-deep .textData {
  padding-bottom: 10px;
}
</style>
