<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.category"
          filterable
          clearable
          size="small"
          placeholder="请选报警类型"
          class="filter-item"
          style="width: 160px"
          @change="categoryChangeHandle"
        >
          <el-option
            v-for="item in optionsObj.Category"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.type"
          filterable
          clearable
          size="small"
          placeholder="请选择二级类型"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.AlarmType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.dealStatus"
          filterable
          clearable
          size="small"
          placeholder="请选择状态"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option :value="1" label="已处理"></el-option>
          <el-option :value="0" label="未处理"></el-option>
        </el-select>
        <date-range-picker
          type="datetimerange"
          v-model="customs.timeRange"
          class="date-item"
          style="width: 400px !important"
          @change="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          type="success"
          icon="el-icon-chat-dot-square"
          size="mini"
          :loading="viewLoading"
          @click="alarmDealAll"
        >
          报警全部确认
        </el-button>
      </crudOperation>
    </div>
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      title="报警记录"
      :width="form.imageUrl ? '1200px' : '500px'"
    >
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form ref="form" :model="form" size="small" label-width="100px">
            <el-form-item label="报警时间" prop="eventTime">
              <el-input
                v-model="form.eventTime"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="报警类型" prop="categoryStr">
              <el-input
                v-model="form.categoryStr"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="二级类型" prop="typeStr">
              <el-input
                v-model="form.typeStr"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="报警内容" prop="content">
              <el-input
                type="textarea"
                :rows="2"
                v-model="form.content"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="处理状态" prop="dealStatusStr">
              <el-input
                v-model="form.dealStatusStr"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="处理时间" prop="dealTime">
              <el-input
                v-model="form.dealTime"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="处理人" prop="dealPersonName">
              <el-input
                v-model="form.dealPersonName"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="处理意见" prop="dealContent">
              <el-input
                type="textarea"
                :rows="4"
                v-model="form.dealContent"
                size="small"
                class="filter-item"
                style="width: 280px"
                :disabled="dialogType === 'view'"
                placeholder="请填写处理意见"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="16" v-if="form.imageUrl">
          <el-image
            fit="contain"
            :src="`${VUE_APP_BASE_API}${form.imageUrl.replace('\/\/', '/')}`"
            :preview-src-list="[`${VUE_APP_BASE_API}${form.imageUrl.replace('\/\/', '/')}`]"
          />
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="closeDialog"> 取消 </el-button>
        <el-button v-if="dialogType === 'deal'" type="primary" @click="submitDeal">
          确认
        </el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column
        v-for="item in allTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
      </el-table-column>
      <el-table-column label="操作" width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            :loading="crud.status.cu === 2"
            v-if="scope.row.dealStatus === '0'"
            size="mini"
            type="primary"
            icon="el-icon-thumb"
            @click="toDeal(scope.row)"
            style="margin-right: 5px"
          />
          <el-button
            :loading="crud.status.cu === 2"
            size="mini"
            type="success"
            icon="el-icon-view"
            @click="toView(scope.row)"
            style="margin-right: 5px"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import DateRangePicker from '@/components/DateRangePicker'
//import crudRailAreaInOutLog from '@/api/positioning/railAreaInOutLog'
import { getOptions, dealAlarm } from '@/api/alarmConfig/public'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { allTableOptions } from '../tableConfig'
import { dealAll } from '@/api/system/alarm'
const defaultForm = {
  id: '',
}

export default {
  name: 'AllAlarm',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '报警记录',
      url: 'alarm/data/getList',
      listOrder: [],
      //crudMethod: { ...crudRailAreaInOutLog },
      optShow: { add: false, edit: false, del: false },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
  },
  created() {
    this.getCategoryOptions()
  },
  data() {
    return {
      dialogVisible: false,
      dialogType: '',
      datetimeRange: undefined,
      allTableOptions,
      viewLoading: false,
      permission: {},
      optionsObj: {
        Category: [],
        AlarmType: [],
        DealStatus: [
          { label: '已处理', value: '1' },
          { label: '未处理', value: '0' },
        ],
      },
    }
  },
  methods: {
    toDeal(data) {
      this.form = data
      ;(this.dialogType = 'deal'), (this.dialogVisible = true)
    },
    toView(data) {
      this.form = data
      ;(this.dialogType = 'view'), (this.dialogVisible = true)
    },
    closeDialog() {
      this.dialogVisible = false
    },
    alarmDealAll() {
      try {
        this.$confirm('是否确认全部报警？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            await dealAll({ id: '0' })
            this.crud.toQuery()
          })
          .catch(() => {})
      } catch (err) {
        console.log(err)
      }
    },
    async submitDeal() {
      await dealAlarm({ id: this.form.id, dealContent: this.form.dealContent })
      this.dialogVisible = false
      this.crud.toQuery()
    },
    async categoryChangeHandle(data) {
      this.query.alarmType = ''
      if (data) await this.getAlarmTypeOptions(data)
      else this.optionsObj['AlarmType'] = []
      this.crud.toQuery()
    },
    async getCategoryOptions() {
      try {
        let res = await getOptions('Category')
        if (res.code === 20000) this.optionsObj['Category'] = res.data.content
      } catch (error) {
        console.log(error)
      }
    },
    async getAlarmTypeOptions(data) {
      try {
        this.optionsObj['AlarmType'] = []
        let res = await getOptions('AlarmType', { category: data })
        if (res.code === 20000) this.optionsObj['AlarmType'] = res.data.content
      } catch (error) {
        console.log(error)
      }
    },
  },
}
</script>

<style></style>
