<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <date-range-picker
          type="datetimerange"
          v-model="query.datatimevalue"
          class="date-item"
          style="width: 400px !important"
        />
        <el-select
          v-model="query.station"
          size="small"
          placeholder="请选择报警场站"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.behaviorType"
          size="small"
          placeholder="请选择行为分类"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.dealStatus"
          size="small"
          placeholder="请选择处理状态"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission"></crudOperation>
    </div>
    <!--表单组件-->
    <alarm-detail-dialog :show.sync="dialogShow" :form="form" :isDeal="isDeal" />
    <!--表格渲染-->
    <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 alarmManageTableOptions"
        :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
        v-if="checkPer(['admin', 'alarmManage:edit', 'alarmManage:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="permission.edit"
            size="mini"
            type="primary"
            icon="el-icon-setting"
            style="margin-right: 5px"
            @click="openDealDialog(scope.row)"
          />
          <template v-if="scope.row.imageUrl">
            <el-button
              v-permission="permission.edit"
              size="mini"
              type="primary"
              icon="el-icon-view"
              style="margin-right: 5px"
              @click="openImage(scope.row)"
            />
            <el-image
              :ref="'img' + scope.row.id"
              v-show="false"
              fit="contain"
              :preview-src-list="[
                `${process.env.VUE_APP_BASE_API}${item.imageUrl.replace('\/\/', '/')}`,
              ]"
            />
          </template>
          <!-- <udOperation :data="scope.row" :permission="permission" /> -->
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudLabel from '@/api/positioning/label'
import { getOptions } from '@/api/positioning/public'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { alarmManageTableOptions } from '../tableConfig'
import DateRangePicker from '@/components/DateRangePicker'
import Dayjs from 'dayjs'
import AlarmDetailDialog from '../../components/AlarmDetailDialog'
const defaultForm = {
  id: null,
  alarmTime: '',
  location: '',
  alarmContent: '',
  dealStatus: '',
  dealPerson: '',
  dealTime: '',
  dealContent: '',
  imageUrl: '',
}

export default {
  name: 'alarmManage',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, AlarmDetailDialog },
  cruds() {
    return CRUD({
      title: 'AI设备',
      url: 'positioning/label/getList',
      listOrder: [],
      crudMethod: { ...crudLabel },
      optShow: {},
      query: {
        datatimevalue: [
          Dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
          Dayjs().format('YYYY-MM-DD HH:mm:ss'),
        ],
      },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {
    this.getOptions()
  },
  data() {
    return {
      alarmManageTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'alarmManage:add'],
        edit: ['admin', 'alarmManage:edit'],
        del: ['admin', 'alarmManage:del'],
      },
      rules: {
        name: [{ required: true, message: '请选择设备名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
      },
      optionsObj: {
        Build: [],
      },
      dialogShow: false,
      isDeal: false,
    }
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.form.buildId = this.query.buildId
      if (this.form.id) {
        delete this.form.id
      }
    },
    openDealDialog(row) {
      this.isDeal = true
      this.dialogShow = true
      this.form = row
    },
    openImage(row) {
      let curImg = this.$refs[`img${row.id}`][0]
      // console.log(this.$refs[`img${data.id}`]);
      this.$nextTick(() => {
        curImg.clickHandler()
      })
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) {
            this.optionsObj[key] = res.data.content
            if (key == 'Build') {
              this.query.buildId = this.optionsObj.Build[0].value
              this.crud.toQuery()
            }
          }
        } catch (error) {
          console.log(error)
        }
      })
    },
  },
}
</script>

<style></style>
