<!--个人实验室进出记录-->
<template>
  <div class="app-container record-my">
    <div class="top-title-box">
      <p>我的进出记录</p>
      <p class="right-button reset-button-one" @click="backPage">返回</p>
    </div>
    <div class="record-my-page">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="学院" prop="deptId" label-width="48px">
          <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="subjectId" label-width="58px">
          <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="inOutType">-->
        <!--<el-select v-model="queryParams.inOutType" placeholder="请选择时间类型" style="width:180px;">-->
        <!--<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="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="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" style="margin:0 20px;" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
      </el-form>
      <el-table style="margin:20px 0 10px 0;" v-loading="loading" border :data="recordList" @selection-change="handleSelectionChange" ref="multipleTable" :row-key="getRowKeys">
        <el-table-column label="实验室" align="left" prop="subjectName" />
        <el-table-column label="学院" align="left" prop="deptName" />
        <el-table-column label="进入时间" align="left" prop="inTime">

        </el-table-column>
        <el-table-column label="离开时间" align="left" prop="outTime">

        </el-table-column>
        <el-table-column label="停留时间" align="left" prop="intervalTime" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        layout="total, prev, pager, next, sizes, jumper"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改实验室进出记录对话框 -->
    <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="出进人员id" prop="joinUserId">
          <el-input v-model="form.joinUserId" 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="inTime">
          <el-date-picker clearable size="small"
            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-date-picker clearable size="small"
            v-model="form.outTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择离开时间">
          </el-date-picker>
        </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 { listRecord, getRecord, delRecord, addRecord, updateRecord,myListRecord,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";

export default {
  name: "Record",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验室进出记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        joinUserId: null,
        subjectId: null,
        deptId: null,
        deptName: null,
        inTime: null,
        outTime: null,
        userId: null,
        inOutType: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //实验室列表
      subjectOptions: [],
      //  进入或离开时间
      inOutTypeOptions: [{
        value: '1',
        label: '进入时间'
      }, {
        value: '2',
        label: '离开时间'
      }],
      //表格扩展选择器---需要在@selection-change绑定的方法内监控selection数组长度
      selectedNum:0,
      dateRange:[],
    };
  },
  created() {
    this.getList();
    this.getDeptList();
  },
  methods: {
    backPage(){
      this.$parent.clickPage(1);
    },
    /** 查询实验室字典列表 */
    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
      }
      myListRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 查询学院列表 */
    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();
    },
    // 表单重置
    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.queryParams = {};
      // this.resetForm("queryForm");
      this.dateRange = [];
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize: 10,
        deptId:"",
        subjectId: '',
        inOutType: '',
        inTime: '',
        outTime: '',
      });
      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();
      const id = row.id || this.ids
      getRecord(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) {
            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(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/record/export', {
        ...this.queryParams
      }, `laboratory_record.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .record-my {
    flex:1;
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    *{
      margin:0;
    }
    .top-title-box{
      display: flex;
      border-bottom:1px solid #E0E0E0;
      p:nth-child(1){
        flex:1;
        line-height:80px;
        font-size:18px;
        color:#0045AF;
        margin-left:20px;
      }
      p:nth-child(2){
        margin:20px;
      }
    }
    .record-my-page{
      flex:1;
      display: flex!important;
      flex-direction: column;
      padding:20px;
      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>
