<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="Ad Title" 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="Ad Location" 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="Type"> <!-- 类型 -->
          <template slot-scope="scope">
            <template v-if="scope.row.TYPE === '1'">
              <el-tag type="success">External Links</el-tag> <!-- 外链 -->
            </template>
            <template v-else-if="scope.row.TYPE === '2'">
              <el-tag>Article</el-tag> <!-- 文章 -->
            </template>
          </template>
        </el-table-column>
        <el-table-column label="Content" prop="CONTENT" width="120">
          <template slot-scope="scope">
            <div v-if="scope.row.TYPE === '1'">
                <el-link type="primary" target="_blank" :href="getHref(scope.row.CONTENT)">{{ scope.row.CONTENT }}</el-link>
            </div><!-- 链接 -->
            <div v-else-if="scope.row.TYPE === '2'">
                <router-link :to="`/article/detail/${scope.row.CONTENT}`" target="_blank" style="cursor: pointer;">{{ getContent(scope.row.CONTENT) }}</router-link>
            </div><!-- 文章 -->
          </template>
        </el-table-column>
        <el-table-column label="Sort" 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="Creator"> <!-- 创建人 -->
          <template slot-scope="scope">
            {{scope.row.CREATE_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="Created Time" width="180"> <!-- 创建时间 -->
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="Modified By"> <!-- 修改人 -->
          <template slot-scope="scope">
            {{scope.row.UPDATE_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="Modified Time" width="180"> <!-- 修改时间 -->
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.UPDATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="Status"> <!-- 状态 -->
          <template slot-scope="scope">
            <div v-if="scope.row.STATUS === '1'">Show</div>
            <div v-else>Hide</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="Action" 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="">
                Action<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`)">Edit</el-dropdown-item>
                <el-dropdown-item command="delete" @click.native="handleDelete(scope.row)" v-if="permission.hasPermission(`ad/ad-delete`)">Delete</el-dropdown-item>
                <el-dropdown-item command="hide" @click.native="handleHide(scope.row)" v-if="permission.hasPermission(`ad/ad-hide`)">Hide</el-dropdown-item>
                <el-dropdown-item command="un-hide" @click.native="handleUnHide(scope.row)" v-if="permission.hasPermission(`ad/ad-un-hide`)">Unhide</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">
            Selected data<!--所选数据--><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()">Delete</el-dropdown-item>
            <el-dropdown-item command="hide" @click.native="handleMultiHide()">Hide</el-dropdown-item>
            <el-dropdown-item command="un-hide" @click.native="handleMultiUnHide()">Unhide</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`)">Add Ad</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'
import {SERVER_API_HTTP_TYPE,SERVER_API_DOMAIN,CDN_BASE_URL} from '@/utils/config'
import article from "../article";

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,
      allArticle: [],
    }
  },
  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)
    },
    handleHide (row) {
      this.hideData(row.ID)
    },
    handleUnHide (row) {
      this.unHideData(row.ID)
    },
    handleMuliDel () {
      this.delData()
    },
    handleMultiHide () {
      this.hideData()
    },
    handleMultiUnHide () {
      this.unHideData()
    },
    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
        obj.allArticle = response.allArticle
      })
    },
    delData (id = null) {
      let obj = this
      obj.$confirm('Are you sure to delete the selected data？', 'Notice', { // 确定删除选定的数据？
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        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 => {

      })
    },
    hideData (id = null) {
      let obj = this
      obj.$confirm('Are you sure to hide the selected data？', 'Notice', { // 确定删除选定的数据？
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        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-hide`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    unHideData (id = null) {
      let obj = this
      obj.$confirm('Are you sure to un-hide the selected data？', 'Notice', { // 确定删除选定的数据？
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        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-un-hide`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
    getImage (imageUrl) {
      return tool.getArImage(imageUrl, '/files/')
    },
    getContent (aid) {
      let titles = this.allArticle.filter(article => article.ID === aid).map(article => article.TITLE)
      return titles.length > 0 ? titles[0] : aid
    },
    getHref (link) {
      return link.indexOf('http') > -1 ? link : 'http://' + link
    }
  }
}

</script>

<style scoped>

</style>
