<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="教室编号" prop="roomNo">
        <el-input
          v-model="queryParams.roomNo"
          placeholder="请输入教室编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教室名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入教室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教学楼" prop="building">
        <el-select v-model="queryParams.building" placeholder="请选择教学楼" clearable>
          <el-option label="A栋" value="A栋" />
          <el-option label="B栋" value="B栋" />
          <el-option label="C栋" value="C栋" />
          <el-option label="D栋" value="D栋" />
        </el-select>
      </el-form-item>
      <el-form-item label="教室类型" prop="roomType">
        <el-select v-model="queryParams.roomType" placeholder="请选择教室类型" clearable>
          <el-option label="普通教室" value="普通教室" />
          <el-option label="实验室" value="实验室" />
          <el-option label="多媒体教室" value="多媒体教室" />
          <el-option label="阶梯教室" value="阶梯教室" />
          <el-option label="体育馆" value="体育馆" />
        </el-select>
      </el-form-item>
      <el-form-item label="可用状态" prop="isAvailable">
        <el-select v-model="queryParams.isAvailable" placeholder="请选择状态" clearable>
          <el-option label="可用" :value="1" />
          <el-option label="不可用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button 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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['courses:classroom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['courses:classroom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['courses:classroom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classroomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="教室编号" align="center" prop="roomNo" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="教室名称" align="center" prop="roomName" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="教室类型" align="center" prop="roomType" width="120">
        <template slot-scope="scope">
          <el-tag :type="getRoomTypeTagType(scope.row.roomType)">
            {{ scope.row.roomType }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="容量" align="center" prop="capacity" width="80">
        <template slot-scope="scope">
          <span :class="{ 'warning-text': scope.row.capacity < 30 }">
            {{ scope.row.capacity }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="教学楼" align="center" prop="building" width="100" />
      <el-table-column label="楼层" align="center" prop="floor" width="80" />
      <el-table-column label="位置描述" align="center" prop="location" :show-overflow-tooltip="true" />
      <el-table-column label="可用状态" align="center" prop="isAvailable" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isAvailable === 1 ? 'success' : 'danger'">
            {{ scope.row.isAvailable === 1 ? '可用' : '不可用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="设施配置" align="center" prop="facilities" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div class="facilities-list">
            <el-tag v-for="facility in getFacilitiesList(scope.row.facilities)" :key="facility" size="small">
              {{ facility }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <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="['courses:classroom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['courses:classroom: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教室编号" prop="roomNo">
              <el-input v-model="form.roomNo" placeholder="请输入教室编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教室名称" prop="roomName">
              <el-input v-model="form.roomName" placeholder="请输入教室名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教室类型" prop="roomType">
              <el-select v-model="form.roomType" placeholder="请选择教室类型" style="width: 100%">
                <el-option label="普通教室" value="普通教室" />
                <el-option label="实验室" value="实验室" />
                <el-option label="多媒体教室" value="多媒体教室" />
                <el-option label="阶梯教室" value="阶梯教室" />
                <el-option label="体育馆" value="体育馆" />
                <el-option label="音乐室" value="音乐室" />
                <el-option label="美术室" value="美术室" />
                <el-option label="计算机房" value="计算机房" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="容量" prop="capacity">
              <el-input-number
                v-model="form.capacity"
                :min="1"
                :max="500"
                controls-position="right"
                style="width: 100%"
                placeholder="请输入容量"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教学楼" prop="building">
              <el-select v-model="form.building" placeholder="请选择教学楼" style="width: 100%">
                <el-option label="A栋" value="A栋" />
                <el-option label="B栋" value="B栋" />
                <el-option label="C栋" value="C栋" />
                <el-option label="D栋" value="D栋" />
                <el-option label="E栋" value="E栋" />
                <el-option label="F栋" value="F栋" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼层" prop="floor">
              <el-input-number
                v-model="form.floor"
                :min="-1"
                :max="30"
                controls-position="right"
                style="width: 100%"
                placeholder="请输入楼层"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="位置描述" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置描述" />
        </el-form-item>

        <el-form-item label="设施配置">
          <el-checkbox-group v-model="form.facilitiesList">
            <el-checkbox label="投影仪">投影仪</el-checkbox>
            <el-checkbox label="空调">空调</el-checkbox>
            <el-checkbox label="电脑">电脑</el-checkbox>
            <el-checkbox label="音响">音响</el-checkbox>
            <el-checkbox label="白板">白板</el-checkbox>
            <el-checkbox label="实验设备">实验设备</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="可用状态" prop="isAvailable">
              <el-radio-group v-model="form.isAvailable">
                <el-radio :label="1">可用</el-radio>
                <el-radio :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </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>
  </div>
</template>

<script>
import { listClassroom, getClassroom, delClassroom, addClassroom, updateClassroom, exportClassroom } from "@/api/course/classroom"

export default {
  name: "Classroom",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教室表格数据
      classroomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomNo: null,
        roomName: null,
        building: null,
        roomType: null,
        isAvailable: null
      },
      // 表单参数
      form: {
        facilitiesList: []
      },
      // 表单校验
      rules: {
        roomNo: [
          { required: true, message: "教室编号不能为空", trigger: "blur" }
        ],
        roomName: [
          { required: true, message: "教室名称不能为空", trigger: "blur" }
        ],
        roomType: [
          { required: true, message: "教室类型不能为空", trigger: "change" }
        ],
        capacity: [
          { required: true, message: "容量不能为空", trigger: "blur" }
        ],
        building: [
          { required: true, message: "教学楼不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询教室列表 */
    getList() {
      this.loading = true
      listClassroom(this.queryParams).then(response => {
        this.classroomList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomNo: null,
        roomName: null,
        roomType: '普通教室',
        capacity: 50,
        building: 'A栋',
        floor: 1,
        location: null,
        facilities: null,
        facilitiesList: [],
        isAvailable: 1,
        remark: null
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    },
    /** 搜索按钮操作 */
    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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加教室"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getClassroom(id).then(response => {
        const data = response.data
        // 处理设施配置
        data.facilitiesList = this.getFacilitiesList(data.facilities)
        this.form = data
        this.open = true
        this.title = "修改教室"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 处理设施配置
          this.form.facilities = this.form.facilitiesList.join(',')

          if (this.form.id != null) {
            updateClassroom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addClassroom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除教室名称为"' + (row.roomName || ids) + '"的数据项？').then(() => {
        return delClassroom(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
        this.ids = []
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有教室数据项？').then(() => {
        this.exportLoading = true
        return exportClassroom(this.queryParams)
      }).then(response => {
        this.$download.name(response, "教室数据.xlsx")
        this.exportLoading = false
      }).catch(() => {})
    },
    /** 教室类型标签类型 */
    getRoomTypeTagType(type) {
      const types = {
        '普通教室': '',
        '实验室': 'success',
        '多媒体教室': 'warning',
        '阶梯教室': 'danger',
        '体育馆': 'info',
        '音乐室': 'success',
        '美术室': 'warning',
        '计算机房': 'info'
      }
      return types[type] || ''
    },
    /** 获取设施列表 */
    getFacilitiesList(facilities) {
      if (!facilities) return []
      return facilities.split(',').filter(item => item.trim())
    }
  }
}
</script>

<style scoped>
.warning-text {
  color: #e6a23c;
  font-weight: bold;
}

.facilities-list {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  justify-content: center;
}
</style>
