<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">
                        <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>
  </div>
</template>

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

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

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

  import {mapGetters} from 'vuex';
  import {
    getToken
  } from 'utils/auth';
  import {
    activityList
  } 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:{
            phone: {
                type: 'string',
                required: true,
                message: '请输入手机号',
                trigger: 'blur'
            },
            num: {
                type: 'string',
                required: true,
                message: '请输入添加张数',
                trigger: 'blur'
            }
        },
        newPeopelVisible:false,
        BASE_API: process.env.BASE_API,
        showLoadingBody: false,
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
        },
        inline: true,
        tableKey: 0,
        formdata:{
            phone:'',
            num:'',
            type:''
        },
        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);
              item.visible1 = false;
              item.visible2 = false;
            });
          }
          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();
      },
      giveCunpon(){
          this.giveDialogVisible =true;
      },
      handleUpdate(item){
          var that = this;
          that.formdata = item;
          that.newPeopelVisible = true;
      }
    }
  }
</script>
