<!--疏散指示灯-->
<template>
  <div class="app-container lightPage">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="指示灯类型" prop="lightDirection">
        <el-select v-model="queryParams.lightDirection" placeholder="请选择指示灯类型" clearable size="small" label-width="100px">
          <el-option label="左指示灯" :value="1" />
          <el-option label="右指示灯" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="实验室" prop="subjectId" label-width="100px">
        <el-select
          v-model="queryParams.subjectId"
          filterable
          clearable
          remote
          reserve-keyword
          placeholder="请输入实验室关键词"
          @visible-change="getUserSelectList"
          :remote-method="userSelectList"
          :loading="loading">
          <el-option
            v-for="item in laboratoryOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="状态" prop="lightStatus">-->
      <!--<el-select v-model="queryParams.lightStatus" placeholder="请选择状态" clearable size="small" label-width="60px">-->
      <!--<el-option label="请选择字典生成" value="" />-->
      <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" border :data="lightList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="实验室" align="center" prop="subName" />
      <el-table-column label="指示灯类型" align="center" prop="lightDirection">
        <template slot-scope="scope">
          {{scope.row.lightDirection == 1?'左指示灯':(scope.row.lightDirection == 2?'右指示灯':'')}}
        </template>
      </el-table-column>
      <el-table-column label="楼栋" align="center" prop="lineDeptName" />
      <el-table-column label="楼层" align="center" prop="floorName" />
      <el-table-column label="继电器编号" align="center" prop="relayCode" />
      <el-table-column label="继电器路数" align="center" prop="relayBit" />
      <!--<el-table-column label="继电器状态" align="center" prop="lightStatus" />-->
      <el-table-column label="继电器配置名称" align="center" prop="configName" />
     <el-table-column label="继电器配置状态" align="center" prop="configStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box" style="display: flex;margin-left:20px;">
            <p class="table-min-button"
               @click="handleUpdate(scope.row)"
               v-hasPermiAnd="['laboratory:light:query','laboratory:light:edit']"
            >编辑</p>
          </div>
        </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>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="实验室名称" prop="subjectId">
          <el-input v-model="form.subName" placeholder="请输入实验室名称" :disabled="true" />
        </el-form-item>
        <el-form-item label="楼栋" prop="lineDeptName">
          <el-input v-model="form.lineDeptName" placeholder="请输入楼栋名称" :disabled="true" />
        </el-form-item>
        <el-form-item label="楼层" prop="floorName">
          <el-input v-model="form.floorName" placeholder="请输入楼层名称" :disabled="true" />
        </el-form-item>
        <el-form-item label="继电器编号" prop="relayCode">
          <el-input v-model="form.relayCode" placeholder="请输入继电器编号" />
        </el-form-item>
        <el-form-item label="继电器路数" prop="relayBit">
          <el-input v-model="form.relayBit" placeholder="请输入继电器路数" />
        </el-form-item>
        <el-form-item label="继电器配置名称" prop="relayBit">
          <el-input v-model="form.configName" placeholder="请输入继电器配置名称" maxlength="20"/>
        </el-form-item>
        <el-form-item label="继电器配置状态" prop="relayBit">
          <el-input v-model="form.configStatus" placeholder="请输入继电器配置状态" maxlength="20"/>
        </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>
  </div>
</template>

<script>
  import { listLight, getLight, delLight, addLight, updateLight } from "@/api/laboratory/light";
  import { subjectListAll} from "@/api/laboratory/approval";

  export default {
    name: "Light",
    data() {
      return {
        tableButtonType:this.hasPermiDom(['laboratory:light:query','laboratory:light:edit']),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 逃生指引灯表格数据
        lightList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          subjectId: null,
          buildId: null,
          relayCode: null,
          relayBit: null,
          lightDirection: null,
          lightStatus: null,
          userId: null,
          subName: null,
          lineDeptName: null,
          floorName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        laboratoryOptions:[],
      };
    },
    created() {
      this.getList();
      this.userSelectList();
    },
    methods: {
      getUserSelectList(type){
        if(!type){
          subjectListAll({name:''}).then(response => {
            this.laboratoryOptions = response.data;
          });
        }
      },
      //懒加载
      userSelectList(e){
        subjectListAll({name:e}).then(response => {
          this.laboratoryOptions = response.data;
        });
      },
      /** 查询逃生指引灯列表 */
      getList() {
        this.loading = true;
        listLight(this.queryParams).then( response => {
          this.lightList =  response.rows;
          this.total =  response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          subjectId: null,
          buildId: null,
          relayCode: null,
          relayBit: null,
          lightDirection: null,
          lightStatus: 0,
          userId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: 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
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加逃生指引灯";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getLight(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) {
              updateLight(this.form).then( response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addLight(this.form).then( response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除逃生指引灯编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLight(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('laboratory/light/export', {
          ...this.queryParams
        }, `laboratory_light.xlsx`)
      }
    }
  };
</script>
<style>
  .lightPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden!important;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    border-radius: 10px;
  }
</style>
