<template>
  <div class="app-container calendar-list-container" v-loading.body="showLoadingBody">
    <div>
      <el-table :key='tableKey' :data="list" border fit highlight-current-row
                style="width: 100%;">
        <el-table-column type="index" align="center" label="活动ID" width="200">
          <template scope="scope">
            <span>{{scope.row.id}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="活动名称">
          <template scope="scope">
            <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="活动时间">
          <template scope="scope">
              <div v-if="scope.row.title">永久</div>
              <div v-else><span>{{scope.row.startTimeStr}}</span> ~ <span>{{scope.row.endTimeStr}}</span></div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="人数限制">
          <template scope="scope">
            <span>{{scope.row.numLimit}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="已参加人数">
          <template scope="scope">
            <span>{{scope.row.hasJoinNum}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template scope="scope">
            <span v-if="scope.row.status==1">上架</span>
            <span v-if="scope.row.status==2">下架</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
            <template scope="scope">
                <el-button size="small" type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button size="small" type="success" @click="handleActivityData(scope.row)" style="margin-left:0px">活动数据</el-button>
            </template>
        </el-table-column>
      </el-table>
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page.sync="listQuery.page" :page-sizes="[10,20,30,40,50]" :page-size="listQuery.limit"
                       layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </div>
    </div>
    <el-dialog title="编辑" :visible.sync="newPeopelVisible">
		  <el-form :model="formdata" :rules="rules" ref="form" >
        <div style="font-weight: bold;">基础设置</div>
			  <el-row>
          <el-col :span="5">
            <el-form-item label="活动状态" label-width="70px">
              <el-radio-group v-model="formdata.status_name" size="medium">
                <el-radio-button label="上架"></el-radio-button>
                <el-radio-button label="下架"></el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="参与人数" label-width="70px" prop="numLimit">
              <el-input v-model="formdata.numLimit" placeholder="请输入参与人数"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="活动开始/结束时间"  label-width="130px" style="padding-left:8px">
              <el-date-picker v-model="formdata.starTimeStr" type = "datetime" placeholder = "开始时间" value-format=" yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsStart"></el-date-picker>
              <span> - </span>
              <el-date-picker v-model="formdata.endTimeStr" type = "datetime" placeholder = "结束时间" value-format=" yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsEnd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
		  </el-form>
      <div slot="footer"  class="dialog-footer">
        <el-button type="primary" @click="add('form')">确认</el-button>
      </div>
	  </el-dialog>

    <!-- 新人活动 -->
    <el-dialog title="活动数据" :visible.sync="newPeopelDataVisible">
		  <el-form :model="newPeopelDataQuery" >
			  <el-row>
          <el-date-picker v-model="newPeopelDataQuery.starTimeStr" type = "datetime" placeholder = "开始时间" value-format=" yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsStart"></el-date-picker>
          <span> - </span>
          <el-date-picker v-model="newPeopelDataQuery.endTimeStr" type = "datetime" placeholder = "结束时间" value-format=" yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsEnd"></el-date-picker>
        </el-row>
		  </el-form>
      <el-table :key='tableKey' :data="list" border fit highlight-current-row
                style="width: 100%;">
          <el-table-column type="index" align="center" label="数据描述" width="200">
            <template scope="scope">
              <span>{{scope.row.id}}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="pv(总)">
            <template scope="scope">
              <span>{{scope.row.name}}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="pv(日均)">
            <template scope="scope">
                <div v-if="scope.row.title">永久</div>
                <div v-else><span>{{scope.row.startTimeStr}}</span> ~ <span>{{scope.row.endTimeStr}}</span></div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="uv(总)">
            <template scope="scope">
              <span>{{scope.row.numLimit}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="uv(日均)">
            <template scope="scope">
              <span>{{scope.row.hasJoinNum}}</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination @size-change="newPeopelDataSizeChange" @current-change="newPeopelDataCurrentChange"
                        :current-page.sync="newPeopelDataQuery.page" :page-sizes="[10,20,30,40,50]" :page-size="newPeopelDataQuery.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
        </div>
	  </el-dialog>


    <!-- 邀请有礼活动 -->
    <el-dialog title="活动数据" :visible.sync="shareDataVisible">
		  <el-form :model="shareDataQuery">
			  <el-row>
          <el-date-picker v-model="shareDataQuery.starTimeStr" type = "datetime" placeholder = "开始时间" value-format=" yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsStart"></el-date-picker>
          <span> - </span>
          <el-date-picker v-model="shareDataQuery.endTimeStr" type = "datetime" placeholder = "结束时间" value-format=" yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsEnd"></el-date-picker>
        </el-row>
		  </el-form>
      <el-table :key='tableKey' :data="list" border fit highlight-current-row
                style="width: 100%;">
          <el-table-column type="index" align="center" label="数据描述" width="200">
            <template scope="scope">
              <span>{{scope.row.id}}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="pv(总)">
            <template scope="scope">
              <span>{{scope.row.name}}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="pv(日均)">
            <template scope="scope">
                <div v-if="scope.row.title">永久</div>
                <div v-else><span>{{scope.row.startTimeStr}}</span> ~ <span>{{scope.row.endTimeStr}}</span></div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="uv(总)">
            <template scope="scope">
              <span>{{scope.row.numLimit}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="uv(日均)">
            <template scope="scope">
              <span>{{scope.row.hasJoinNum}}</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination @size-change="shareDataSizeChange" @current-change="shareDataCurrentChange"
                        :current-page.sync="shareDataQuery.page" :page-sizes="[10,20,30,40,50]" :page-size="shareDataQuery.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
        </div>
	  </el-dialog>
  </div>
</template>

<script>
  import {
    formatDate
  } from 'utils/dateFormattor';

  import {
    toEast8Date,
    deepCopyDate,
    newEast8Date,
    convertDate2Str,
    timestamp2Date,
    getYMD_hm
  } from 'utils/dateUtils';

  import rsCode from '../../utils/rsCode';

  import {mapGetters} from 'vuex';
  import {
    getToken
  } from 'utils/auth';
  import {
    activityList,
    getObj
  } from 'api/activityManagement';
  import Element1 from "../admin/menu/components/element";
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";

  export default {
    name: 'conponList',
    components: {
      ElCol,
      ElRow,
      Element1,
    },
    data() {
      return {
        rules:{
            numLimit: {
                type: 'string',
                required: true,
                message: '请输入参与人数',
                trigger: 'blur'
            }
        },
        newPeopelVisible:false,
        newPeopelDataVisible:false,
        shareDataVisible:false,
        BASE_API: process.env.BASE_API,
        showLoadingBody: false,
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
        },
        newPeopelDataQuery:{
          page: 1,
          limit: 20,
          starTimeStr:'',
          endTimeStr:''
        },
        shareDataQuery:{
          page: 1,
          limit: 20,
          starTimeStr:'',
          endTimeStr:''
        },
        inline: true,
        tableKey: 0,
        formdata:{},
        pickerOptionsStart: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
          }
        }, 
        pickerOptionsEnd: {
            disabledDate: time => {
                const beginDateVal = new Date(this.formdata.starTimeStr).getTime()
                if (beginDateVal) {
                    return time.getTime() < beginDateVal - 0
                }
            }
        },
      }
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      /**
       * 获取活动列表
       * */
      getList() {
        this.listLoading = true;
        activityList(this.listQuery).then(response => {
          let totalCountRs = undefined;
          let listRs = undefined;
          // if (!this.$utils.isEmpty(response.data) && this.$utils.isInteger(response.data.totalCount)) {
          if (!this.$utils.isEmpty(response.data)) {
            listRs = response.data;
            listRs.map(function(item){
              item.startTimeeStr = timestamp2Date(item.startTime);
              item.endTimeStr = timestamp2Date(item.endTime);
            });
          }
          this.listLoading = false;
          this.list = listRs;
          this.total = totalCountRs;
        })

      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      newPeopelDataSizeChange(val) {
        this.newPeopelDataQuery.limit = val;
      },
      newPeopelDataCurrentChange(val) {
        this.newPeopelDataQuery.page = val;
      },
      shareDataSizeChange(val) {
        this.shareDataQuery.limit = val;
      },
      shareDataCurrentChange(val) {
        this.shareDataQuery.page = val;
      },
      handleUpdate(item){
          var that = this;
          getObj(row.id).then(response => {
            var item = response.data;
            item.status_name = item.status==1?"上架":item.type==2?"下架":'';
            if(item.startTime>0){
              item.starTimeStr = getYMD_hm(item.startTime);
            }else{
              item.starTimeStr = '';
            }
            if(item.endTime>0){
              item.endTimeStr = getYMD_hm(item.endTime);
            }else{
              item.endTimeStr = '';
            }
            this.formdata = item;
            console.log(this.form)
            that.newPeopelVisible = true;
          })
          
      },
      handleActivityData(row){
        // 1--新人尊享; 2--推广活动表
        if(row.type==1){
          this.newPeopelDataVisible = true;
        }
        if(row.type==2){
          this.shareDataVisible = true;
        }
      }
    }
  }
</script>
