
<template>
  <div class="activityManage app-container">
    <div class="search">
        <div>
            <el-input
            v-model="query.name"
            size="small"
            clearable
            placeholder="请输入活动名称"
            class="item-width-200 "
            ></el-input>
            <el-select
          v-model="query.activityType"
          size="small"
          class="item-width-200 ml10"
          placeholder="请选择活动类型"
          clearable
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="query.state"
          size="small"
          class="item-width-200 ml10"
          placeholder="请选择活动状态"
          clearable
        >
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
            <el-button
            class="ml10"
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleSearch"
        >
            搜索
        </el-button>
        </div>
        <el-button
            type="primary"
            size="small"
            @click="handleAdd"
        >
            添加
        </el-button>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="活动名称"
        prop="activityName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="活动时间"
        prop="orderNo"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
         {{scope.row.activityStartTime}} 至 {{scope.row.activityEndTime}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="活动类型"
        prop="activityType"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
        {{scope.row.activityType == 1?'海报':'报名'}}
       </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="活动状态"
        prop="status"
        show-overflow-tooltip
      >
      </el-table-column>

       <el-table-column prop="address" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleData(scope.row.id)">数据</el-button>
          <el-button type="text" size="small" v-if="scope.row.status != '已结束'" @click="handleShare(scope.row)">分享</el-button>
          <el-button type="text" size="small" @click="handleDetail(scope.row, scope.row.status != '已结束'?'edit':'detail')">{{scope.row.status != '已结束'?'编辑':'查看'}}</el-button>
          <el-button type="text" size="small" v-if="scope.row.status != '已结束'" @click="handleStop(scope.row.id)">停止</el-button>
          <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

     <el-dialog
    :visible.sync="showCode"
    width="400px"
    center>
    <div v-loading="codeLoading">
      <img style="width:300px;height:300px;margin:0 auto;display:block" :src="codeImg" alt="">
    </div>
    <span slot="footer" class="dialog-footer">
      <a id="downBtn" v-show="false" :download="downName" :href="codeImg" >下载</a>
      <el-button @click="download">下载</el-button>
      <el-button @click="refresh">刷新二维码</el-button>
    </span>
    </el-dialog>

  </div>
</template>

  <script>
  import {debounce} from '@/utils/index'
  import {activityList,activityDel,activityStop} from '@/api/activity'
  import {timeFormat} from '@/utils/index'
  import {BASE_URL} from '@/utils/config'
  import axios from 'axios'
  import {clear} from '@/api/common'
export default {
  name: "activityManage",
  data() {
    return {
      options:[
        {
          value:1,
          label:'海报'
        },
        {
          value:2,
          label:'报名'
        },
      ],
      options1:[
        {
          value:1,
          label:'未开始'
        },
        {
          value:1,
          label:'进行中'
        },
        {
          value:1,
          label:'已结束'
        }
      ],
      tableData: [],
      loading: false,
      codeLoading: false,
      showCode: false,
      codeImg:'',//收款码
      downName:'',//下载名称
      query: {
        "activityType":null,
        "state":'',
        "currentPage": 1,
        "pageSize": 10,
        shopId: 0,
      },
      title: "",
      total: 0,
      id:''
    };
  },
  methods: {
    handleShare(item){
      this.showCode = true
      this.codeLoading = true
      this.downName = item.activityName + "活动码"
      this.id = item.id
      const platformType = localStorage.getItem('platformType')
      axios(
        {
      url: BASE_URL+'zswl-cloud-bdb/promotion/qrcode',
      method: 'post',
      responseType: "arraybuffer",
      data:{
        "page": "signUp/index",
        "scene": item.id,
        "platformType" : platformType
      }
    }).then(res=>{
      console.log(res);
      this.codeLoading = false
       let blob = new Blob([res.data], { type: "image/png" });
       this.codeImg =  (window.URL || window.webkitURL).createObjectURL(blob);
      })
    },
    refresh(){
      this.codeLoading = true
      clear({type:1}).then(res=>{
          if(res.state == 'Success'){
             axios(
              {
              url: BASE_URL+'zswl-cloud-bdb/promotion/qrcode',
              method: 'post',
              responseType: "arraybuffer",
              data:{
                "page": "signUp/index",
                "scene": this.id
              }
            }).then(res=>{
              console.log(res);
              this.codeLoading = false
              let blob = new Blob([res.data], { type: "image/png" });
              this.codeImg =  (window.URL || window.webkitURL).createObjectURL(blob);
            })
          }
      })
    },
    // 下载
    download(){
      document.getElementById('downBtn').click()
    },
    handleAdd(){
        this.$router.push({
            path:'/operationManage/activityManage/addActivity',
            query:{
            mode:'add',
            }
        })
    },
    handleSearch(){
      this.query.currentPage = 1
      this.activityList();
    },
    timeFormat(val){
      return timeFormat(val)
    },
    handleData(id){
        this.$router.push({
          path:'/operationManage/activityManage/dataList',
          query:{
            id
          }
        })
    },
    handleDetail(row,mode){
      this.$router.push({
        path:'/operationManage/activityManage/addActivity',
        query:{
          mode,
          id:row.id,
          status:row.status
        }
      })
    },
    handleDel(id){
         this.$confirm(`此操作将删除该活动, 是否继续?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
        }).then(() => {
          activityDel({id}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
              });
              this.handleSearch()
            }
          })
        })
    },
    handleStop(id){
         this.$confirm(`此操作将停止该活动, 是否继续?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
        }).then(() => {
          activityStop({id}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
              });
              this.handleSearch()
            }
          })
        })
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
      this.activityList()
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.activityList()
    },
    activityList(){
      this.loading = true
      activityList(this.query).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.records
          this.total = res.content.total
        }
      })
    }
  },
  created() {
    this.activityList()
  },
};
</script>

<style lang="scss" >
.activityManage {

}

</style>
