<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-heart__actbanner}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="状态">
          <el-select v-model="dataForm.status" placeholder="状态" clearable>
            <el-option label="上架" :value="1"></el-option>
            <el-option label="下架" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间"
                      prop="startTime">
          <el-date-picker v-model="dataForm.startTime"
                          type="date"
                          :picker-options="pickerBeginDateBefore"
                          value-format="yyyy-MM-dd"
                          format="yyyy-MM-dd"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="至"
                      label-width="25px"
                      prop="endTime">
          <el-date-picker v-model="dataForm.endTime"
                          type="date"
                          :picker-options="pickerBeginDateAfter"
                          value-format="yyyy-MM-dd"
                          format="yyyy-MM-dd"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column label="序号" type="index" show-overflow-tooltip align="center" width="50"></el-table-column>
        <el-table-column prop="title" label="标题" min-width="400" show-overflow-tooltip header-align="center" align="left"></el-table-column>
        <el-table-column prop="status" label="状态" width="70" :formatter="formatState" header-align="center" align="center"></el-table-column>
        <el-table-column prop="statusTime" label="上下架时间" width="180" header-align="center" align="center"></el-table-column>
        <el-table-column prop="browseNum" label="浏览数量" width="80" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createdTime" label="创建时间" width="180" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="85">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('heart:actbanner:grounding')" type="text" size="small" @click="changeGroundingHandle(scope.row)">{{ changeGroundingText(scope.row.status) }}</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 './actbanner-add-or-update'
import {debounce} from 'lodash'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/heart/actbanner/page',
        getDataListIsPage: true,
        deleteURL: '/heart/actbanner',
        deleteIsBatch: true
      },
      dataForm: {
        status: '',
        startTime: '',
        endTime: ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    changeGroundingText(status){
      let state = status
      if (state === '1') {
        return '下架'
      } else if (state === '0') {
        return '上架'
      }
    },
    formatState: function (row, column) {
      let state = row.status
      if (state === '1') {
        return '上架'
      } else if (state === '0') {
        return '下架'
      }
    },

    changeGroundingHandle: debounce(function (row) {

      this.$confirm(this.$t('prompt.info', { 'handle': this.changeGroundingText(row.status) }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.get(`/heart/actbanner/grounding/${row.id}`).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.getDataList()
            }
          })
        }).catch(() => {})
      }).catch(() => {})
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
