<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-kpi__kpitimelimititem}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataListSearch()">
        <el-form-item label="项目类别" prop="categoryId">
          <div class="block">
            <el-cascader clearable v-model="categoryIds" placeholder="试试搜索：城市管理" :props="{ checkStrictly: true }" :options="categoryOptions" filterable></el-cascader>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getDataListSearch()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('kpi:kpitimelimititem:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('kpi:kpitimelimititem:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column label="序号" header-align="center" align="center" width="50px">
          <template slot-scope="scope">
            {{scope.$index+1}}
          </template>
        </el-table-column>
        <el-table-column prop="allCategoryNames" label="所属类别" header-align="center" align="center" min-width="250" show-overflow-tooltip></el-table-column>
        <el-table-column prop="validRespondTime" label="有效响应时间(h)" header-align="center" align="center" width="180"></el-table-column>
        <el-table-column prop="validCloseTime"  label="有效结案时间(h)" header-align="center" align="center" width="180"></el-table-column>
        <el-table-column prop="createdTime" label="创建时间" header-align="center" align="center" width="180"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('kpi:kpitimelimititem:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('kpi:kpitimelimititem:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
              :current-page="page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="limit"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="pageSizeChangeHandle"
              @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
  import mixinViewModule from '@/mixins/view-module'
  import AddOrUpdate from './timeLimitItem-add-or-update'
  export default {
    mixins: [mixinViewModule],
    data () {
      return {
        mixinViewModuleOptions: {
          getDataListURL: '/kpi/timeLimitItem/page',
          getDataListIsPage: true,
          deleteURL: '/kpi/timeLimitItem',
          deleteIsBatch: true
        },
        dataForm: {
          categoryId: ''
        },
        categoryIds: [],
        categoryOptions: []
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.getCategoryList()
    },
    watch: {
      'categoryIds': function (val) {
        if (val.length === 0) {
          this.dataForm.categoryId = ''
        } else if (val.length > 0) {
          this.dataForm.categoryId = this.categoryIds[this.categoryIds.length - 1]
        }
      }
    },
    methods: {
      getCategoryList () {
        return this.$http.get('/events/category/getCategoryTree').then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.categoryOptions = res.data.options
        }).catch(() => { })
      }
    }
  }
</script>
