<template>
  <div class="log-error-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.log_error_name"
              clearable
              :placeholder="__('日志名称')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.log_error_info"
              clearable
              :placeholder="__('日志内容')"
            />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="log_error_date"
              :placeholder="__('选择日期')"
              type="date"
            >
              >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column fixed="left" type="expand">
        <template #default="{ row }">
          <div class="ms-table-expand">
            <p>
              {{ row.log_error_info }}
            </p>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('日志编号')"
        prop="log_error_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('日志类型')"
        prop="log_error_type"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('日志名称')"
        prop="log_error_name"
        show-overflow-tooltip
        width="600"
      />
      <el-table-column
        align="center"
        :label="__('日志文件')"
        prop="log_error_line"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :formatter="formatErrorTime"
        :label="__('日志时间')"
        prop="log_error_time"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('日志内容')"
        prop="log_error_info"
        show-overflow-tooltip
        width="400"
      />
      <el-table-column
        v-if="false"
        align="center"
        :formatter="formatDate"
        :label="__('日志日期')"
        prop="log_error_date"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('日志时间')"
        prop="log_time"
        show-overflow-tooltip
        width="180"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/log/error'
  import { renderTime, timestampToTime, timeToYMD } from '@/utils/date'

  export default {
    name: 'Error',
    components: {},
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        log_error_date: undefined,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          log_error_date: undefined,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatErrorTime(row) {
        const log_error_time = row.log_error_time
        return log_error_time ? timestampToTime(log_error_time) : ''
      },
      formatDate(row) {
        const log_error_date = row.log_error_date
        return log_error_date ? timeToYMD(log_error_date) : ''
      },
      formatTime(row) {
        const log_time = row.log_time
        return log_time ? renderTime(log_time) : ''
      },
      __,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.dealTimeParam()
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      dealTimeParam() {
        // 处理日期查询参数
        if (this.log_error_date) {
          this.queryForm.log_error_date = renderTime(this.log_error_date)
        } else {
          this.queryForm.log_error_date = undefined
        }
      },
    },
  }
</script>
