import {Component, OnInit} from '@angular/core';
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {ActivatedRoute, RouterLink} from "@angular/router";
import {NzModalComponent, NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzMessageService} from "ng-zorro-antd/message";
import {StoreDeviceService} from "../../../services/store-device.service";
import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";
import {SalesmanStatusPipe} from "../../../pipes/salesman-status.pipe";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {NzFormDirective, NzFormLabelComponent, NzFormModule} from "ng-zorro-antd/form";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {NzCellFixedDirective, NzTableComponent, NzTableModule, NzThMeasureDirective} from "ng-zorro-antd/table";
import {AgentStatusPipe} from "../../../pipes/agent-status.pipe";
import {BrowserStorageService} from "../../../utils/localStorage.service";
import {NzSpinModule} from "ng-zorro-antd/spin";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";
import {NzDatePickerModule} from "ng-zorro-antd/date-picker";

@Component({
  selector: 'app-store-device-list',
  standalone: true,
  imports: [
    RouterLink,
    SalesmanStatusPipe,
    DatePipe,
    BreadcrumbComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzSelectComponent,
    NzOptionComponent,
    ReactiveFormsModule,
    NgIf,
    NzInputDirective,
    NzButtonComponent,
    NzIconDirective,
    NzTableComponent,
    NzThMeasureDirective,
    NzCellFixedDirective,
    NgForOf,
    AgentStatusPipe,
    NzModalComponent,
    NzFormLabelComponent,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './store-device-list.component.html',
  styleUrl: './store-device-list.component.less'
})
export class StoreDeviceListComponent implements OnInit {
  dataObject: any = {};
  storeId: number;
  tableLoading = true;
  searchForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};

  addForm: UntypedFormGroup;
  addModal = false;

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

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['storeId'] != null) {
        this.storeId = queryParams['storeId'];
      } else {
        this.storeId = this.store.get(ADMIN_INFO_OBJECT)['store']['id'];
      }
      this.requestData(1);
    });

    this.addForm = this.form.group({
      storeId: [null, [Validators.required]],
      deviceSn: [null, [Validators.required]],
    });
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['storeId'] = this.storeId;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.storeDeviceService.getStoreDeviceList(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();
  }

  /**
   * 打开增加设备模态框
   */
  showAddModal(): void {
    this.addForm.reset( { storeId: this.storeId});
    this.addModal = true;
  }

  /**
   * 关闭增加设备对话框
   */
  closeAddModal(): void {
    this.addModal = false;
  }

  /**
   * 弹出删除对话框
   */
  showDelConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除门店设备？<br/>注意：如果设备正在使用，会导致设备无法使用',
      nzOkText: '是',
      nzCancelText: '否',
      nzOnOk: () => this.delDevice(id)
    });
  }

  /**
   * 提交数据
   */
  submitAddData() {
    for (const i in this.addForm.controls) {
      this.addForm.controls[i].markAsDirty();
      this.addForm.controls[i].updateValueAndValidity();
    }
    if (this.addForm.status == null || this.addForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写必填项信息',
      });
      return;
    }
    this.storeDeviceService.configStoreDevice(this.addForm.value, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
        this.closeAddModal();
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

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