<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="parentId">
        <treeselect
          style="width: 200px;"
          v-model="queryParams.parentId"
          :options="querySpaceOptions"
          :normalizer="normalizer"
          placeholder="请选择上级空间"
        />
      </el-form-item>
      <el-form-item label="空间名称" prop="spaceName">
        <el-input
          v-model="queryParams.spaceName"
          placeholder="空间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="空间类型" prop="spaceType">
        <el-select v-model="queryParams.spaceType" placeholder="空间类型" clearable>
          <el-option
            v-for="dict in spaceTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></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="spaceId"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column type="index" label="序号" align="center" :index="indexFormatter" width="80px"></el-table-column>
      <el-table-column label="空间名称" align="left" prop="spaceName" />
      <el-table-column label="空间类型" align="center" prop="spaceType" :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>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改项目空间对话框 -->

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

      <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 {
  listSpace,
  getSpace,
  exportSpace,
  listSpaceTree
} from "@/api/tenant/project";
import SpaceDeviceWrap from "./spaceDevice";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Space",
  components: {
    Treeselect,
    SpaceDeviceWrap
  },
  props: ["infoData"],
  data() {
    return {
      selectTableShow: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 项目空间表格数据
      spaceList: [],
      // 项目空间树选项
      spaceOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        parentId: null,
        projectId: null,
        spaceName: null,
        spaceCode: null,
        spaceType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      spaceTypeOptions: [],
      querySpaceOptions: [],
      sourceId: "",
      spaceDeviceTitle: ""
    };
  },
  created() {
    this.getDicts("space_type").then(response => {
      this.spaceTypeOptions = response.data;
    });
    this.getQueryTreeselect();
    this.getList();
  },
  methods: {
    indexFormatter(val) {
      return val + 1;
    },
    // 打开厂商选择窗口 ——表格
    handleDetails(row) {
      this.sourceId = row.spaceId;
      this.spaceDeviceTitle = `[ ${row.spaceName} ] 空间--设备管理`;
      this.selectTableShow = true;
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.spaceTypeOptions, row.spaceType);
    },
    /** 查询项目空间列表 */
    getList() {
      this.loading = true;
      this.queryParams.projectId = this.infoData.projectId;
      listSpace(this.queryParams).then(response => {
        this.spaceList = this.handleTree(response.data, "spaceId", "parentId");
        this.loading = false;
      });
    },
    /** 转换项目空间数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.spaceId,
        label: node.spaceName,
        children: node.children
      };
    },
    /** 查询部门下拉树结构 */
    getTreeselect(param) {
      listSpaceTree(param).then(response => {
        this.spaceOptions = [];
        const data = { id: 0, label: "顶级节点", children: [] };
        // data.children = this.handleTree(response.data, "spaceId", "parentId");
        data.children = response.data;
        this.spaceOptions.push(data);
      });
    },
    /** 查询部门下拉树结构 */
    getQueryTreeselect() {
      let param = {
        projectId: this.infoData.projectId
      };
      listSpace(param).then(response => {
        this.querySpaceOptions = [];
        const data = { spaceId: 0, spaceName: "顶级节点", children: [] };
        data.children = this.handleTree(response.data, "spaceId", "parentId");
        this.querySpaceOptions.push(data);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</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>
