import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {CompanyService} from '../../../services/company.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {CommonsService} from '../../../services/commons.service';
import {FormBuilder, FormGroup} from '@angular/forms';
import {Router} from '@angular/router';
import {ActivateService} from "../../../services/activate.service";

@Component({
  selector: 'app-activate-list',
  templateUrl: './activate-list.component.html',
  styleUrls: ['./activate-list.component.scss']
})
export class ActivateListComponent implements OnInit {

  WEB_SERVE_URL = environment.imageUrl;
  searchForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;

  constructor(
      private form: FormBuilder,
      private company: CompanyService,
      private activate: ActivateService,
      private iconService: IconService,
      private message: NzMessageService,
      private router: Router,
      private common: CommonsService
  ) {
  }

  ngOnInit(): void {
    this.init();
  }

  public init(): void {
    this.searchForm = this.form.group({
      title: [null],
      type: [null],
      status: [null],
    });
    this.getRequest(true, this.searchForm.value);
  }

  // 查询列表
  public getRequest(reset: boolean = false, whereObject: object) {

    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.activate.getList(whereObject, data => {
      if (data['return_code'] === '000000') {
        console.log(data);
        this.requestData = data['return_data'].list;
        this.total = data['return_data'].total;
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

  // 重置
  public resetForm(): void {
    this.searchForm.reset();
  }

  // 活动开始
  public startActivity(id: number): void {
    this.activate.startActivity({activityId : id}, data => {
      if (data['return_code'] === '000000') {
        this.message.success(data['return_msg']);
        this.getRequest(true, this.searchForm.value);
      } else  {
        this.message.error(data['return_msg']);
      }
    });
  }

  // 活动结束
  public endActivity(id: number): void {
    this.activate.endActivity({activityId : id}, data => {
      if (data['return_code'] === '000000') {
        this.message.success(data['return_msg']);
        this.getRequest(true, this.searchForm.value);
      } else  {
        this.message.error(data['return_msg']);
      }
    });
  }


  // 修改
  public getEdit(id: number): void {
    this.router.navigate(['/admin/activate/activate-edit'], {
      queryParams: {
        id: id
      }
    }).then(r => console.log(r));
  }

  // 跳转中奖记录
  public jumpLotteryRecord(id: number): void {
    this.router.navigate(['/admin/activate/activate-lottery-record'], {
      queryParams: {
        activityId: id
      }
    }).then(r => console.log(r));
  }

}

