<!--实验室进出记录-->
<template>
  <div class="record">
    <div class="record-page" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="关键字" prop="nickName" label-width="60px">
          <el-input
            style="width:200px;"
            v-model="queryParams.nickName"
            placeholder="请输入姓名"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="学院" prop="deptId" label-width="50px">
          <el-select v-model="queryParams.deptId" placeholder="请选择学院" @change="handleNodeClick" style="width:200px;">
            <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="subjectId" label-width="60px">
          <el-select v-model="queryParams.subjectId" placeholder="请选择学院再选择实验室" style="width:220px;">
            <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="subjectId" label-width="80px">
          <el-date-picker
            :clearable="false"
            v-model="dateRange"
            style="width: 240px;height:40px;"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <!--<el-form-item label="时间类型" prop="inOutType">-->
        <!--<el-select v-model="queryParams.inOutType" placeholder="选择时间类型">-->
        <!--<el-option-->
        <!--v-for="item in inOutTypeOptions"-->
        <!--:key="item.value"-->
        <!--:label="item.label"-->
        <!--:value="item.value">-->
        <!--</el-option>-->
        <!--</el-select>-->
        <!--</el-form-item>-->

        <!--<el-form-item label="进入时间" prop="inTime">-->
        <!--<el-date-picker clearable size="small"-->
        <!--v-model="queryParams.inTime"-->
        <!--type="date"-->
        <!--value-format="yyyy-MM-dd"-->
        <!--placeholder="选择进入时间">-->
        <!--</el-date-picker>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="离开时间" prop="outTime">-->
        <!--<el-date-picker clearable size="small"-->
        <!--v-model="queryParams.outTime"-->
        <!--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-item style="float: right;">
          <el-col :span="1.5">
            <p class="add-button-one-120"
               @click="clickPage(2)"
            >我的进出记录</p>
          </el-col>
        </el-form-item>
      </el-form>
      <!--<div class="sheet-expand-box" v-hasPermi="['laboratory:record:export']">-->
      <!--<i class="el-icon-warning" style="color:#0045AF;"></i>-->
      <!--<p class="color_99">已选择 {{selectedNum}} 项</p>-->
      <!--<p class="color_one cursor_hover" @click="selectPage">全选本页</p>-->
      <!--<p class="color_warn cursor_hover" @click="clearSelection">清除选项</p>-->
      <!--<p class="color_one border-solid-color-one cursor_hover" @click="getSelected" v-hasPermi="['laboratory:record:export']">立即导出</p>-->
      <!--<p class="color_one border-solid-color-one cursor_hover" @click="handleExport" v-hasPermi="['laboratory:record:export']">导出全部</p>-->
      <!--</div>-->
      <el-table v-loading="loading" border :data="recordList" @selection-change="handleSelectionChange" ref="multipleTable" :row-key="getRowKeys">
        <!--<el-table-column type="selection" width="55" align="center" :reserve-selection="true" />-->
        <el-table-column label="姓名" align="left" prop="nickName"  width="140"/>
        <el-table-column label="学院" align="left" prop="deptName" show-overflow-tooltip width="200"/>
        <el-table-column label="实验室" align="left" prop="subjectName" show-overflow-tooltip/>
        <el-table-column label="进入时间" align="left" prop="inTime" width="200">
          <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.inTime, '{y}-{m}-{d}') }}</span>-->
          <!--</template>-->
        </el-table-column>
        <el-table-column label="离开时间" align="left" prop="outTime" width="200">
          <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>-->
          <!--</template>-->
        </el-table-column>
        <el-table-column label="停留时间" align="left" prop="intervalTime" width="200"/>
        <!--<el-table-column label="操作" align="left" 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-hasPermi="['laboratory:record:query']"-->
              <!--&gt;查看</p>-->
            <!--</div>-->
          <!--</template>-->
        <!--</el-table-column>-->
      </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="nickName" >
            <el-input v-model="form.nickName" placeholder="无" disabled/>
          </el-form-item>
          <el-form-item label="学院" prop="deptName" >
            <el-input v-model="form.deptName" placeholder="无" disabled/>
          </el-form-item>
          <el-form-item label="实验室" prop="subjectName" >
            <el-input v-model="form.subjectName" placeholder="无" disabled/>
          </el-form-item>
          <el-form-item label="进入时间" prop="inTime" >
            <el-input v-model="form.inTime" placeholder="无" disabled/>
          </el-form-item>
          <!--<el-form-item label="进入时间" prop="inTime" >-->
          <!--<el-date-picker clearable size="small"-->
          <!--disabled-->
          <!--v-model="form.inTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="无">-->
          <!--</el-date-picker>-->
          <!--</el-form-item>-->
          <el-form-item label="离开时间" prop="outTime" >
            <el-input v-model="form.outTime" placeholder="无" disabled/>
          </el-form-item>
          <!--<el-form-item label="离开时间" prop="outTime" >-->
          <!--<el-date-picker clearable size="small"-->
          <!--disabled-->
          <!--v-model="form.outTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="无">-->
          <!--</el-date-picker>-->
          <!--</el-form-item>-->
          <el-form-item label="停留时间" prop="remark" >
            <el-input v-model="form.intervalTime" placeholder="无" disabled/>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <my-record v-if="pageType == 2"></my-record>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { listRecord, getRecord, delRecord, addRecord, updateRecord, subjectDictList } from "@/api/laboratory/record";
import { listDepartments } from "@/api/system/dept";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import myRecord from "./myRecord.vue";

export default {
  name: "Record",
  components: {
    Treeselect,
    myRecord
  },
  data() {
    return {
      pageType:1,
      tableButtonType:this.hasPermiDom(['laboratory:record:query']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验室进出记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: [],
      //  进入或离开时间
      inOutTypeOptions: [{
        value: '1',
        label: '进入时间'
      }, {
        value: '2',
        label: '离开时间'
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        nickName: null,
        subjectId: null,
        deptId: null,
        deptName: null,
        inTime: null,
        outTime: null,
        userId: null,
        inOutType: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //实验室列表
      subjectOptions: [],
      //表格扩展选择器---需要在@selection-change绑定的方法内监控selection数组长度
      selectedNum:0,
      //进入时间
      dateRange:[],
    };
  },
  created() {
    this.getList();
    this.getDeptList();
    // this.getSubjectDictList();
  },
  methods: {
    clickPage(type){
      if(this.pageType != type){
        this.pageType = type;
      }
    },
    //选中导出
    getSelected(){
      if(!this.ids[0]){
        Message({
          message: "请选择要导出的数据",
          type: 'error'
        });
        return
      }
      let obj = JSON.parse(JSON.stringify(this.queryParams));
      obj.accessIds = this.ids;
      this.download('laboratory/record/export', {
        ...obj
      }, `进出记录.xlsx`)
    },
    /* 全部导出按钮操作 */
    handleExport() {
      let obj = JSON.parse(JSON.stringify(this.queryParams));
      obj.accessIds = '';
      this.download('laboratory/record/export', {
        ...obj
      }, `进出记录.xlsx`)
    },
    /** 查询实验室字典列表 */
    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;
      });
    },
    //=========表格扩展选择器方法---开始=========
    //选择本页
    selectPage(){
      console.log(this.$refs.multipleTable)
      this.$refs.multipleTable.toggleAllSelection()
      // console.log(this.$refs.multipleTable.selection)
    },
    //清除选择
    clearSelection(){
      this.$refs.multipleTable.clearSelection()
    },
    /*===记录勾选数据===
      需要再el-table 添加  :row-key="getRowKeys"
      需要在selection 添加 :reserve-selection="true"
    */
    getRowKeys(row) {
      return row.id
    },
    //=========表格扩展选择器方法---结束=========
    /** 查询实验室进出记录列表 */
    getList() {
      this.loading = true;
      if(this.dateRange&&this.dateRange.length>0) {
        this.queryParams.inTime = this.dateRange[0];
        this.queryParams.outTime = this.dateRange[1];
        this.queryParams.inOutType = 1
      } else {
        this.queryParams.inTime = null;
        this.queryParams.outTime = null;
        this.queryParams.inOutType = null
      }
      listRecord(this.queryParams).then(response => {
        this.recordList = 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,
        joinUserId: null,
        subjectId: null,
        deptId: null,
        deptName: null,
        inTime: null,
        outTime: 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.resetForm("queryForm");
      this.$set(this,'subjectOptions',[]);
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize:20,
        nickName:"",
        deptId:"",
        subjectId: '',
        inOutType: '',
        inTime: '',
        outTime: '',
      });
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedNum = selection.length;
      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();
      this.form = row;
        this.open = true;
        this.title = "记录详情";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(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 delRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>

<style scoped lang="scss">
  .record {
    flex: 1;
    display: flex!important;
    flex-direction: column;
    overflow: hidden;
    .record-page{
      flex: 1;
      display: flex!important;
      flex-direction: column;
      box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
      margin:5px 20px 20px 10px;
      border-radius:10px;
      padding:20px!important;
      overflow: hidden;
    }
    .el-row{
      margin-bottom:20px;
    }
    .sheet-expand-box{
      height:40px;
      display:flex;
      background:rgba(1,131,250,0.1);
      border-radius: 5px;
      margin-bottom:18px;
      font-size:14px;
      i{
        margin:10px 16px 0 14px;
        height:20px;
        width:20px;
        font-size:20px;
        display: block;
      }
      p{
        margin:0;
      }
      p:nth-child(2){
        width:132px;
        font-size:14px;
        height:40px;
        line-height:40px;
      }
      p:nth-child(3){
        width:60px;
        font-size:14px;
        height:40px;
        line-height:40px;
        margin-right:20px;
      }
      p:nth-child(4){
        width:60px;
        font-size:14px;
        height:40px;
        line-height:40px;
        margin-right:20px;
      }
      p:nth-child(5){
        font-size:14px;
        line-height:30px;
        width: 100px;
        height: 30px;
        border-radius: 6px;
        text-align: center;
        margin-top:5px;
        margin-right:20px;
      }
      p:nth-child(6){
        font-size:14px;
        line-height:30px;
        width: 100px;
        height: 30px;
        border-radius: 6px;
        text-align: center;
        margin-top:5px;
      }
    }
  }
</style>
