<template>
  <div :class="$style.container">
    <SearchBar
      :placeholder="$t('module.report.reportList.reportName')"
      :input-width="200"
      @on-search="handleSearch"
    >
      <el-button type="success" icon="el-icon-plus" @click="handleAdd()">{{ "btn.add" | i18nFilter }}</el-button>
      <el-button icon="el-icon-arrow-left" @click="$router.go(-1)">{{ 'btn.back' | i18nFilter }}</el-button>
    </SearchBar>
    <el-table
      :data="tableDataList"
      :class="$style.table"
      :stripe="true"
      :header-cell-style="{background:'#eef1f6',color:'#606266',height: '56px', textAlign: 'center'}"
      :cell-style="{ textAlign: 'center' }"
      size="medium"
    >
      <el-table-column prop="index" :label="'tableHeader.index' | i18nFilter" width="80" fixed="left" />
      <el-table-column prop="fileName" :label="$t('module.report.reportList.reportName')" />
      <el-table-column prop="reportType" :label="$t('module.report.reportList.reportSort')" width="300">
        <template
          slot-scope="scope"
        ><el-select
          v-model="scope.row.category"
          :class="$style.select"
          filterable
          clearable
          @change="reportChange(scope.row)"
        >
          <el-option
            v-for="item in reportTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          /> </el-select></template>
      </el-table-column>
      <el-table-column prop="createTime" :label="$t('module.report.reportList.createTime')" width="220">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | commonFilter(timeFormatter) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('module.report.reportList.operation')" width="420" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="handleView(scope.row.fileName)"
          >{{ "btn.view" | i18nFilter }}</el-button>
          <el-button
            size="mini"
            type="info"
            icon="el-icon-edit"
            @click="handleDesign(scope.row.name)"
          >{{ "btn.design" | i18nFilter }}</el-button>
          <el-button
            size="mini"
            type="info"
            icon="el-icon-edit"
            @click="handleEdit(scope.row.id)"
          >{{ "btn.edit" | i18nFilter }}</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
          >{{ "btn.delete" | i18nFilter }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :hide-on-single-page="false"
      :class="$style.table"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next"
      :total="pageTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <Report-edit v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="handleSearch" />
  </div>
</template>
<script>
import moment from 'moment'
import { page, del, listCategory, update } from '@/api/report-manage/report.js'
import SearchBar from './components/search-bar'
import ReportEdit from './components/report-edit'
import i18n from '@/lang'
import { timeFormatter } from '@/filters'
export default {
  components: { SearchBar, ReportEdit },
  filters: {
    commonFilter: (source, filterType = a => a) => {
      return filterType(source)
    }
  },
  props: {},
  data() {
    return {
      reportTypeList: [],
      moment,
      tableDataList: [],
      pageTotal: 0,
      page: {
        current: 1,
        size: 10
      },
      param: {},
      addOrUpdateVisible: false,
      timeFormatter
    }
  },
  created() {
    this.init()
    this.handleSearch()
  },
  methods: {
    init() {
      listCategory({}).then(res => {
        this.reportTypeList = res.data
      })
    },
    handleSearch(param) {
      this.param = param
      console.log(param)
      page({ ...param, key: param && param.keywords, category: this.$route.query.sortId, ...this.page }).then(
        res => {
          if (!res.data) {
            return
          }
          this.pageTotal = res.data && res.data.total
          console.log(this.pageTotal)
          if (!res.data.records) {
            this.tableDataList = []
            return
          }
          this.tableDataList = res.data.records.map((m, index) => ({
            ...m,
            index: this.page.size * (this.page.current - 1) + index + 1
          }))
        }
      )
    },
    handleView(name) {
      this.$router.push({
        path: '/form/report-view',
        query: {
          redirect: '/form/report',
          name
        }
      })
    },
    handleAdd() {
      this.$router.push({
        path: '/form/report-design',
        query: {
          redirect: '/form/report'
        }
      })
    },
    handleDesign(name) {
      this.$router.push({
        path: '/form/report-design',
        query: {
          redirect: '/form/report',
          name
        }
      })
    },
    handleEdit(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.size = val
      this.handleSearch(this.param)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.current = val
      this.handleSearch(this.param)
    },
    handleDelete(id) {
      this.$confirm(
        i18n.t('deleteMsgBox.content'),
        i18n.t('deleteMsgBox.hint'),
        {
          confirmButtonText: i18n.t('btn.confirm'),
          cancelButtonText: i18n.t('btn.cancel'),
          type: 'warning'
        }
      ).then(async() => {
        del({ id }).then(res => {
          this.page.current = 1
          this.handleSearch(this.param)
          this.$message({
            type: 'info',
            message: i18n.t('deleteMsgBox.doneMsg')
          })
        })
      })
    },
    reportChange($e, row) {
      console.log($e)
      $e.url = $e.url || '-'
      update($e).then(res => {
        this.$message.info('更新成功！')
        this.handleSearch()
      })
    }
  }
}
</script>
<style lang="scss" module>
.container {
  .table {
    margin: 16px;
    width: 98.5%;
  }
}
</style>
