<!--控制记录-->
<template>
  <div class="app-container control">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="74px">
      <el-form-item label="操作性质" prop="triggerModes">
        <el-select v-model="queryParams.triggerModes" placeholder="操作性质" style="width:180px;">
          <el-option
            v-for="dict in triggerClassOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="控制类型" prop="controlType">-->
        <!--<el-select v-model="queryParams.controlType" placeholder="请选择控制类型" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="硬件类型" prop="hardwareType">
        <el-select v-model="queryParams.hardwareType" placeholder="请选择硬件类型" style="width:180px;">
          <el-option v-for="hardType in hardTypeOptions"
                     :key="hardType.enumName"
                     :label="hardType.name"
                     :value="hardType.enumName">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="硬件类型" prop="controlType">-->
        <!--<el-select v-model="queryParams.controlType" placeholder="请选择硬件类型" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="学院" prop="type" label-width="50px">
        <el-select v-model="queryParams.deptId" placeholder="请选择学院" @change="handleNodeClick" style="width:180px;">
          <el-option
            v-for="dict in deptOptions"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="学院" prop="controlType">-->
        <!--<el-select v-model="queryParams.controlType" placeholder="请选择学院" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="实验室" prop="subjectId" label-width="60px">
        <el-select v-model="queryParams.subjectId" placeholder="请选择实验室" style="width:180px;">
          <el-option
            v-for="item in subjectOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="实验室" prop="controlType">-->
        <!--<el-select v-model="queryParams.controlType" placeholder="请选择实验室" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="操作时间">
        <el-date-picker
          :clearable="false"
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!--<el-form-item label="开始时间" prop="inTime">-->
        <!--<el-date-picker clearable size="small"-->
                        <!--v-model="queryParams.beginTimeStr"-->
                        <!--type="date"-->
                        <!--value-format="yyyy-MM-dd"-->
                        <!--placeholder="请选择开始时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="结束时间" prop="inTime">-->
        <!--<el-date-picker clearable size="small"-->
                        <!--v-model="queryParams.endTimeStr"-->
                        <!--type="date"-->
                        <!--value-format="yyyy-MM-dd"-->
                        <!--placeholder="请选择结束时间">-->
        <!--</el-date-picker>-->
      <!--</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="controlList" @selection-change="handleSelectionChange">
      <el-table-column label="操作性质" align="left" prop="triggerModes" >
        <template slot-scope="scope">
          <p v-for="(item,index) in triggerClassOptions" :key="index" v-if="scope.row.triggerModes==item.dictValue">
          {{item.dictLabel+'('+scope.row.userName+')'}}
          </p >
        </template>
      </el-table-column>
      <!--<el-table-column label="操作性质" align="left" prop="id" />-->
      <el-table-column label="执行操作" align="left" prop="controlType" >
        <template slot-scope="scope">
          <p v-for="(item,index) in controlTypeClassOptions" :key="index" v-if="scope.row.controlType==item.dictValue">{{item.dictLabel}}</p >
        </template>
      </el-table-column>
      <!--<el-table-column label="执行操作" align="left" prop="controlType" />-->
      <el-table-column label="硬件类型" align="left" prop="hardwareType" >
        <template slot-scope="scope">
          {{scope.row.hardwareType.name }}
        </template>
      </el-table-column>
      <!--<el-table-column label="硬件类型" align="left" prop="controlType" />-->
      <el-table-column label="学院" align="left" prop="deptName" />
      <el-table-column label="实验室" align="left" prop="subjectName" />
      <el-table-column label="操作时间" align="left" prop="createTime" />
    </el-table>

    <pagination :page-sizes="[20, 30, 40, 50]"
      v-show="total>0"
      :total="total"
      layout="total, prev, pager, next, sizes, jumper"
      :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="80px">
        <el-form-item label="触发方式" prop="triggerModes">
          <el-input v-model="form.triggerModes" placeholder="请输入触发方式" />
        </el-form-item>
        <el-form-item label="控制类型" prop="controlType">
          <el-select v-model="form.controlType" placeholder="请选择控制类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作人id" prop="joinUserId">
          <el-input v-model="form.joinUserId" placeholder="请输入操作人id" />
        </el-form-item>
        <el-form-item label="操作人姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入操作人姓名" />
        </el-form-item>
        <el-form-item label="执行操作" prop="operation">
          <el-input v-model="form.operation" placeholder="请输入执行操作" />
        </el-form-item>
        <el-form-item label="关联硬件设备id" prop="hardwareId">
          <el-input v-model="form.hardwareId" placeholder="请输入关联硬件设备id" />
        </el-form-item>
        <el-form-item label="实验室id" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入实验室id" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="创建人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listControl, getControl, delControl, addControl, updateControl } from "@/api/laboratory/control";
import { listDepartments } from "@/api/system/dept";
import {subjectDictList } from "@/api/laboratory/record";

  import { queryHardwareType} from "@/api/laboratory/hardware";


import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Control",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 控制记录表格数据
      controlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      controlTypeClassOptions: "",
      triggerClassOptions: "",
      cateClassOptions: "",
      // 部门树选项
      deptOptions: undefined,
      subjectOptions: "",
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        triggerModes: null,
        controlType: null,
        joinUserId: null,
        userName: null,
        operation: null,
        hardwareId: null,
        subjectId: null,
        deptId: null,
        deptName: null,
        userId: null,
        hardwareType: null,
        hardwareTypeEnum: null,
        beginTimeStr: null,
        endTimeStr: null
      },
      hardTypeOptions:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("control_type").then(response => {
      this.controlTypeClassOptions = response.data;
      console.log(this.controlTypeClassOptions)
    })
    this.getDicts("trigger_modes").then(response => {
      this.triggerClassOptions = response.data;
      console.log(this.triggerClassOptions)
    })
    this.getDicts("equipment_category").then(response => {
      this.cateClassOptions = response.data;
      console.log(this.cateClassOptions)
    })
    this.getHardTypeList();
    this.getDeptList();
  },
  methods: {

    getHardTypeList(){
          queryHardwareType().then(response => {
            if(response.code==200){
                this.hardTypeOptions=response.data;
            }
            console.log(this.hardTypeOptions)
        });
      },
    /** 查询实验室字典列表 */
    getSubjectDictList (deptId) {
      this.subjectOptions = null;
      this.loading = true;
      // this.queryParams.deptId = deptId;
      let params = {};
      params.deptId = deptId;
      this.queryParams.subjectId = "";
      subjectDictList(params).then(response => {
        this.subjectOptions = response.data;
        this.loading = false;
      });
    },
    /** 查询控制记录列表 */
    getList() {
      this.loading = true;
      if(this.dateRange[0]){
        this.queryParams.beginTimeStr = this.dateRange[0];
        this.queryParams.endTimeStr = this.dateRange[1];
      }else {
        this.queryParams.beginTimeStr = null
        this.queryParams.endTimeStr = null
      }
      listControl(this.queryParams).then(response => {
        this.controlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询学院列表 */
    getDeptList() {
      listDepartments().then(response => {
        this.$set(this, 'deptOptions', response.data)
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data;
      // console.log("data",data);
      this.getSubjectDictList(data);
      // this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        triggerModes: null,
        controlType: null,
        joinUserId: null,
        userName: null,
        operation: null,
        hardwareId: null,
        subjectId: null,
        deptId: null,
        deptName: null,
        createTime: null,
        userId: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.subjectOptions = []
      this.queryParams.beginTimeStr = null
      this.queryParams.endTimeStr = null
      this.queryParams.deptId = null
      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
      getControl(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) {
            updateControl(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addControl(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除控制记录?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delControl(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/control/export', {
        ...this.queryParams
      }, `laboratory_control.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .control{
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    .button-box{
      width:120px;
      display: flex;
    }
    .form-box{
      display:flex;
      .null-p{
        flex:1;
      }
    }
  }
</style>

