import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '_@angular_forms@9.0.7@@angular/forms';
import {CompanyService} from '../../../services/company.service';
import {ActivateService} from '../../../services/activate.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from '_ng-zorro-antd@9.3.0@ng-zorro-antd';
import {Router} from '_@angular_router@9.0.7@@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {DiscountPackageService} from '../../../services/discount-package.service';

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

  WEB_SERVE_URL = environment.imageUrl;
  searchForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  usingAttribution = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  listOfData = [];
  isVisible = false;
  isVisibleInventory = false;
  id: number;
  num: number;
  constructor(
      private form: FormBuilder,
      private company: CompanyService,
      private activate: ActivateService,
      private iconService: IconService,
      private message: NzMessageService,
      private router: Router,
      private discountPackage: DiscountPackageService,
      private commonsService: CommonsService,
  ) {
  }

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

  public init(): void {
    this.searchForm = this.form.group({
      title: [null],
      salesType: [null],
      usingAttribution: [null],
      recordNo: [null],
      status: [null],
      phone: [null],
    });
    this.getRequest(true, this.searchForm.value);
    this.commonsService.getDictionary('USING_ATTRIBUTION  ', data => {
      this.usingAttribution = data['return_data'];
    });
  }

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

    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.discountPackage.getDiscountPackageList(whereObject, data => {
      if (data['return_code'] === '000000') {
        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 upDiscountPackage(id: number): void {
    this.discountPackage.upDiscountPackage({discountPackageId : id}, data => {
      if (data['return_code'] === '000000') {
        this.message.success('上架成功');
        this.getRequest(true, this.searchForm.value);
      } else  {
        this.message.error(data['return_msg']);
      }
    });
  }

  // 活动结束
  public downDiscountPackage(id: number): void {
    this.discountPackage.downDiscountPackage({discountPackageId : id}, data => {
      if (data['return_code'] === '000000') {
        this.message.success('下架成功');
        this.getRequest(true, this.searchForm.value);
      } else  {
        this.message.error(data['return_msg']);
      }
    });
  }

  // 删除
  public deleteDiscountPackage(id: number): void {
    this.commonsService.showConfirm('是否确认删除', isDelete => {
      if (isDelete) {
        this.discountPackage.deleteDiscountPackage({discountPackageId : id}, data => {
          if (data['return_code'] === '000000') {
            this.message.success('删除成功');
            this.getRequest(true, this.searchForm.value);
          } else  {
            this.message.error(data['return_msg']);
          }
        });
      }
    });
  }


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

  handleCancel(): void {
    this.isVisible = false;
  }

  public getDetails(id: number): void {
    this.discountPackage.getDiscountPackageRuleById(id , data => {
      if (data['return_code'] === '000000') {
        this.listOfData = data['return_data'];
        this.isVisible = true;
      } else {
        this.message.create('error', data['return_msg']);
      }
    });
  }

  // 增加库存
  public addInventory(id: number): void {
    this.id = id;
    this.isVisibleInventory = true;
  }


  handleCancelInventory(): void {
    this.isVisibleInventory = false;
  }

  handleOkdInventory(): void {

    if (this.num == null || this.num === 0) {
      this.message.error('请输入新增库存数量');
      return;
    }


    const param = {
      num: this.num,
      discountPackageId: this.id
    };
    const id = this.message.loading('增加库存中..', { nzDuration: 0 }).messageId;
    this.discountPackage.addDiscountPackageStock(param , data => {
      if (data['return_code'] === '000000') {
        this.isVisibleInventory = false;
        this.getRequest(true, this.searchForm.value);
        this.message.success('新增成功');
      } else {
        this.message.create('error', data['return_msg']);
      }
      this.message.remove(id);
    });
  }

}

