<template>
  <div class="app-container iot-detail-space">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="上级建筑" prop="parentCode">
        <treeselect
          style="width: 200px;"
          v-model="queryParams.parentCode"
          :options="querySpaceOptions"
          :normalizer="normalizer"
          placeholder="请选择上级建筑"
        />
      </el-form-item>
      <el-form-item label="建筑名称" prop="buildingName">
        <el-input
          v-model="queryParams.buildingName"
          placeholder="建筑名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建筑类型" prop="buildingType">
        <el-select v-model="queryParams.buildingType" placeholder="建筑类型" clearable>
          <el-option
              v-for="dict in buildingTypeList"
              :key="dict.buildingTypeCode"
              :label="dict.buildingTypeName"
              :value="dict.buildingTypeCode"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="spaceList"
      row-key="buildingCode"
      default-expand-all
      :tree-props="{ children: 'children' }"
    >
      <el-table-column type="index" label="序号" align="center" :index="indexFormatter" width="80px"></el-table-column>
      <el-table-column label="建筑名称" align="left" prop="buildingName" />
      <el-table-column label="建筑类型" align="center" prop="buildingType" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetails(scope.row)"
          >建筑设备</el-button>
          <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>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改项目建筑对话框 -->
    <el-dialog class="eldialog-wrap" :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="上级建筑:" prop="parentCode">
          <treeselect v-model="form.parentCode" :normalizer="normalizer" :options="spaceOptions" placeholder="请选择上级建筑" />
        </el-form-item>
        <el-form-item label="建筑名称:" prop="buildingName">
          <el-input v-model="form.buildingName" placeholder="请输入建筑名称" />
        </el-form-item>
        <el-form-item label="建筑编码:" prop="buildingCode">
          <el-input v-model="form.buildingCode" placeholder="请输入建筑编码" />
        </el-form-item>
        <el-form-item label="建筑类型:" prop="buildingType">
          <el-select v-model="form.buildingType" filterable style="width: 100%;" placeholder="请选择建筑类型">
            <el-option
              v-for="dict in buildingTypeList"
              :key="dict.buildingTypeCode"
              :label="dict.buildingTypeName"
              :value="dict.buildingTypeCode"
            ></el-option>
          </el-select>
        </el-form-item>
      </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>

    <el-dialog
      class="eldialog-wrap"
      :title="spaceDeviceTitle"
      :visible.sync="selectTableShow"
      width="75%"
      top="10vh"
      :close-on-click-modal="false"
    >
      <building-device
        v-if="selectTableShow"
        :buildingCode="buildingInfo.buildingCode"
      ></building-device>

      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="() =>{selectTableShow = false}">确 定</el-button>
        <el-button size="mini" @click="() =>{selectTableShow = false}">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBuildings,
  getBuildings,
  delBuildings,
  addBuildings,
  updateBuildings,
  listRegionSpaceTree
} from "@/api/iot/regionSpace";
import {
  listType
} from "@/api/iot/buildingType";
import BuildingDevice from "./BuildingDevice";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Space",
  components: {
    Treeselect,
    BuildingDevice
  },
  props: ["infoData"],
  data() {
    return {
      selectTableShow: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 项目建筑表格数据
      spaceList: [],
      // 项目建筑树选项
      spaceOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        parentCode: null,
        buildingCode: null,
        buildingName: null,
        spaceCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      spaceTypeOptions: [],
      querySpaceOptions: [],
      sourceId: "",
      spaceDeviceTitle: "",
      buildingTypeList: [],
      buildingInfo: {}
    };
  },
  created() {
    this.getQueryTreeselect();
    this.getBuildingTypeList();
    this.getList();
  },
  methods: {
    indexFormatter(val) {
      return val + 1;
    },
    getBuildingTypeList() {
      listType({
        pageNum: 1,
        pageSize: 10
      }).then(res => {
        this.buildingTypeList = res.rows
      })
    },
    // 打开厂商选择窗口 ——表格
    handleDetails(row) {
      this.buildingInfo = row
      this.spaceDeviceTitle = `[ ${row.buildingName} ] 建筑--设备管理`;
      this.selectTableShow = true;
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.spaceTypeOptions, row.buildingType);
    },
    /** 查询项目建筑列表 */
    getList() {
      this.loading = true;
      this.queryParams.regionCode = this.infoData.regionCode;
      listBuildings(this.queryParams).then(response => {
        this.spaceList = this.handleTree(response.rows, "buildingCode", "parentCode");
        this.loading = false;
      });
    },
    /** 转换项目建筑数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.buildingCode,
        label: node.buildingName,
        children: node.children
      };
    },
    /** 查询部门下拉树结构 */
    getTreeselect(param) {
      listRegionSpaceTree(param).then(response => {
        this.spaceOptions = [];
        const data = { buildingCode: 0, buildingName: "顶级节点", children: [] };
        data.children = this.handleTree(response.data, "buildingCode", "parentCode");
        // data.children = response.data;
        console.log('---', data)
        this.spaceOptions.push(data);
      });
    },
    /** 查询部门下拉树结构 */
    getQueryTreeselect() {
      let param = {
        regionCode: this.infoData.regionCode
      };
      listBuildings(param).then(response => {
        this.querySpaceOptions = [];
        const data = { buildingCode: 0, buildingName: "顶级节点", children: [] };
        data.children = this.handleTree(response.data, "buildingCode", "parentCode");
        this.querySpaceOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        regionCode: this.infoData.regionCode,
        buildingName: null,
        buildingType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      let param = {
        regionCode: this.infoData.regionCode
      };
      this.getTreeselect(param);
      this.getBuildingTypeList();
      this.open = true;
      this.title = "添加建筑";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      let param = {
        regionCode: this.infoData.regionCode,
        buildingCode: row.buildingCode
      };
      this.getTreeselect(param);
      this.getBuildingTypeList();
      if (row != null) {
        this.form.regionCode = row.regionCode;
      }
      getBuildings({ buildingCode: row.buildingCode }).then(response => {
        this.form = JSON.parse(JSON.stringify(response.data));
        this.open = true;
        this.title = "修改项目建筑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateBuildings(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuildings(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除项目建筑编号为"' + row.buildingName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          debugger
          return delBuildings({ buildingCode: row.buildingCode });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    }
  }
};
</script>
<style lang="scss">
.iot-detail-space {
  .eldialog-wrap {
    .el-dialog__header {
      border-bottom: 1px solid #747373;
    }
    .el-dialog__body {
      padding: 0px;
      height: calc(100vh - 200px);
      overflow: auto;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;
      border-top: 1px solid #747373;
      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
  }
}
</style>
