<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">{{ $t('errorLog.table.device_code') }}</label>
        <el-input
          v-model="query.device_code"
          clearable
          :placeholder="$t('errorLog.table.device_code')"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.error_code"
          clearable
          :placeholder="$t('errorLog.table.alarm_code')"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.error_info"
          clearable
          :placeholder="$t('errorLog.table.alarm_message')"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-date-picker
          v-model="value"
          type="datetimerange"
          style="height: 30.5px; display: inline-block; vertical-align: middle; margin: 0 3px 10px 0; line-height: 30.5px;"
          :end-placeholder="$t('errorLog.table.end_time')"
          :start-placeholder="$t('errorLog.table.start_time')"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions"
          @change="pickerChange"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-download"
          size="mini"
          :loading="downLoadDeviceErrorLogging"
          @click="doExportDeviceErrorLogging()"
        >
          {{ $t('common.Export') }}
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="device_code" :label="$t('errorLog.table.device_code')" />
        <el-table-column prop="error_code" :label="$t('errorLog.table.alarm_code')" />
        <el-table-column prop="error_info" :label="$t('errorLog.table.alarm_message')" />
        <el-table-column prop="error_time" :label="$t('errorLog.table.error_time')" />
        <el-table-column
          v-permission="['admin','acsDeviceErrorLog:edit','acsDeviceErrorLog:del']"
          :label="$t('table.operate')"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-edit="true"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudAcsDeviceErrorLog from '@/api/acs/history/acsDeviceErrorLog'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'

const defaultForm = { error_log_uuid: null, device_code: null, error_code: null, error_info: null, error_time: null }
export default {
  name: 'DeviceErrorLog',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备报警记录',
      url: 'api/deviceErrorLog',
      idField: 'error_log_uuid',
      sort: 'error_log_uuid,desc',
      crudMethod: {
        ...crudAcsDeviceErrorLog },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'deviceErrorLog:add'],
        edit: ['admin', 'deviceErrorLog:edit'],
        del: ['admin', 'adeviceErrorLog:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'device_code', display_name: '设备编码' },
        { key: 'error_code', display_name: '报警编码' },
        { key: 'error_info', display_name: '报警信息' }
      ],
      value: [],
      pickerOptions: {
        shortcuts: [{
          text: this.$t('monitor.lucence.the_past_week'),
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: this.$t('monitor.lucence.the_past_month'),
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: this.$t('monitor.lucence.the_past_three_months'),
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {

    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      console.log(this.query.value)
      return true
    },
    pickerChange() {
      if (this.value && this.value.length === 2) {
        this.query.start_time = this.value[0]
        this.query.end_time = this.value[1]
      } else {
        this.query.start_time = ''
        this.query.end_time = ''
      }
    },
    doExportDeviceErrorLogging() {
      this.downLoadTaskLogging = true
      const params = {
        device_code: this.query.device_code,
        error_code: this.query.error_code,
        error_info: this.query.error_info,
        start_time: this.query.start_time,
        end_time: this.query.end_time
      }
      const url = '/api/deviceErrorLog/download'
      download(url, params).then(result => {
        downloadFile(result, this.crud.title + '数据', 'csv')
        this.downLoadTaskTreeLogging = false
      }).catch(() => {
        this.downLoadTaskTreeLogging = false
      })
    }
  }
}
</script>

<style scoped>

</style>
