<template>
  <!-- 烟感设备管理-告警监测 -->
  <!-- <vue-scroll style="height: 100%"> -->
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <el-form :inline="true" size="medium" class="demo-form-inline">
        <el-form-item label="设备名称">
          <el-select v-model="devName" placeholder="请选择">
            <el-option
              :label="item.devName"
              :value="item.devSn"
              v-for="(item, index) in towerList"
              :key="item.devSn"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间段">
          <el-date-picker
            v-model="time"
            type="daterange"
            value-format="yyyy-MM-dd"
            :range-separator="$t('message.lifter.to')"
            :start-placeholder="$t('message.lifter.startDate')"
            :end-placeholder="$t('message.lifter.deadline')"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit" plain>查询</el-button>
          <el-button type="warning" plain @click="refreshBtn">刷新</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table
        :data="tableData"
        class="tables"
        style="width: 100%"
        height="600px"
      >
        <el-table-column prop="location" align="center" label="安装位置">
        </el-table-column>
        <el-table-column prop="devName" align="center" label="设备名称">
        </el-table-column>

        <el-table-column prop="devSn" align="center" label="设备编号">
        </el-table-column>
        <el-table-column prop="deviceStatus" align="center" label="设备状态">
          <template slot-scope="scope">{{
            scope.row.deviceStatus == 0 ? '报警恢复' : '报警'
          }}</template>
        </el-table-column>

        <el-table-column
          prop="alarmDetailStatus"
          align="center"
          label="警情状态"
        >
        </el-table-column>
        <el-table-column prop="alarmTime" align="center" label="报警时间">
        </el-table-column>

        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <div class="tableBtns">
              <div
                @click="dispBtn(scope.row.id)"
                class="operationText"
                v-if="scope.row.handleDone == 0"
              >
                <span style="background: #5181f6; color: #fff; width: 60px"
                  >待处置</span
                >
              </div>
              <div @click="closedBtn(scope.row)" class="operationText" v-else>
                <span style="background: #a2a4af; color: #fff; width: 60px"
                  >已闭合</span
                >
              </div>
              <div class="operationText last" @click="detailBtn(scope.row)">
                <i
                  class="el-icon-tickets"
                  style="color: #8dacfa; font-size: 16px; margin-right: 2px"
                ></i>
                <span>详情</span>
              </div>

              <div @click="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(total)"
        background
      ></el-pagination>
    </div>
    <!-- 处置记录弹框-->
    <el-dialog
      :modal-append-to-body="false"
      @close="close"
      :title="disTitle"
      :visible.sync="recordShow"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-width="120px"
          class="dialogFormBox"
        >
          <el-form-item label="操作人" v-if="disTitle == '处置记录'">
            <el-input v-model="addEditForm.operateName" disabled></el-input>
          </el-form-item>
          <el-form-item label="操作时间" v-if="disTitle == '处置记录'">
            <el-input v-model="addEditForm.operateTime" disabled></el-input>
          </el-form-item>
          <el-form-item label="处置结果" prop="handleResult">
            <el-radio
              v-model="addEditForm.handleResult"
              :label="1"
              :disabled="disTitle == '处置记录'"
              >已处置</el-radio
            >
            <el-radio
              v-model="addEditForm.handleResult"
              :label="2"
              :disabled="disTitle == '处置记录'"
              >误报忽略</el-radio
            >
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input
              :disabled="disTitle == '处置记录'"
              type="textarea"
              :rows="2"
              v-model="addEditForm.description"
              :placeholder="disTitle == '处置记录' ? '' : '请输入'"
            ></el-input>
          </el-form-item>
          <el-form-item label="上传图片" prop="image">
            <el-upload
              :disabled="disTitle == '处置记录'"
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              :file-list="fileList"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              icon="el-icon-circle-close"
              size="medium"
              @click="recordShow = false"
              >取消
            </el-button>
            <el-button
              @click="addBtn"
              type="primary"
              icon="el-icon-circle-check"
              size="medium"
              v-if="disTitle == '处置详情' || disTitle == '修改处置记录'"
              >确定
            </el-button>
            <el-button
              v-if="disTitle == '处置记录'"
              type="primary"
              icon="el-icon-edit-outline"
              size="medium"
              @click="editRecord"
              >修改
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <!-- 详情弹框-->
    <el-dialog
      :modal-append-to-body="false"
      title="详情"
      :visible.sync="detailsShow"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          label-width="120px"
          class="dialogFormBox"
        >
          <el-form-item label="安装位置">
            <el-input
              v-model="detailData.location"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="设备名称">
            <el-input
              v-model="detailData.devName"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="设备编号">
            <el-input
              v-model="detailData.devSn"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="设备状态">
            <el-input
              :placeholder="detailData.deviceStatus == 0 ? '报警恢复' : '报警'"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="警情状态">
            <el-input
              v-model="detailData.alarmDetailStatus"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="报警时间">
            <el-input
              v-model="detailData.alarmTime"
              disabled
            ></el-input>
          </el-form-item>
          <div class="dialog-footer">
            <el-button
              @click="detailsShow = false"
              type="primary"
              size="medium"
              >关闭
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
  <!-- </vue-scroll> -->
</template>
<script>
import {
  deleteSmokeDevAlarmApi,
  getSmokeDevAlarmPageApi,
  editSmokeDevAlarmApi,
  getSmokeDevAlarmListApi,//设备下拉
} from "@/assets/js/api/edgeProtection";
export default {
  data() {
    return {
      detailData:{},
      disTitle: '',
      fileList: [],
      dialogShow: false,//处置详情弹框
      recordShow: false,//处置记录弹框
      detailsShow: false,//详情弹框
      addEditForm: {
        operateName: '',//操作人
        operateTime: '',//操作时间
        handleResult: 0,
        description: '',
        handleDone: 0,
        image: '',
        id: '',
      },
      addEditRules: {
        handleResult: [
          { required: true, message: '必填', trigger: "blur" },
          { required: true, message: '必填', trigger: "change" }
        ],
      },
      projectSn: "",
      towerList: [],
      devName: "",
      time: ['', ''],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
    };
  },
  created() {
    this.projectSn = this.$store.state.projectSn;
    this.getTowerList();
    this.getListData();
  },
  methods: {
    //获取设备下拉
    getTowerList() {
      let data = {
        projectSn: this.projectSn,
      };
      getSmokeDevAlarmListApi(data).then((res) => {
        if (res.code == 200) {
          this.towerList = res.result;
        }
      });
    },
    //查询数据
    getListData() {
      let data = {
        queryStartTime: this.time.length > 0 ? this.time[0] : "",
        queryEndTime: this.time.length > 0 ? this.time[1] : "",
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        projectSn: this.projectSn,
        devSn: this.devName,
      };
      getSmokeDevAlarmPageApi(data).then((res) => {
        if (res.code == 200) {
          console.log('查询告警监测数据', res);

          this.tableData = res.result.records;
          this.total = res.result.total;
          res.result.records.map((item) => {
            if (item.image != null) {
              item.image = JSON.parse(item.image)
              if (
                item.image.length !== 0 &&
                !item.image[0].url.includes(this.$store.state.FILEURL)
              ) {
                item.image[0].url =
                  this.$store.state.FILEURL + item.image[0].url
              }
            } else {
              item.image = []
            }

          })
        }
      });
    },
    //删除
    deleteDev(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteSmokeDevAlarmApi({ id: obj.id }).then((result) => {
            if (result.success) {
              this.$message.success(result.message)
              this.getListData()
            }
          })
        })
        .catch(() => { })
    },
    //点击待处置
    dispBtn(val) {
      this.disTitle = '处置详情'
      this.recordShow = true
      this.addEditForm.id = val
      this.addEditForm.handleResult=1
    },
    //点击已闭合
    closedBtn(obj) {
      this.recordShow = true
      this.disTitle = '处置记录'
      this.fileList = obj.image
      this.addEditForm = JSON.parse(JSON.stringify(obj))
    },
    //点击详情修改
    editRecord() {
      this.disTitle = '修改处置记录'
      this.recordShow = true
    },
    //待处置提交
    addBtn() {
      this.processTheFile()
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      params.handleDone = 1
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.disTitle == '处置详情' || this.disTitle == '修改处置记录') {
            editSmokeDevAlarmApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getListData()
              }
            })
          }
          this.recordShow = false
        } else {
          return false
        }
      })
    },
    //查询数据
    onSubmit() {
      this.getListData();
    },
    //查看详情
    detailBtn(val) {
      this.detailData=val
      this.detailsShow = true
      console.log('查看详情', val);
      this.addEditForm = JSON.parse(JSON.stringify(val))
      this.fileList = val.image
    },

    //刷新
    refreshBtn() {
      this.time = [];
      this.pageNo = 1;
      this.pageSize = 10;
      this.devName = "";
      this.getListData();
    },
    processTheFile() {
      //处理el上传的文件格式(结构)
      this.addEditForm.image = []
      this.fileList.map((item) => {
        if (!item.response) {
          this.addEditForm.image.push(item)
        } else if (item.response) {
          this.addEditForm.image.push({
            name: item.response.data[0].filename,
            url: item.response.data[0].imageUrl
          })
        }
      })
      this.addEditForm.image = JSON.stringify(this.addEditForm.image)

    },
    handleSuccess(response, file, fileList) {
      console.log('图片上传成功', fileList);
      this.fileList = fileList;
    },
    handleRemove(response, fileList) {
      this.fileList = fileList;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getListData();
    },
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getListData();
    },
    close() {
      this.addEditForm = {}
      this.fileList = []
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
  },
};
</script>

<style lang="less" scoped>
</style>