<!--  -->
<template>
  <div>
    <main-top :crumb="crumbData"></main-top>
    <div class="table-interface card">
      <div class="top-body">
        <div class="info-top">
          <div class="info-left">
            <span>按资讯类型查看：</span>
            <el-select style="width:120px;" v-model="region" placeholder="请选择">
              <el-option label="全部" value=""></el-option>
              <el-option label="海报轮播" :value="0"></el-option>
              <el-option label="视频轮播" :value="1"></el-option>
            </el-select>
            <span style="margin-left:20px;">资讯名称：</span>
            <el-input style="width:220px;margin-right:20px;" v-model="inputKey" placeholder="请输入资讯名称"></el-input>
            <el-button type="primary" @click="getInformation">查找</el-button>
            <el-button @click="inputKey=''">重置</el-button>
          </div>
          <div class="info-right">
            <el-button type="primary" @click="$router.push({name:'edit-information',params:{type:0}})">发布资讯</el-button>
          </div>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            v-for="(item, idx) in data"
            :key="idx"
            :prop="item.prop"
            :label="item.label"
          >
          <template slot-scope="scope">
            <img style="width:80px" v-if="item.prop === 'contentUrl'" :src="scope.row[item.prop]" alt="">
            <span v-else>{{scope.row[item.prop]}}</span>
          </template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope" v-if="scope.row.qiyong">
              <el-switch
                @change='changeState(scope.row)'
                v-model="scope.row.qiyong.st"
              >
              </el-switch>
              <span >{{scope.row.qiyong.text}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <span class="o-span" @click="goto(scope.row)">编辑</span>
              <span class="o-span" @click="del(scope.row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="e-pagination">
          <el-pagination @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="size" layout="prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';

import MainTop from '@/components/main-top'
import SearchBar from '@/components/search-bar'
const crumbData = [
  {
    name: '资讯管理',
    id: 2
  }
]
let zxlx = {
  0: '海报轮播',
  1: '视频轮播'
}

export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    MainTop,
    SearchBar
  },
  data () {
    let data = [
      {
        prop: 'orderNum',
        label: '展示顺序'
      },
      {
        prop: 'leixing',
        label: '资讯类型'
      },
      {
        prop: 'contentUrl',
        label: '缩略图'
      },
      {
        prop: 'name',
        label: '资讯名称'
      },
      {
        prop: 'date',
        label: '轮播时间'
      }
    ]

    return {
      crumbData,
      data,
      region: '',
      inputKey: '',
      currentPage: 1,
      size: 10,
      total: 0,
      tableData: []
    }
  },
  watch: {
    region () {
      this.refresh()
    },
    currentPage () {
      this.refresh()
    },
    size () {
      this.refresh()
    },
    inputKey () {
      this.refresh()
    }
  },
  mounted () {
    this.refresh()
  },
  methods: {
    goto (item) {
      window.localStorage.setItem('editInfo', JSON.stringify(item))
      this.$router.push({name: 'edit-information', params: {type: 1}})
      this.$bus.$emit('editinfo', item)
    },
    async changeState (item) {
      console.log(item)
      // let { name, id, typeId, equipmentId, description } = item
      // let data = { name, typeId, equipmentId, state: !item.qiyong.st ? 1 : 0, description, id }
      let {type, id, name, contentUrl, orderNum, startTime, endTime, description} = item
      let data = {name, id, type: Number(type), contentUrl, orderNum, state: !item.qiyong.st ? 1 : 0, startTime, endTime, description}
      this.$http.post('/information/updateInformation', data).then(res => {
        if (res.code === 0) {
          this.refresh()
        } else {
          this.$notify.error({
            title: '错误',
            message: res.message,
            duration: 2000
          })
        }
      })
    },
    refresh () {
      this.loading = true
      this.getInformation()
      this.loading = false
    },
    handleCurrentChange (val) {
      this.currentPage = val
    },
    fixState (state) {
      let obj = {}
      switch (Number(state)) {
        case 0:
          obj = {
            status: 0,
            text: '启用',
            class: 'zfb-success',
            st: true
          }
          break

        case 1:
          obj = {
            status: 1,
            text: '禁用',
            class: 'zfb-guanbi',
            st: false
          }
          break
        default:
          break
      }
      return obj
    },
    del (item) {
      let data = {
        id: item.id
      }
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/information/deleteInformation', data).then(res => {
          if (res.code === 0) {
            this.$notify.success({
              title: '提示',
              message: '删除成功',
              duration: 2000
            })
            this.refresh()
          } else {
            this.$notify.error({
              title: '错误',
              message: res.msg,
              duration: 2000
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async getInformation () {
      let params = {
        type: this.region,
        name: this.inputKey,
        pageNum: this.currentPage,
        pageSize: this.size
      }
      let result = await this.$http({
        method: 'post',
        data: params,
        url: '/information/list'
      })

      if (result.code !== 0) {
        return
      }
      this.tableData = result.data.list
      this.total = result.data.total
      this.tableData.forEach(item => {
        item['date'] = this.$base.timestampToTime(item['startTime']) + '至' + this.$base.timestampToTime(item['endTime'])
        item['leixing'] = zxlx[item['type']]
        item['qiyong'] = this.fixState(item['state'])
      })
    }
  }
}
</script>

<style scoped>
/* 引入公共css类 */
@import url(./style);
</style>
