<template>
  <a-card>
    <sd-log-form
      v-if="initValues.length"
      :init-values="initValues"
      @search="(value) => (customFilterExpressions = value)"
    />
    <sd-data-table
      data-url="api/framework/v1/log-query/logs/admin"
      :filter-expressions="customFilterExpressions"
      :columns="columns"
      :actions="actions"
      :row-key="'id'"
    >
      <template v-slot:level="text">
        {{ text === 800 ? '一般' : text === 900 ? '错误' : text === 1000 ? '警告' : text }}
      </template>
    </sd-data-table>
    <a-modal v-if="detailDialog" v-model="detailDialog" title="详细信息" width="800px">
      <template slot="footer">
        <a-button
          @click="
            () => {
              detailDialog = false
              detailInfo = null
            }
          "
          >关闭</a-button
        >
      </template>
      <div v-for="(item, key) in detailInfo" :key="key" :class="$style.detail"
        ><div :class="$style.detailLabel">{{ key }}</div
        ><div :class="$style.detailInfo">{{ item }}</div></div
      >
    </a-modal>
  </a-card>
</template>

<script>
import TableColumnTypes from '@/common/services/table-column-types'
import TableActionTypes from '@/common/services/table-action-types'
import LogService from './log-service'
import sdLogForm from './sd-log-form.vue'
import components from './_import-components/sd-admin-logs-import'

export default {
  name: 'SdAdminLogs',
  metaInfo: {
    title: '管理员日志',
  },
  components: { ...components, sdLogForm },
  data() {
    const columns = [
      {
        title: 'id',
        dataIndex: 'id',
      },
      {
        title: '级别',
        dataIndex: 'level',
        scopedSlots: { customRender: 'level' },
      },
      {
        title: '日期',
        dataIndex: 'createdTime',
        sorter: true,
        sdRender: TableColumnTypes.dateTime,
      },
      {
        title: '操作者部门',
        dataIndex: 'deptName',
      },
      {
        title: '操作者',
        dataIndex: 'userName',
      },
      {
        title: '任务类别',
        dataIndex: 'taskType',
      },
      {
        title: '事件ID',
        dataIndex: 'eventId',
      },
      {
        title: '业务系统ID',
        dataIndex: 'businessSystemId',
        sdCandidate: true,
      },
      {
        title: '关键字',
        dataIndex: 'keyword',
      },
      {
        title: '信息',
        dataIndex: 'message',
      },
      {
        title: '来源',
        dataIndex: 'source',
      },
    ]
    return {
      initValues: [
        {
          name: 'createdTime',
          caption: '记录时间',
          dataType: 'selectlist',
          value: '',
          attr: {
            displayValue: '',
            controlType: 'DictType',
            selectListItem: [
              { label: '任何时间', value: '*' },
              { label: '前一个小时', value: '1h' },
              { label: '过去的12小时', value: '12h' },
              { label: '过去的24小时', value: '1d' },
              { label: '过去7天', value: '7d' },
              { label: '过去30天', value: '30d' },
            ],
          },
        },
        {
          attrFD: null,
          caption: '操作者部门',
          controlCat: -1,
          dataType: 'selectperson',
          defaultValue: null,
          htmlType: null,
          idProperty: false,
          index: 18,
          mobileVisible: true,
          mutable: true,
          name: 'deptId',
          readonly: false,
          required: false,
          specType: 'XcoaSpec',
          value: null,
          attr: {
            roots: [
              {
                id: '-1',
                name: '北京XX科技',
              },
            ],
            field: 'deptId',
            selectionType: 'GROUP',
          },
        },
        {
          attrFD: null,
          caption: '操作者',
          controlCat: -1,
          dataType: 'selectperson',
          defaultValue: null,
          htmlType: null,
          idProperty: false,
          index: 18,
          mobileVisible: true,
          mutable: true,
          name: 'userId',
          readonly: false,
          required: false,
          specType: 'XcoaSpec',
          value: null,
          attr: {
            roots: [
              {
                id: '-1',
                name: '北京XX科技',
              },
            ],
            field: 'userId',
            selectionType: 'USER',
          },
        },
        {
          name: 'taskType',
          caption: '任务类别',
          dataType: 'string',
          value: '',
        },
        {
          name: 'source',
          caption: '来源',
          dataType: 'string',
          value: '',
        },
        {
          name: 'keyword',
          caption: '关键字',
          dataType: 'string',
          value: '',
        },
        {
          name: 'eventId',
          caption: '事件ID',
          dataType: 'string',
          value: '',
        },
      ], // 查询条件表单默认数据
      customFilterExpressions: [], // 自定义查询条件
      columns,
      actions: [
        {
          label: '详细信息',
          id: 'read',
          type: TableActionTypes.inline,
          callback: (record) => {
            LogService.getSysLogRecordDetails({
              id: record.id,
              systemId: record.businessSystemId,
            }).then((res) => {
              const info = res.data.info
              const props = res.data.props
              const detailInfo = {}
              detailInfo['日志ID'] = info.id
              detailInfo['日志名称'] = info.name
              detailInfo['日志级别'] =
                info.level === 800
                  ? '一般'
                  : info.level === 900
                  ? '警告'
                  : info.level === 1000
                  ? '错误'
                  : info.level
              const createdTime = new Date(info.createdTime)
              detailInfo['日志日期'] = `${createdTime.getFullYear()}-${createdTime.getMonth() +
                1}-${createdTime.getDate()} ${createdTime.getHours()}:${createdTime.getMinutes()}`
              detailInfo['操作者所在部门'] = info.deptName
              detailInfo['操作者名称及账号'] = info.userName
              detailInfo['任务类别'] = info.taskType
              detailInfo['任务代码'] = info.code
              detailInfo['事件ID'] = info.eventId
              detailInfo['关键字'] = info.keyword
              detailInfo['描述信息'] = info.message
              if (props['access.remoteAddr']) {
                detailInfo['操作者IP'] = props['access.remoteAddr']
              }
              for (const key in props) {
                if (
                  key === 'access.remoteAddr' ||
                  key === '处理人' ||
                  key === '处理人账户' ||
                  key === ' 流程定义Id' ||
                  key === 'URI' ||
                  key === '发起人账户' ||
                  key === '浏览器' ||
                  key === '调用者IP'
                ) {
                  continue
                }
                if (key === '执行步骤的步骤Id') {
                  detailInfo['流程定义环节Id'] = props[key]
                } else {
                  detailInfo[key] = props[key]
                }
              }
              this.detailDialog = true
              this.detailInfo = detailInfo
            })
          },
        },
      ],
      detailDialog: false, // 详情弹窗
      detailInfo: null,
    }
  },
  methods: {},
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.detail {
  padding: $padding-sm 0;
  overflow: hidden;
  clear: both;
  border-bottom: 1px solid #e8e8ee;
  .detail-label {
    width: 150px;
    font-weight: bold;
    float: left;
  }
  .detail-info {
    width: calc(100% - 150px);
    float: left;
  }
}
</style>
