<template>
  <div>
    <manager-ui
      :loading="loading"
      :query-model="qureyParam"
      :query-items="queryItems"
      :static-data="staticData"
      :column-items="columnItems"
      :table-data="tableData"
      :pagination="page"
      :spread-threshold="1"
      :show-add-btn="showAddBtn"
      show-table-tools
      @create="addPage('add')"
      @selection-change="handleSelectionChange"
      @searchClick="searchClick"
      @search="search"
      @resetForm="reset"
    >
      <template slot="taskType" slot-scope="scope">
        <span>{{ scope.scope.row.taskType|toCase(staticData.taskTypeList,'dataId', 'dataName') }}</span>
      </template>
      <template slot="tableTools">
        <el-button v-rule-code="'auditReportConfig_del'" type="text" icon="el-icon-delete-solid">
          <span @click="batchDeleteClick">
            删除
          </span>
        </el-button>
      </template>
      <template slot="action" slot-scope="scope">
        <el-context-menu :menus="menus" :scope="scope.scope" :default="3" @oper="clickRow" />
      </template>
    </manager-ui>
    <Dialog
      ref="dialog"
      :report-id="reportId"
      :command="command"
      :statics="staticData"
      @success="search"
    />
    <oper-log-dialog ref="operDialog" :report-id="reportId" />
  </div>
</template>
<script>
import { checkRuleCodeExit } from '@ability/directive/rule-code'
import table from '@ability/mixins/table'
import staticsData from '@ability/mixins/staticsData'
import {
  reportConfigAdd,
  reportConfigUpd,
  getReportInfo,
  reportDel,
  getReportList,
  getOperRecordList } from '../api/report-config-manage'
import Dialog from './EditDialog'
import operLogDialog from './OperLogDialog'
export default {
  components: { Dialog, operLogDialog },
  mixins: [table, staticsData],
  data() {
    return {
      loading: false,
      // 查询参数
      qureyParam: {
        repoerName: '', // 报表名称
        createTimeStart: '', // 创建时间
        createTimeEnd: '', // 创建时间
        createTime: null
      },
      // 查询条件
      queryItems: [
        {
          type: 'input',
          key: 'repoerName',
          span: 12,
          label: '报表名称'
        },
        {
          dataType: 'datetimerange',
          type: 'datetime',
          key: 'createTime',
          span: 12,
          label: '创建时间',
          format: 'yyyy-MM-dd HH:mm:ss',
          valueFormat: 'yyyy-MM-dd HH:mm:ss'
        }
      ],
      // 列
      columnItems: [
        {
          type: 'selection',
          width: 50
        },
        {
          label: '报表名称',
          prop: 'repoerName'
        },
        {
          label: '审计分析数',
          prop: 'taskNum'
        },
        {
          label: '创建人',
          prop: 'creatUserName'
        },
        {
          label: '创建时间',
          prop: 'createTime'
        },
        {
          _key: 'action',
          prop: 'action',
          fixed: 'right',
          label: '操作',
          render: true,
          width: 150
        }
      ],
      tableData: [],
      menus: [
        {
          text: '查看',
          command: 'detail',
          icon: 'el-icon-s-order'
        },
        {
          text: '修改',
          command: 'modify',
          icon: 'el-icon-edit',
          ruleCode: 'auditReportConfig_upd'
        },
        {
          text: '删除',
          command: 'del',
          icon: 'el-icon-delete',
          iconColor: 'red'
        },
        {
          text: '操作日志',
          command: 'operlog',
          ruleCode: 'auditReportConfig_log',
          icon: 'el-icon-chat-line-square'
        }
      ],
      command: '',
      row: null, // 行内数据
      reportId: null,
      deleteData: {
        ids: []
      }
    }
  },
  computed: {
    showAddBtn() {
      // auditReportConfig_del 删除权限
      // return checkRuleCodeExit('auditReportConfig_add')
      return true
    },
    staticData() {
      return {
        // 任务类型
        taskTypeList: [
          {
            dataId: '0',
            dataName: '实时任务'
          },
          {
            dataId: '1',
            dataName: '历史任务'
          },
          {
            dataId: '2',
            dataName: '周期任务'
          }
        ],
        // 分析日志时序类型
        analyzeLogTimingList: [
          {
            dataId: '0',
            dataName: '时间范围'
          },
          {
            dataId: '1',
            dataName: '单位时间'
          }
        ],
        // 图表类型
        chartTypeList: [
          {
            dataId: '0',
            dataName: '分组统计表'
          },
          {
            dataId: '1',
            dataName: '趋势图'
          },
          {
            dataId: '2',
            dataName: '饼图'
          },
          {
            dataId: '3',
            dataName: '柱状图'
          },
          {
            dataId: '4',
            dataName: '地图'
          },
          {
            dataId: '5',
            dataName: '雷达图'
          },
          {
            dataId: '6',
            dataName: '一览表'
          }
        ],
        // 任务周期
        taskCycleList: [
          {
            dataId: '0',
            dataName: '按日'
          },
          {
            dataId: '1',
            dataName: '按周'
          },
          {
            dataId: '2',
            dataName: '按月'
          }
        ],
        // 日志状态
        taskStatusList: [
          {
            dataId: '0',
            dataName: '实时'
          },
          {
            dataId: '1',
            dataName: '创建'
          },
          {
            dataId: '2',
            dataName: '执行中'
          },
          {
            dataId: '3',
            dataName: '完成'
          },
          {
            dataId: '4',
            dataName: '等待下一次执行'
          },
          {
            dataId: '5',
            dataName: '失败'
          }
        ],
        // 日志分析时序
        analysisLogTimingTypeList: [
          {
            dataId: '0',
            dataName: '时间范围'
          },
          {
            dataId: '1',
            dataName: '单位时间'
          }
        ],
        logTypeList: this.logTypeListData,
        businessTree: this.businessTree
      }
    }
  },
  created() {
    this.search()
  },
  methods: {
    batchDeleteClick: function() {
      const ids = this.multipleSelection.map(item => {
        return item.repoerId
      })
      if (ids.length == 0) {
        this.$message.warning('请选择删除的记录')
        return false
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(ids)
      }).catch(() => {})
    },
    clickRow(command, scope) {
      this.reportId = scope.row.repoerId
      if (command == 'detail') { // 查看
        this.reportId = scope.row.repoerId
        this.command = 'detail'
        this.$refs.dialog.dialogVisible = true
      } else if (command == 'modify') { // 修改
        this.command = 'modify'
        this.$refs.dialog.dialogVisible = true
      } else if (command == 'del') { // 删除
        const arr = []
        arr.push(scope.row.repoerId)
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(arr)
        }).catch(() => { })
      } else if (command == 'operlog') { // 操作日志
        this.$refs.operDialog.operlogDialogVisible = true
      }
    },
    addPage(command, row) {
      this.command = 'add'
      this.$refs.dialog.dialogVisible = true
    },
    handleDelete(ids) {
      this.deleteData.ids = ids
      reportDel(this.deleteData).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.page.current = 1
          this.$message.success('删除成功')
          this.search()
        }
      })
    },
    searchClick() {
      this.search()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
      this.search()
    },
    search() {
      if (this.qureyParam.createTime != null) {
        this.qureyParam.createTimeEnd = this.qureyParam.createTime[1]
        this.qureyParam.createTimeStart = this.qureyParam.createTime[0]
      }
      this.loading = true
      getReportList(
        {
          logAudireportConfMap: this.qureyParam,
          page: this.page
        }
      ).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.tableData = resp.data.data.list
          const { current, size, total } = resp.data.data.page
          this.page.current = current
          this.page.size = size
          this.page.total = total
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
