<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%;"
                @selection-change="handleSelectionChange" @row-click="handleExpand">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <img :src="getImage(scope.row.IMAGE)" width="100%" height="100%" :alt="scope.row.TITLE">
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="广告标题" prop="TITLE" width="250">
          <template slot-scope="scope">
            <el-tag type="" size="small" class="no-border">{{scope.row.TITLE}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="广告位" width="150">
          <template slot-scope="scope">
            <el-tag type="warning" size="small" class="no-border">{{allData.allLocation[scope.row.LID].LOCATION_NAME}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <template v-if="scope.row.TYPE === '1'">
              <el-tag type="success">外链</el-tag>
            </template>
            <template v-else-if="scope.row.TYPE === '2'">
              <el-tag>文章</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="连接或文章ID" prop="CONTENT" width="120"></el-table-column>
        <el-table-column label="排序" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.SORT" min="0" max="99" @change="handleChangeSort(scope.row, scope.row.SORT)"
                      @click.native.stop=""></el-input>
          </template>
        </el-table-column>
        <el-table-column label="创建人">
          <template slot-scope="scope">
            {{scope.row.CREATE_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="修改人">
          <template slot-scope="scope">
            {{scope.row.UPDATE_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="修改时间" width="180">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.UPDATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-dropdown size="small" trigger="click" v-if="permission.hasPermission(`ad/ad-delete`) || permission.hasPermission(`ad/edit`)">
              <el-button type="primary" size="small" @click.stop="">
                操作该数据<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit" @click.native="handleEdit(scope.row)" v-if="permission.hasPermission(`ad/edit`)">编辑</el-dropdown-item>
                <el-dropdown-item command="delete" @click.native="handleDelete(scope.row)" v-if="permission.hasPermission(`ad/ad-delete`)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-dropdown size="small" trigger="click" v-if="permission.hasPermission(`ad/ad-delete`)">
          <el-button type="primary" size="small">
            所选数据<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="delete" @click.native="handleMuliDel()">删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="primary" size="small" @click="handleAdd" icon="el-icon-plus" v-if="permission.hasPermission(`ad/add`)">添加广告</el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import network from '@/utils/network'
import tool from '@/utils/tool'
import store from '@/utils/vuexStore'
import permission from '@/utils/permission'
import Pagination from '@/components/Pagination'

export default {
  name: 'ad_list',
  components: {Pagination},
  mounted () {
    this.getData()
  },
  data () {
    return {
      allData: null,
      tableData: null,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
      permission: permission,
    }
  },
  methods: {
    handleExpand (row, event, column) {
      this.$refs.multipleTable.toggleRowExpansion(row)
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleAdd () {
      this.$router.push({path: `/ad/add`})
    },
    handleEdit (row) {
      this.$router.push({path: `/ad/edit/${row.ID}`})
    },
    handleDelete (row) {
      this.delData(row.ID)
    },
    handleMuliDel () {
      this.delData()
    },
    handleChangeSort (row, sort) {
      network.getData('/ad/sort', {id: row.ID, sort: sort}).then(response => {
        this.getData(this.currentPage, this.pageSize)
      }).catch(response => {
      })
    },
    getData (page, pageSize) {
      let obj = this
      network.getPageData(this, `ad/list/${this.$route.params.lid}`, page, pageSize, null, function (response) {
        obj.allData = response
      })
    },
    delData (id = null) {
      let obj = this
      obj.$confirm('确定删除选定的数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let selectedIds = []
        if (id === null) {
          for (let val of obj.multipleSelection) {
            selectedIds.push(val.ID)
          }
        } else {
          selectedIds.push(id)
        }

        return network.postData(`ad/ad-delete`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    getImage(imageUrl) {
        return tool.getLocaleLink(imageUrl, '/files/');
    },
  }
}

</script>

<style scoped>

</style>
