<template>
  <el-dialog
    title="选择场地"
    :visible.sync="openSelectArea"
    width="750px"
    append-to-body
    style="padding: 0 0 0 0"
    @close="cancel"
  >
    <div>
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="场地名称" prop="areaName">
          <el-input
            v-model="queryParams.areaName"
            placeholder="请输入场地名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" size="mini" @click="handleReset"
          >重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref="multipleTable"
        max-height="250"
        width="100%"
        v-loading="loading"
        :data="areaList"
        size="small"
        row-key="id"
        highlight-current-row
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @row-click="handleCurrentChange"
        @selection-change="select"
        @select-all="selectAll"
        >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="场地编号" align="center" prop="areaNum"></el-table-column>
        <el-table-column label="场地名称" align="center" prop="areaName"></el-table-column>
        <el-table-column label="场地类型" align="center" 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" 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="场地介绍" prop="areaIntroduce" align="center">
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getAreaList"
      />
      <div style="text-align: center; margin: 35px 0 0 0">
        <el-button
          type="primary"
          @click="confirm"
          size="small"
          style="margin: 0 10px 0 0"
          :disabled="!isSimple"
        >确 定
        </el-button>
        <el-button @click="cancel" size="small" style="margin: 0 30px 0 10px"
        >取 消
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
// import {listArea} from "@/api/smartSchool/device/device.js";
import {getAreaList} from "@/api/smartSchool/schoolManage/areaManage/index";

export default {
  name: "selectArea",
  created() {
    this.getAreaList();
  },
  dicts: ['area_type', 'area_open_state'],
  data() {
    return {
      // 选择场地对话框
      openSelectArea: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场地表格数据
      areaList: [],
      // 是否为单个
      isSimple: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        areaName: null,
      },
      // 表单参数
      form: {},
      //
      areaName: null,
      areaId: null,
    };
  },
  methods: {
    //查询所有场地信息
    getAreaList() {
      this.loading = true;
      getAreaList(this.queryParams).then((response) => {
        this.areaList = this.handleTree(response.data, "id", 'pid', 'children');
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getAreaList();
    },
    handleReset() {
      this.queryParams.pageNum = 1;
      this.queryParams.areaName = null;
      this.isSimple = false;
      this.getAreaList();
    },
    //打开设备选择对话框，并调用方法
    open() {
      this.openSelectArea = true;
      // this.getAreaList();
    },
    // 多选
    select(selection) {
      if (selection.length > 1) {
        this.$refs.multipleTable.toggleRowSelection(selection[0], false);
        selection.shift(); //删除上一个选项
      }
      this.areaName = selection[0].areaName;
      this.areaId = selection[0].id;
      this.isSimple = selection.length === 1;
    },
    selectAll(selection) {
      this.$refs.multipleTable.clearSelection();
    },
    //单选
    handleCurrentChange(row) {
      // console.log("不触发也打印？");
      this.areaName = row.areaName;
      this.areaId = row.id;
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    confirm() {
      this.openSelectArea = false;
      this.$parent.confirmArea(this.areaName, this.areaId);
      this.handleReset()

    },
    cancel() {
      this.openSelectArea = false;
      this.handleReset()
    },
  },
};
</script>
