<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-news__banner}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataListSearch()">
        <el-form-item label="标题">
          <el-input v-model="dataForm.title" @keyup.native="btKeyUptitle"
                    placeholder="请输入标题"
                    clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getDataListSearch()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="addOrUpdateHandle()">{{ $t('add') }}</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="index"
                         width="50"
                         label="序号" align="center"></el-table-column> 
        <el-table-column prop="title"
                         label="标题"
                         header-align="center"
                         align="center"
                         min-width="400"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="imgUrl"
                         label="banner图片"
                         header-align="center"
                         align="center"
                         width="120">
          <template slot-scope="scope">
            <img :src="scope.row.imgUrl"
                 width="40"
                 height="40" />
          </template>
        </el-table-column>
        <el-table-column prop="bannerType"
                         label="banner类型"
                         :formatter="setBannerType"
                         header-align="center"
                         align="center"
                         width="120"></el-table-column>
        <el-table-column prop="state"
                         label="上下架状态"
                         :formatter="bannerState"
                         header-align="center"
                         align="center"
                         width="100"></el-table-column>
        <el-table-column prop="stateTime"
                         label="上下架时间"
                         header-align="center"
                         align="center"
                         width="180"></el-table-column>
        <!-- <el-table-column prop="createdBy"
                         label="创建人"
                         header-align="center"
                         align="center"></el-table-column> -->
        <el-table-column prop="createdTime"
                         label="创建时间"
                         header-align="center"
                         align="center"
                         width="180"></el-table-column>
        <el-table-column prop="position"
                         label="位置"
                         header-align="center"
                         :formatter="setPosition"
                         align="center"
                         width="100"></el-table-column>
        <el-table-column prop="updatedTime"
                         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 type="text"
                       size="small"
                       @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button> -->
            <el-button type="text"
                       size="small"
                       @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
            <el-button type="text"
                       size="small"
                       v-if="$hasPermission('banner:show:down') && scope.row.state === '1'"
                       @click="soltOut(scope.row.id)">下架</el-button>
            <el-button type="text"
                       size="small"
                       v-if="$hasPermission('banner:show:up') && scope.row.state === '0'"
                       @click="putaway(scope.row.id)">上架</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 './banner-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/news/banner/page',
        getDataListIsPage: true,
        deleteURL: '/news/banner',
        deleteIsBatch: true
      },
      dataForm: {
        id: '',
        title: ''
      },
      bannerOptions: [],
      positionList: []
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getListFromDict('bannerType')
    this.getListFromDict('bannerPosition')
  },
  methods: {
    bannerState: function (row, column) {
      return row.state === '1' ? '已上架' : row.state === '0' ? '已下架' : '未知'
    },
    soltOut (id) {
      this.$confirm(this.$t('prompt.info', { 'handle': '下架' }), this.$t('下架'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post(`/news/banner/soltOut/` + 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.getDataList()
            }
          })
        }).catch(() => { })
      }).catch(() => { })
    },
    putaway (id) {
      this.$confirm(this.$t('prompt.info', { 'handle': '上架' }), this.$t('上架'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post(`/news/banner/putAway/` + 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.getDataList()
            }
          })
        }).catch(() => { })
      }).catch(() => { })
    },
    setBannerType: function (row) {
      let choosenItem = this.bannerOptions.filter(item => item.dictValue === row.bannerType)[0]
      if (choosenItem) {
        return choosenItem.dictName
      } else {
        return row.bannerType
      }
    },
    setPosition: function (row) {
      let positionItem = this.positionList.filter(item => item.dictValue === row.position)[0]
      if (positionItem) {
        return positionItem.dictName
      } else {
        return row.position
      }
    },
    getListFromDict (dictType) {
      this.$http.get(`/sys/dict/listSimple/` + dictType).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (dictType === 'bannerType') {
          this.bannerOptions = res.data
        } else if (dictType === 'bannerPosition') {
          this.positionList = res.data
        }
      }).catch(() => { })
    },
    btKeyUptitle(e){
      e.target.value = e.target.value.replace(/[`~!#$%^&*()_\+=<>?:"{}|~！#￥%……&*（）={}|《》？：“”【】、；‘’，。、]/g, '')
      this.dataForm.title = e.target.value
    },
  }
}
</script>
