<!--报警记录-->
<template>
  <div class="apply">
    <div class="apply_page" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" style="margin-top:20px;" :inline="true">
        <el-form-item label="关键字">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="姓名/联系电话/实验地点"
            clearable
            maxLength="30"
            size="small"
          />
        </el-form-item>
        <el-form-item label="报警时间" prop="dateRange">
          <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>
          <p class="inquire-button-one" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
        <el-form-item label="" prop="title" style="float: right">
          <el-dropdown @command="exportButton" style="float: left"    v-hasPermi="['airbottle:alarm:record:export']">
            <div class="form-dropdown-box">
              <img src="@/assets/ZDimages/personnelManagement/icon_jzgxx_sc.png">
              <p>导出</p>
              <img src="@/assets/ZDimages/personnelManagement/icon_jzggl_xljt.png">
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item style="border-bottom:1px solid #E0E0E0;margin:0 10px;color:#333;" :command="{command:1}">导出全部数据</el-dropdown-item>
              <el-dropdown-item style="margin:0 10px;color:#666;" :command="{command:2}">导出选中数据</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <p class="reset-button-one" style="margin-left: 12px;" @click="backPage">返回</p>
        </el-form-item>
      </el-form>
      <el-table border v-loading="loading" :data="tableData" @selection-change="handleSelectionChange" ref="multipleTable" :row-key="getRowKeys">
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="当前使用人" align="left" prop="currentUser">
          <template slot-scope="scope">
            <span >{{scope.row.currentUser?scope.row.currentUser:'-'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系方式" align="left" prop="currentPhone">
          <template slot-scope="scope">
            <span >{{scope.row.currentPhone?scope.row.currentPhone:'-'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="气瓶所有人" align="left" prop="owner"></el-table-column>
        <el-table-column label="联系方式" align="left" prop="ownerPhone"></el-table-column>
        <el-table-column label="实验地点" align="left" prop="location"></el-table-column>
        <el-table-column label="气体名称" align="left" prop="airName"></el-table-column>
        <el-table-column label="气体级别/规格" align="left" prop="configName"></el-table-column>
        <el-table-column label="气体余量" align="left" prop="currentPressure">
          <template slot-scope="scope">
            <span >{{scope.row.currentPressure}}Mpa</span>
          </template>
        </el-table-column>
        <el-table-column label="报警时间" align="left" prop="alarmTime"></el-table-column>
      </el-table>
      <div style="display: flex;height:32px;margin-top:15px;">
        <!--<p style="flex:4;"></p>-->
        <p style="text-align: left;margin:0;line-height:32px;margin-right:20px;font-size:14px;color:#999;">
          <i class="el-icon-warning" style="color:#0183FA;"></i>
          已选择 {{selectedNum}} 项
        </p>
        <div style="flex:5;">
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </div>
     <!--详情页面-->
    <detail-page v-if="pageType==2" :pageData2="pageData2"></detail-page>
  </div>
</template>
<script>
import { alarmRecordList, useRecordList } from '@/api/gasManage3_0/gasManage'
import { getToken } from "@/utils/auth";
import detailPage from "./abnormalRecordDetail.vue"
export default {
  name: "Approval",
  components: {
    detailPage
  },
  data() {
    return {
      //页面状态
      pageType:1,
      loading:false,
      headers: {
        Authorization: "Bearer " + getToken()
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue:'',
      },
      total:0,
      tableData:[],
      dateRange:[],
      pageData2:{},
      //导出
      //表格扩展选择器---需要在@selection-change绑定的方法内监控selection数组长度
      selectedNum:0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
    };


  },
  methods: {
    handleClick(index,row,doType){
      let _this=this;
      if(doType=='detail'){//查看
        _this.pageData2.item=row;
        _this.pageType=2;
      }else if(doType=='back'){//返回
        _this.pageType=1;
      }else if(doType=='export'){//导出
        this.download('airbottle/alarm/record/export', {
          ...this.queryParams
        }, `报警记录导出.xlsx`)
      }

    },
    /** 当前时间 */
    getCurrentTime () {
      const yy = new Date().getFullYear()
      const mm = new Date().getMonth() + 1
      const dd = new Date().getDate()
      const hh = new Date().getHours()
      const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
      const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
      return yy + '-' + mm + '-' + dd
    },
    /** 导出按钮操作 */
    exportButton(item) {
      let self = this;
      let currentDate = this.getCurrentTime()
      if(item.command == 1){
        self.$confirm(`确认导出全部数据?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          self.download('/airbottle/alarm/record/export/', {...self.queryParamsData}, '报警记录导出-'+currentDate+'.xlsx')
        }).catch(() => {})
      }else if(item.command == 2){
        if(self.ids.length>0) {
          self.$confirm(`确认导出选中数据?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(async () => {
            let ids = self.ids.join(',');
            let obj = {
              ids :ids
            }
            console.log("obj",obj)
            self.download(`/airbottle/alarm/record/export/`,obj, '报警记录导出-'+currentDate+'.xlsx')
          }).catch(() => {})
        }else {
          this.msgError('请选择要导出的数据')
        }
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedNum = selection.length;
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /*===记录勾选数据===
      需要再el-table 添加  :row-key="getRowKeys"
      需要在selection 添加 :reserve-selection="true"
    */
    getRowKeys(row) {
      return row.id
    },
      //返回
      backPage(){
        this.$parent.handleClick('','','back');
        this.$parent.outStatistics()
        this.$parent.gasApplyStatistics()
        this.$parent.useGasStatistics()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue = "";
      this.dateRange=[];
      this.queryParams.startTime=null;
      this.queryParams.endTime=null
      this.handleQuery();
    },
    getList(){
      let _this=this;
      if(this.dateRange&&this.dateRange.length>0) {
        this.queryParams.startTime=this.dateRange[0]
        this.queryParams.endTime=this.dateRange[1]
      } else {
        this.queryParams.startTime=null;
        this.queryParams.endTime=null
      }
      alarmRecordList(_this.queryParams).then( response => {
        let res=response.rows;
        _this.tableData=res;
        _this.total=response.total;
      });
    },
  },
  mounted() {
    this.getList();
  }
};
</script>

<style scoped lang="scss">
  /*导出按钮样式*/
  .form-dropdown-box{
    display: flex;
    margin:0;
    padding:0 10px;
    cursor: pointer;
    height:40px;
    img:nth-child(1){
      width:16px;
      height:16px;
      margin-top:12px;
    }
    p{
      width:47px;
      text-align: center;
      font-size:14px;
      margin:0;
      line-height:40px;
    }
    img:nth-child(3){
      width:10px;
      height:6px;
      margin-top:17px;
    }
  }
  .apply {
    flex:1;
    display: flex!important;
    flex-direction: column;
    .apply_page{
      flex:1;
      display: flex!important;
      flex-direction: column;
      box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
      padding:20px 20px 20px!important;
      border-radius:10px;

      .button-box{
        width:200px;
        display: flex;
      }
    }

  }
</style>
