
<template>
  <div class="luckyDrawManage app-container">
    <div class="search">
        <div>
            <el-input
            v-model="query.raffleName"
            size="small"
            clearable
            placeholder="请输入活动名称"
            class="item-width-200 "
            ></el-input>
            <el-select
          v-model="query.status"
          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="活动ID"
        prop="id"
        show-overflow-tooltip
      >
      </el-table-column>
       <el-table-column
        align="center"
        label="排序"
        prop="sort"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="活动名称"
        prop="raffleName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="活动时间"
        prop="orderNo"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
        <div v-if="scope.row.raffleCycle == 1">
          {{scope.row.startTime}} 至 {{scope.row.endTime}}
        </div>
        <div v-else>
          {{scope.row.raffleCycleContent | filterDate}}
        </div>
        
        </template>
      </el-table-column>
     
      <el-table-column
        align="center"
        label="活动状态"
        prop="msg"
        show-overflow-tooltip
      >
      </el-table-column>
       <el-table-column
        align="center"
        label="展示状态"
        prop="activityType"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
        <el-switch
          v-model="scope.row.raffleState"
          :active-value="1"
          :inactive-value="2"
          disabled>
        </el-switch>
       </template>
      </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="handleEdit(scope.row,'edit')">编辑</el-button>
          <el-button type="text" size="small" @click="handleStop(scope.row.id,scope.row.raffleStatus)">{{scope.row.raffleStatus==1?'停止':'开启'}}</el-button>
          <el-button type="text" size="small" @click="handleRule(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 {getLuckyDrawList,activityDel,luckyDrawEnable} from '@/api/luckyDraw.js'
  import {timeFormat} from '@/utils/index'
  import {BASE_URL} from '@/utils/config'
  import axios from 'axios'
  import {clear} from '@/api/common'
export default {
  name: "luckyDrawManage",
  data() {
    return {
      options:[
        {
          value:1,
          label:'未开始'
        },
        {
          value:2,
          label:'进行中'
        },
        {
          value:3,
          label:'已结束'
        }
      ],
      options1:[
        {
          value:1,
          label:'展示'
        },
        {
          value:2,
          label:'不展示'
        },
      ],
      tableData: [],
      loading: false,
      codeLoading: false,
      showCode: false,
      codeImg:'',//收款码
      downName:'',//下载名称
      query: {
        raffleName:'',
        "status":'',
        "state":'',
        "currentPage": 1,
        "pageSize": 10,
      },
      title: "",
      total: 0,
      id:''
    };
  },
  filters: {
    filterDate: function(value) {
      let arr = ['','周一','周二','周三','周四','周五','周六','周日']
      let str = ''
      let list = value.split(',')
      list.map(item=>{
        str = str + arr[item] +'、'
      })
      return str;
    }
  },
  methods: {
    handleRule(id){
      this.$router.push({
            path:'/operationManage/luckyDrawManage/luckyDrawRule',
            query:{
              id
            }
        })
    },
    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/luckyDrawManage/addLuckyDraw',
            query:{
            mode:'add',
            }
        })
    },
    handleSearch(){
      this.query.currentPage = 1
      this.getLuckyDrawList();
    },
    timeFormat(val){
      return timeFormat(val)
    },
    handleData(id){
        this.$router.push({
          path:'/operationManage/luckyDrawManage/dataList',
          query:{
            id
          }
        })
    },
    handleEdit(row,mode){
      if(row.raffleStatus == 1){
        return this.$message({
          message: '进行中的活动不能编辑',
          type: 'error'
        });
      }

      this.$router.push({
        path:'/operationManage/luckyDrawManage/addLuckyDraw',
        query:{
          mode,
          id:row.id,
        }
      })
    },

    handleStop(raffleId,status){
      let msg = ''
      if(status == 1){
        msg = '关闭'
      }else{
        msg = '开启'
      }
         this.$confirm(`此操作将${msg}该活动, 是否继续?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
        }).then(() => {
          luckyDrawEnable({raffleId,status:status == 1?2:1}).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.getLuckyDrawList()
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getLuckyDrawList()
    },
    getLuckyDrawList(){
      this.loading = true
      getLuckyDrawList(this.query).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.records
          this.total = res.content.total
        }
      })
    }
  },
  created() {
    this.getLuckyDrawList()
  },
};
</script>

<style lang="scss" >
.luckyDrawManage {

}

</style>
