import {Component, OnInit} from '@angular/core';
import {FormsModule, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {NzModalComponent, NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {CommonsService} from "../../../services/commons.service";
import {StoreDiscountActivityService} from "../../../services/store-discount-activity.service";
import {MerService} from "../../../services/mer.service";
import {StoreService} from "../../../services/store.service";
import {ActivatedRoute, Router, RouterLink, RouterOutlet} from "@angular/router";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {
  NzFormControlComponent,
  NzFormDirective,
  NzFormItemComponent,
  NzFormLabelComponent,
  NzFormModule
} from "ng-zorro-antd/form";
import {NzInputDirective, NzInputGroupComponent} from "ng-zorro-antd/input";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {
  NzCellFixedDirective,
  NzTableComponent, NzTableModule,
  NzTdAddOnComponent,
  NzThMeasureDirective,
  NzThSelectionComponent
} from "ng-zorro-antd/table";
import {MerStatusPipe} from "../../../pipes/mer/mer-status.pipe";
import {CompleteStatusPipe} from "../../../pipes/mer/complete-status.pipe";
import {NzDropDownDirective, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzMenuDirective, NzMenuItemComponent, NzSubMenuComponent} from "ng-zorro-antd/menu";
import {NzTabComponent, NzTabSetComponent} from "ng-zorro-antd/tabs";
import {NzUploadComponent} from "ng-zorro-antd/upload";
import {MerTypePipe} from "../../../pipes/mer/mer-type.pipe";
import {NzTooltipDirective} from "ng-zorro-antd/tooltip";
import {NzContentComponent, NzHeaderComponent, NzLayoutComponent, NzSiderComponent} from "ng-zorro-antd/layout";
import {NzDrawerComponent} from "ng-zorro-antd/drawer";
import {NzSwitchComponent} from "ng-zorro-antd/switch";
import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
import {NzBreadCrumbComponent, NzBreadCrumbItemComponent} from "ng-zorro-antd/breadcrumb";
import {NzDatePickerComponent, NzDatePickerModule} from "ng-zorro-antd/date-picker";
import {NzSpinModule} from "ng-zorro-antd/spin";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";

@Component({
  selector: 'app-store-discount-activity-edit',
  standalone: true,
  imports: [
    NzOptionComponent,
    NzFormDirective,
    NzModalComponent,
    NzFormItemComponent,
    NzFormLabelComponent,
    NzFormControlComponent,
    NzSelectComponent,
    NzInputDirective,
    NzColDirective,
    BreadcrumbComponent,
    NzRowDirective,
    ReactiveFormsModule,
    NgForOf,
    NzButtonComponent,
    NzIconDirective,
    NzTableComponent,
    NzThMeasureDirective,
    NzThSelectionComponent,
    NzTdAddOnComponent,
    MerStatusPipe,
    CompleteStatusPipe,
    DatePipe,
    NzDropdownMenuComponent,
    NzMenuDirective,
    RouterLink,
    NgIf,
    NzMenuItemComponent,
    NzTabSetComponent,
    NzTabComponent,
    NzUploadComponent,
    MerTypePipe,
    NzCellFixedDirective,
    NzDropDownDirective,
    NzTooltipDirective,
    NzSubMenuComponent,
    NzSiderComponent,
    NzLayoutComponent,
    NzHeaderComponent,
    NzContentComponent,
    RouterOutlet,
    NzDrawerComponent,
    NzSwitchComponent,
    FormsModule,
    NzInputGroupComponent,
    NzInputNumberComponent,
    NzBreadCrumbComponent,
    NzBreadCrumbItemComponent,
    NzDatePickerComponent,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './store-discount-activity-edit.component.html',
  styleUrl: './store-discount-activity-edit.component.less'
})
export class StoreDiscountActivityEditComponent implements OnInit {

  dataForm: UntypedFormGroup;
  btnLoading = false;

  storeArray = [];
  merArray = [];
  discountActivityPartakeWayArray = [];

  constructor(private form: UntypedFormBuilder,
              private modal: NzModalService,
              private commonsService: CommonsService,
              private storeDiscountActivityService: StoreDiscountActivityService,
              private merService: MerService,
              private storeService: StoreService,
              private activatedRoute: ActivatedRoute,
              private router: Router) { }

  ngOnInit(): void {
    this.dataForm = this.form.group({
      id: [null],
      merId: [null, [Validators.required]],
      storeId: [null],
      partakeWay: ['1', [Validators.required]],
      name: [null, [Validators.required]],
      discountType: [1, [Validators.required]],
      discountCondition: [0, [Validators.required]],
      discountPrice: [0, [Validators.required]],
      finalCostPrice: [0, [Validators.required]],
      restrictType: ['1'],
      restrictPartakeNum: [1],
      endTime: [null, [Validators.required]],
    });

    this.commonsService.getDictionary('DISCOUNT_ACTIVITY_PARTAKE_WAY', data => {
      this.discountActivityPartakeWayArray = data['return_data'];
    });

    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['id'] != null) {
        this.storeDiscountActivityService.getActivityDetail(queryParams['id'], data => {
          data['return_data']['merId'] = String(data['return_data']['merId']);
          data['return_data']['storeId'] = String(data['return_data']['storeId']);
          data['return_data']['partakeWay'] = String(data['return_data']['partakeWay']);
          data['return_data']['discountType'] = String(data['return_data']['discountType']);
          if (data['return_data']['restrictType'] != null) {
            data['return_data']['restrictType'] = String(data['return_data']['restrictType']);
          }
          this.dataForm.patchValue(data['return_data']);
        });
      }
    });

    this.merService.getMerList({merStatus: 1, pageNum: 1,  pageSize: 99999}, data => {
      this.merArray = data['return_data']['list'];
    });
  }

  submitFrom() {
    this.btnLoading = true;
    for (const i in this.dataForm.controls) {
      this.dataForm.controls[i].markAsDirty();
      this.dataForm.controls[i].updateValueAndValidity();
    }
    if (this.dataForm.status == null || this.dataForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写所有的必填项信息',
      });
      return;
    }

    if (this.dataForm.value.partakeWay !== '3') {
      this.dataForm.value.restrictType = null;
      this.dataForm.value.restrictPartakeNum = null;
    }

    this.storeDiscountActivityService.configActivity(this.dataForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '提交成功',
          nzOnOk: () => this.router.navigateByUrl('admin/store-discount-activity/list')
        });
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
      this.btnLoading = false;
    });
  }

  getStore(merId: number) {
    this.storeService.getStoreListByMer({ merId: merId, pageNum: 1, pageSize: 99999}, data => {
      this.storeArray = data['return_data']['list'];
    });
  }
}
