<template>
  <div class="right-list-toolbar right-list-tabs right-list-panel-card">
    <div>
      <!-- 二级导航栏 start-->
      <el-toolbar>
        <el-button @click="addMaterial" priv="Weixin.Manage.Media_Add">
          <i class="fa fa-plus" aria-hidden="true"></i>新建文章
        </el-button>

        <el-button @click="logs" priv="Weixin.Manage.Media_SendLog">
          <i class="fa fa-file-text" aria-hidden="true"></i>微信群发记录
        </el-button>

        <el-col :span="10" class="search-box-r tool-search-box-r">
          <el-input v-model="searchTitle" @keyup.enter.native="getMaterialsList" placeholder="请输入关键字" class="search-bar-algin-toolbar"></el-input>
          <span class="search-icon"><i class="fa fa-search" @click="getMaterialsList" title="查询"></i></span>
        </el-col>

      </el-toolbar>

      <!-- 二级导航栏 end-->
      <div class="mini media-body">
        <!-- 表单栏 -->
        <Waterfall-flow-list :data.sync="materialsList" ref="dataTable" :data-read-url="getData" :page-size="10" :space-y="20" :selectable="true" :multiOperates="multiOperates">

        <template slot="list" slot-scope="{ item }">
          <div class="panel-card table-group-item">
            <div v-for="(val,index) in item.data" :key="val.ID" class="table-card">
              <div class="appmsg-title-ct" v-if="index === 0">
                <div  class="appmsg-title">
                <div class="title" @click.stop="edit(item.ID)">{{val.title}}</div>
                </div>
                <img :src="val.prefix + val.logoFile" @click.stop="edit(item.ID)"  onerror="this.src='assets/images/picture404-'+((row.ID||row.$index) % 4)+'.png';" class="logo-img" :title="val.title" />
              </div>
              <div v-else class="appmsg-div">
                <el-row>
                  <el-col :span="16">
                    <a @click.stop="edit(item.ID)" href="javascript:void(0);" target="_blank">{{val.title}}</a>
                  </el-col>
                  <el-col :span="8">
                    <img :src="val.prefix + val.logoFile" @click.stop="edit(item.ID)" onerror="this.src='assets/images/picture404-'+((row.ID||row.$index) % 4)+'.png';" style="cursor:pointer;" :title="val.title"/>
                  </el-col>
                </el-row>
              </div>
            </div>
            <div class="bottom-div weixin-media-bottom">
              添加时间：{{item.addTime | fixDateTime}}
              <div class="buttons">
                <el-button type="text" @click.stop="edit(item.ID)" title="编辑" priv="Weixin.Manage.Media_Edit">
                  <i class="fa fa-edit f16" aria-hidden="true"></i>
                </el-button>
                <el-button type="text" @click.stop="del(item.ID)" title="删除" priv="Weixin.Manage.Media_Delete">
                  <i class="fa fa-times f16" aria-hidden="true"></i>
                </el-button>
                <el-button type="text" @click.stop="uploadMaterial(item.ID)" title="上传图文素材" priv="Weixin.Manage.Media_Upload">
                  <i class="fa fa-cloud-upload f16" aria-hidden="true"></i>
                </el-button>
                <el-button type="text" @click.stop="send(item.ID)" title="发送" priv="Weixin.Manage.Media_Send">
                  <i class="fa fa-sign-out f16" aria-hidden="true"></i>
                </el-button>
              </div>
            </div>
          </div>
        </template>
      </Waterfall-flow-list>
      </div>
    </div>

    <!-- 微信群发记录 发送日志弹出框 -->
    <massSend-dialog :show.sync="logsVisible"></massSend-dialog>

  </div>
</template>

<script>
import util from '@src/common/util.js'
import massSend from './massSend.vue'
import WaterfallFlowList from '@src/components/waterfallFlow/WaterfallFlowList.vue'

export default {
  components: {
    'massSend-dialog': massSend,
    'Waterfall-flow-list': WaterfallFlowList
  },
  filters: {
    fixDateTime: function(val) {
      return util.formatDate(val, 'yyyy-MM-dd HH:mm')
    }
  },
  data() {
    return {
      logsVisible: false, // 微信群发记录 发送日志弹出框
      // 素材列表
      materialsList: [],
      searchTitle: '',
      multiOperates: [{
        label: '删除',
        value: 'delete',
        fn: function(value) {
          // TODO
        }
      }, {
        label: '导出',
        value: 'export',
        fn: function(value) {
          // TODO 完善批量导出方法
          console.log('export', value)
        }
      }]
    }
  },
  methods: {
    handleCommand(method) {
      if (method && typeof this[method] === 'function') {
        this[method]()
      } else {
        console.warn(`未找到改菜单对应的方法:${method}`)
      }
    },
    edit(ID) {
      this.$router.push(`${this.$route.path}/${ID}`)
    },
    async del(val) {
      try {
        await this.$confirm('确定删除吗？删除后无法恢复。是否继续删除？', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.delete(`/ui/wechat/medias/${[val]}`)
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.getMaterialsList()
        }
      } catch (e) {

      }
    },
    async send(val) {
      let res = await axios.put(`/ui/wechat/medias/${val}/send`, { weiXinID: this.$route.params.weiXinID })
      util.showResponseMessage(res.data)
    },
    addMaterial() {
      this.$router.push(`${this.$route.path}/new`)
    },

    // 微信素材库 上传图文库
    async uploadMaterial(val) {
      let res = await axios.put(`/ui/wechat/medias/${val}/upload`, { weiXinID: this.$route.params.weiXinID })
      util.showResponseMessage(res.data)
    },
    // 微信素材库 微信群发记录
    logs() {
      this.logsVisible = true
    },
    // 加载素材列表数据
    getData() {
      return [
        '/ui/wechat/medias',
        {
          params: {
            weiXinID: this.$route.params.weiXinID,
            title: encodeURIComponent(this.searchTitle)
          }
        }
      ]
    },
    getMaterialsList() {
      this.$refs.dataTable && this.$refs.dataTable.getData()
    }
  }
}
</script>
<style>
.bottom-div.weixin-media-bottom .el-button {
  padding: 3px;
  font-size: 16px;
  padding-top: 2px;
}

.bottom-div.weixin-media-bottom .el-button + .el-button {
  margin-left: 0px;
}

.bottom-div.weixin-media-bottom button > span > i{
  padding: 0px;
}
</style>

<style scoped>
.media-search {
  width: 210px;
  position: absolute;
  right: 10px;
}

.table-group-item {
  margin: 0;
  width: 330px;
  display: inline-block;
  position: relative;
  float: left;
}
.appmsg-title-ct {
  margin-bottom: 13px;
}
.appmsg-title {
  margin-bottom: 6px;
  display: table;
  height: 44px;
}

.appmsg-title .title {
  color: #333;
  height: 44px;
  font-size: 15px;
  line-height: 20px;
  font-weight: bold;
  vertical-align: bottom;
  display: table-cell;
  cursor: pointer;
}

.appmsg-title .title:hover {
  color:#28b06e;
}

.table-group-item .logo-img {
  width: 100%;
  height: 140px;
  border-radius: 3px;
  cursor: pointer;
}

.appmsg-div {
  padding: 14px 0;
  line-height: 20px;
  border-top: 1px solid #eee;
}

.appmsg-div a {
  display: block;
  color: #8d8d8d;
  padding-right: 20px;
}

.appmsg-div a:hover {
  color:#28b06e;
  text-decoration: none;
}

.appmsg-div img {
  float: right;
  width: 95px;
  height: 60px;
  border-radius: 2px;
}

.bottom-div {
  overflow: hidden;
  margin: 0 -15px -14px -14px;
  line-height: 30px;
  font-size: 12px;
  background-color: #fafafa;
  border-top: 1px solid #eee;
  padding: 5px 15px;
  border-radius: 0 0 3px 3px;
}

.bottom-div .buttons {
  float: right;
  text-align: right;
}
</style>
