<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="5">
        <el-button v-permissions="['system:report:add']" icon="el-icon-plus" type="primary" @click="handleEdit($event)">
          添加
        </el-button>
        <el-button
          v-permissions="['system:report:delete']"
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete($event)">
          删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="19">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item label="报表名称" prop="reportName">
            <el-input v-model.trim="queryForm.reportName" clearable placeholder="请输入报表名称" />
          </el-form-item>
          <el-form-item label="报表代码" prop="reportCode">
            <el-input v-model.trim="queryForm.reportCode" clearable placeholder="请输入报表代码" />
          </el-form-item>
          <el-form-item label="报表类型" prop="reportType">
            <el-select v-model="queryForm.reportType" clearable placeholder="类型">
              <el-option label="数值指标" value="10" />
              <el-option label="数据报表" value="20" />
            </el-select>
          </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="height" @selection-change="setSelectRows">
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column align="center" label="报表类型" prop="reportType">
        <template #default="{ row }">
          {{ row.reportType === '10' ? '数值指标' : '数据报表' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="报表代码" prop="reportCode" show-overflow-tooltip />
      <el-table-column align="center" label="报表图标" prop="reportIcon" show-overflow-tooltip />
      <el-table-column align="center" label="报表名称" prop="reportName" show-overflow-tooltip />
      <!-- <el-table-column align="center" label="正在使用的角色" prop="" show-overflow-tooltip /> -->
      <el-table-column align="center" label="报表描述" prop="reportDesc" show-overflow-tooltip />
      <el-table-column align="center" label="备注" prop="remark" show-overflow-tooltip />

      <el-table-column align="center" label="创建时间" prop="createdTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" show-overflow-tooltip width="125">
        <template #default="{ row }">
          <el-button v-permissions="['system:report:edit']" type="text" @click="handleApplication(row)">应用</el-button>
          <el-button v-permissions="['system:report:edit']" type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button v-permissions="['system:report:application']" type="text" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image class="vab-data-empty" :src="require('@/assets/empty_images/data_empty.png')" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <edit ref="edit" @fetch-data="fetchData" />
    <rlist ref="reportlist" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import reportApi from '@/api/system/report'
  import Edit from './components/ReportEdit'
  import Rlist from './components/ReportList'

  export default {
    name: 'Post',
    components: { Edit, Rlist },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          reportName: undefined,
          reportCode: undefined,
          reportType: undefined,
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight(1)
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleApplication(row) {
        if (row.id) {
          this.$refs['reportlist'].showEdit(row)
        } else {
          this.$refs['reportlist'].showEdit()
        }
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await reportApi.doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            let ids = []
            for (let item of this.selectRows) {
              ids.push(item.id)
            }

            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await reportApi.doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      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.listLoading = true
        const {
          data: { list, total },
        } = await reportApi.getList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
