<template>
  <div class="common-page device-manage">

    <div class="option page-row">
      <!--工具栏-->
      <div class="head-container">
        <!-- 搜索 -->
        <label class="el-form-item-label" style="font-weight: 500;">姓名</label>
        <el-input v-model="query.name" clearable size="small" placeholder="请输入姓名" style="width:180px;"/>
        <label class="el-form-item-label" style="font-weight: 500;">报警类型</label>
        <el-select v-model="query.alarmtype" placeholder="请选择">
          <el-option v-for="item in stuOpts" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <label class="el-form-item-label" style="font-weight: 500;">日期范围</label>
        <el-date-picker
          v-model="query.timeList"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <el-button size="mini" type="success" icon="el-icon-search" @click="toSearch">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="clearLimit">重置</el-button>
      </div>
    </div>

    <!-- 表格 -->
    <div class="panel-bottom page-row">
      <div class="ctin-box">
        <div class="content-within">
          <div class="content-fix">
            <div class="toolbar"></div>
            <div class="content">
              <el-table :data="tableData" v-loading="loading" border style="width:auto">
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="safetyhatmac" label="安全帽MAC" align="center"></el-table-column>
                <el-table-column label="状态" align="center">
                  <template slot-scope="scope">
                    <span>{{ scope.row.state=='1'?'报警':'解除报警' }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="报警位置" align="center">
                  <template slot-scope="scope">
                    <span>X：{{ scope.row.x}}</span><br/>
                    <span>Y：{{ scope.row.y}}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="time" label="报警时间" align="center"></el-table-column>
                <el-table-column label="报警类型" align="center">
                  <template slot-scope="scope">
                    <span v-show="scope.row.alarmtype===1">电子围栏</span>
                    <span v-show="scope.row.alarmtype===2">人员求救</span>
                    <span v-show="scope.row.alarmtype===3">电池欠压</span>
                  </template>
                </el-table-column>
                <el-table-column prop="jctime" label="解除报警时间" align="center"></el-table-column>
                <el-table-column prop="jcreason" label="解除报警原因" align="center"></el-table-column>
                <el-table-column label="操作" align="center" fixed="right">
                  <template slot-scope="scope">
                    <el-button v-show="scope.row.state===1" size="mini" style="margin:0" type="primary" icon="el-icon-close-notification" @click="toEdit(scope.row)"></el-button>
                    <el-button v-show="scope.row.state!==1" size="mini" style="margin:0" type="primary" icon="el-icon-close-notification" @click="toEdit(scope.row)" disabled></el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!--分页组件-->
              <el-pagination :total="total" :current-page="page" style="margin-top: 8px;"
                             layout="total, prev, pager, next, sizes" @size-change="sizeChange"
                             @current-change="pageChange"/>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 表单渲染 -->
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="cancelForm" :visible.sync="visible" title="解除报警" width="500px">
      <el-form :model="formData" :rules="rules" ref="formViewRef" :inline="true" label-width="180px">
        <el-form-item label="解除时长（h）：" class="form-cell" prop="jcHours" style="width:100%">
          <div class="cell-box">
            <el-input v-model="formData.jchours" class="cell-input" type="number"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="解除报警原因：" class="form-cell" prop="jcbjReason">
          <div class="cell-box">
            <el-input v-model="formData.jcreason" class="cell-input"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancelForm">取消</el-button>
        <el-button type="primary" @click="submitForm('formViewRef', formData)">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {Tools, HttpReq, Dates} from '@/assets/js/common.js';

export default {
  name: 'peopleAlarm',
  data() {
    return {
      title: '新增',
      visible: false,
      loading: false,
      page: 1,
      size: 10,
      total: 0,
      query: {},
      map: null,
      graphicLayer: null,
      stuOpts: [
        {label: '全部', value: undefined},
        {label: '电子围栏', value: 1},
        {label: '人员求救', value: 2},
        {label: '电池欠压', value: 3},
      ],
      tableData: [],
      formData: {
        jchours: '',
        jcreason: '',
      },
      rules: {},
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData();
    })
  },
  methods: {
    loadData() {
      var param = this.query;
      param.page = this.page - 1;
      param.size = this.size;
      param.vague = {name:param.name||undefined};
      param.exact = {alarmtype:param.alarmtype};
      param.sort = `date,desc`;
      param.daterange = undefined;
      if(param.timeList && param.timeList.length){
        param.daterange=param.timeList.join(',');
      }
      this.$nextTick(() => {
        //获取安检信息
        HttpReq.truckDispatching.propleGetPerabnormal(param).then((res) => {
          if (res.code == 200) {
            this.loading = false;
            this.tableData = res.body.list;
            this.total = res.body.total;
          }
        })
      })
    },
    toEdit(item) {
      this.visible = true;
      this.formData = {
        id: item.id,
        jchours: 1,
        jcreason: '',
      }
    },
    cancelForm() {
      this.visible = false;
    },
    // 点击搜索
    toSearch() {
      this.page = 1;
      this.loadData();
    },
    // 重置搜索
    clearLimit() {
      this.query = {};
      this.loadData()
    },
    pageChange(e) {
      this.page = e
      this.loadData()
    },
    sizeChange(e) {
      this.page = 1
      this.size = e
      this.loadData()
    },

    submitForm(validateName, item) {
      this.$refs[validateName].validate(valid => {
        if (valid) {
          HttpReq.truckDispatching.proplePutPerabnormal(item).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: 'success'
              });
              this.cancelForm();
              this.loadData()
            }
          })
        }
      });
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.cell-input {
  //min-width: 220px;
}

.form-cell {
  margin: 0 !important;
  width: 100%;
}

.grid-content {
  //border: 1px solid rgba(100, 100, 100, 0.1);
  padding: 5px 30px;
}
.cell-box {
  min-width: 120px;
}
.cell-box .el-input__inner {
  border: 1px solid rgba(100, 100, 100, 0.1);
}

.cell-box .el-input.is-disabled .el-input__inner {
  border: 1px solid rgba(100, 100, 100, 0.1);
  background: white;
  cursor: text;
}

.cell-box .el-input.is-disabled .el-input__icon {
  cursor: text;
}

.cell-box .el-icon-arrow-up:before {
  content: '';
}

.mapcontainer {
  height: 30vh;
  width: 680px;
  overflow: hidden;
}
</style>
