<!--  -->
<template>
  <div>
    <main-top :crumb="crumbData"></main-top>
    <div class="table-interface">
      <div class="top-body" v-loading="loading"
    element-loading-text=""
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.6)">
        <div class="info-top">
          <div class="info-left">
            <span >展示方案:</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-display',params:{type:0,id:'none'}})">新增方案</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">
            <span>{{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">
              <!-- <a href="javascript:;" @click="up(scope.row, scope.$index)">
                <img class="icon" src="@/assets/img/up-arrow.png" alt="">
              </a>
              <a href="javascript:;"  @click="down(scope.row, scope.$index)">
                <img class="icon" src="@/assets/img/down-arrow.png" alt="">
              </a> -->
              <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'
const crumbData = [
  {
    name: '展示管理',
    id: 3
  }
]

export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    MainTop
  },
  data () {
    // 这里存放数据
    let data = [
      // {
      //   prop: 'idx',
      //   label: '序号'
      // },
      {
        prop: 'name',
        label: '展示方案'
      },
      {
        prop: 'typeName',
        label: '展示类别'
      },
      {
        prop: 'orderNum',
        label: '排序'
      },
      {
        prop: 'date',
        label: '发布时间'
      }
    ]

    return {
      crumbData,
      data,
      loading: true,
      tableData: [],
      inputKey: '',
      currentPage: 1,
      size: 10,
      total: 0
    }
  },
  watch: {
    currentPage () {
      this.refresh()
    },
    size () {
      this.refresh()
    },
    inputKey () {
      this.refresh()
    }
  },
  mounted () {
    this.refresh()
  },
  methods: {
    goto (item) {
      this.$router.push({ name: 'edit-display', params: { type: 1, id: item.id } })
    },
    async changeState (item) {
      this.loading = true

      let { name, id, typeId, equipmentId, description } = item
      let data = { name, typeId, equipmentId, isSend: 1, state: !item.qiyong.st ? 1 : 0, description, id }
      this.$http.post('/exhibition/updateExhibition', data).then(res => {
        if (res.code === 0) {
          this.loading = false
          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
    },
    del (item) {
      let data = {
        id: item.id
      }
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/exhibition/deleteExhibition', 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: '已取消删除'
        })
      })
    },
    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
    },
    async getInformation () {
      let params = {
        name: this.inputKey,
        pageNum: this.currentPage,
        pageSize: this.size
      }
      let result = await this.$http({
        method: 'post',
        data: params,
        url: '/exhibition/list'
      })

      if (result.code !== 0) {
        return
      }
      this.tableData = result.data.list
      this.total = result.data.total
      this.tableData.forEach((item, i) => {
        item['date'] = this.$base.timestampToTime(item['createTime'])
        item['qiyong'] = this.fixState(item['state'])
        item['idx'] = i + 1
      })
    }
  }
}
</script>

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