<template>
  <ele-modal
    :visible.sync="visible"
    title="处理"
    width="70%"
    append-to-body
    :maxable="true"
    @close="cancel"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      class="el-form-box"
    >
      <!-- 告警详细 -->
      <header-title title="告警详细"></header-title>
      <el-row>
        <el-col :span="8">
          <el-form-item label="告警名称：">
            <el-input disabled v-model="form.name" placeholder=" " />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="告警级别：">
            <el-input
              disabled
              :value="getLevel(form.alarmLevel)"
              placeholder=" "
            />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="告警设备：">
            <el-input disabled v-model="form.deviceName" placeholder=" " />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="告警描述：">
            <el-input disabled v-model="form.description" placeholder=" " />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="告警时间：">
            <el-input disabled v-model="form.alarmTime" placeholder=" " />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 告警处理意见 -->
      <header-title title="告警处理意见"></header-title>
      <el-row>
        <el-col :span="8">
          <el-form-item label="处理时间：" prop="handleTime">
            <el-date-picker
              v-model="form.handleTime"
              type="datetime"
              placeholder="时间 "
              style="width: 100%"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              :disabled="type == 'view'"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="处理方式：" prop="handleResult">
            <el-select
              v-model="form.handleResult"
              placeholder="请选择"
              :disabled="type == 'view'"
              style="width: 100%"
            >
              <el-option label="现场处理" :value="1"></el-option>
              <el-option label="远程处理" :value="2"></el-option>
              <el-option label="转派处理" :value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="处理意见：">
            <el-input
              v-model="form.remark"
              :disabled="type == 'view'"
              type="textarea"
              :rows="4"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="处理照片：">
            <fileMain
              v-model="form.attachmentUrl"
              :type="type == 'view' ? 'view' : 'add'"
            ></fileMain>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template v-slot:footer>
      <el-button
        type="primary"
        @click="save()"
        v-loading="loading"
        v-if="type != 'view'"
        >保存</el-button
      >
      <el-button @click="cancel">关闭</el-button>
    </template>
  </ele-modal>
</template>

<script>
  import dayjs from 'dayjs';
  import {
    getAlarmlogsheetById,
    updateAlarmlogsheet
  } from '@/api/warning/index.js';

  const defForm = {
    name: '',
    levelText: '',
    energyClassificationName: '',
    areaName: '',
    description: '',
    warningTime: '',
    handleTime: '',
    handleResult: '',
    remark: '',
    increaseDate: '',
    increaseUnit: 'degree',
    increaseDosage: '',
    qualitativeProcessing: 'needCheck',
    deviceName: '',
    attachmentUrl: []
  };

  export default {
    data() {
      return {
        visible: false,
        loading: false,
        form: { ...defForm },
        type: 'edit',
        fileList: [],
        rules: {
          handleTime: [
            { required: true, message: '请选择处理时间', trigger: 'change' }
          ],
          handleResult: [
            { required: true, message: '请选择处理方式', trigger: 'change' }
          ]
        }
      };
    },
    methods: {
      async open(row, type) {
        this.visible = true;
        this.type = type;
        if (row) {
          this.form = await getAlarmlogsheetById(row.id);
          if (this.type !== 'view' && !this.form.handleTime) {
            this.form.handleTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
          }
        }
      },
      getLevel(val) {
        return { 1: '轻微', 2: '中等', 3: '严重', 4: '紧急', 5: '致命' }[val];
      },
      cancel() {
        this.form = { ...defForm };
        this.visible = false;
      },

      save() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.form.handleUserName = this.$store.state.user.info.name;
            this.form.handleUserId = this.$store.state.user.info.userId;
            this.form.handleStatus = 2;
            updateAlarmlogsheet(this.form)
              .then((res) => {
                this.loading = false;
                this.$message.success('操作成功');
                this.cancel();
                this.$emit('reload');
              })
              .finally(() => {
                this.loading = false;
              });
          }
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .el-form-box {
    ::v-deep .el-input.is-disabled .el-input__inner {
      background-color: #f5f7fa;
      color: #606266;
    }
  }
</style>
