<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-news__banner}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.title"
                    placeholder="请输入标题"
                    clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $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 prop="title"
                         label="标题"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="imgUrl"
                         label="banner图片"
                         header-align="center"
                         align="center">
          <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"></el-table-column>
        <el-table-column prop="state"
                         label="上下架状态"
                         :formatter="bannerState"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="stateTime"
                         label="上下架时间"
                         header-align="center"
                         align="center"></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"></el-table-column>
        <el-table-column prop="updatedBy"
                         label="更新人"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="updatedTime"
                         label="更新时间"
                         header-align="center"
                         align="center"></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="scope.row.state === '1'"
                       @click="soltOut(scope.row.id)">下架</el-button>
            <el-button type="text"
                       size="small"
                       v-if="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) {
      console.log(row.partyFlag)
      return row.state === '1' ? '已上架' : row.state === '0' ? '已下架' : '未知'
    },
    soltOut (id) {
      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(() => { })
    },
    putaway (id) {
      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(() => { })
    },
    setBannerType: function (row) {
      this.bannerOptions.forEach(item => {
        console.log(item.id)
        if (item.id === row.bannerType) {
          return item.name
        }
      })
    },
    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(() => { })
    }
  }
}
</script>
