import {Component, OnInit} from '@angular/core';
import {FormsModule, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
import {ActivatedRoute, RouterLink, RouterOutlet} from "@angular/router";
import {NzModalComponent, NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzMessageService} from "ng-zorro-antd/message";
import {StoreDiscountActivityService} from "../../../services/store-discount-activity.service";
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 {BrowserStorageService} from "../../../utils/localStorage.service";
import {NzSpinModule} from "ng-zorro-antd/spin";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";

@Component({
  selector: 'app-store-discount-activity-list',
  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-list.component.html',
  styleUrl: './store-discount-activity-list.component.less'
})
export class StoreDiscountActivityListComponent implements OnInit {
  dataObject: any = {};
  storeId: number;
  tableLoading = true;
  searchForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};

  BTN_STORE_DISCOUNT_ACTIVITY_ADD =  false;
  BTN_STORE_DISCOUNT_ACTIVITY_UPDATE =  false;
  BTN_STORE_DISCOUNT_ACTIVITY_DEL =  false;

  constructor(private activatedRoute: ActivatedRoute,
              private store: BrowserStorageService, // 数据请求
              private modal: NzModalService,
              private message: NzMessageService,
              private storeDiscountActivityService: StoreDiscountActivityService,
              private form: UntypedFormBuilder) { }

  ngOnInit(): void {
    this.BTN_STORE_DISCOUNT_ACTIVITY_ADD = this.store.isBtnCompetence('BTN_STORE_DISCOUNT_ACTIVITY_ADD');  // 增加按钮权限
    this.BTN_STORE_DISCOUNT_ACTIVITY_UPDATE = this.store.isBtnCompetence('BTN_STORE_DISCOUNT_ACTIVITY_UPDATE');  // 修改按钮权限
    this.BTN_STORE_DISCOUNT_ACTIVITY_DEL = this.store.isBtnCompetence('BTN_STORE_DISCOUNT_ACTIVITY_DEL');  // 删除按钮权限


    this.searchForm = this.form.group({
      name: [null],
      status: [null],
    });
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['storeId'] = this.storeId;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.storeDiscountActivityService.getActivityList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.modal.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    this.whereObject = whereObject;
    this.requestData(1);
  }

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

  /**
   * 弹出上线对话框
   */
  showUpOnlineConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除优惠活动？<br/>',
      nzOkText: '是',
      nzCancelText: '否',
      nzOnOk: () => this.upOnline(id)
    });
  }

  /**
   * 活动上线数据
   *
   */
  upOnline(id: number) {
    this.storeDiscountActivityService.upOnline(id, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  /**
   * 弹出活动下线对话框
   */
  showLowerOnlineConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除优惠活动？<br/>',
      nzOkText: '是',
      nzCancelText: '否',
      nzOnOk: () => this.lowerOnline(id)
    });
  }

  /**
   * 活动下线数据
   *
   */
  lowerOnline(id: number) {
    this.storeDiscountActivityService.lowerOnline(id, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  /**
   * 弹出删除对话框
   */
  showDelConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除优惠活动？<br/>',
      nzOkText: '是',
      nzCancelText: '否',
      nzOnOk: () => this.delete(id)
    });
  }

  /**
   * 删除数据
   *
   */
  delete(id: number) {
    this.storeDiscountActivityService.delete(id, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }
}
