import {Component, OnInit} from '@angular/core';
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzFormDirective, NzFormItemComponent, NzFormLabelComponent, NzFormModule} from "ng-zorro-antd/form";
import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {
  NzCellFixedDirective,
  NzTableComponent, NzTableModule,
  NzTdAddOnComponent,
  NzThMeasureDirective,
  NzThSelectionComponent
} from "ng-zorro-antd/table";
import {AgentStatusPipe} from "../../../pipes/agent-status.pipe";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {NzDropDownDirective, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzMenuDirective, NzMenuItemComponent} from "ng-zorro-antd/menu";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {RouterLink} from "@angular/router";
import {NzModalComponent, NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzSpinComponent, NzSpinModule} from "ng-zorro-antd/spin";
import {DeviceTypePipe} from "../../../pipes/device/device-type.pipe";
import {NzUploadComponent} from "ng-zorro-antd/upload";
import {DeviceStatusPipe} from "../../../pipes/device/device-status.pipe";
import {DeviceOrderService} from "../../../services/device-order.service";
import {NzMessageService} from "ng-zorro-antd/message";
import {CommonsService} from "../../../services/commons.service";
import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";
import {environment} from "../../../../environments/environment";
import {DeviceOrderTypePipe} from "../../../pipes/device/device-order-type.pipe";
import {BrowserStorageService} from "../../../utils/localStorage.service";
import {DeviceOrderPayTypePipe} from "../../../pipes/device/device-order-pay-type.pipe";
import {DeviceOrderStatusPipe} from "../../../pipes/device/device-order-status.pipe";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";
import {NzDatePickerModule} from "ng-zorro-antd/date-picker";

@Component({
  selector: 'app-device-order-list',
  standalone: true,
  imports: [
    NzColDirective,
    NzFormItemComponent,
    NzInputNumberComponent,
    NzInputDirective,
    NzButtonComponent,
    BreadcrumbComponent,
    NzFormDirective,
    NzRowDirective,
    ReactiveFormsModule,
    NzOptionComponent,
    NzSelectComponent,
    NzTableComponent,
    NzThMeasureDirective,
    AgentStatusPipe,
    DatePipe,
    NzDropdownMenuComponent,
    NzMenuDirective,
    NzMenuItemComponent,
    NzCellFixedDirective,
    NzIconDirective,
    RouterLink,
    NzDropDownDirective,
    NzModalComponent,
    NzFormLabelComponent,
    NgIf,
    NgForOf,
    NzSpinComponent,
    NzTdAddOnComponent,
    DeviceTypePipe,
    NzUploadComponent,
    DeviceStatusPipe,
    NzThSelectionComponent,
    DeviceOrderTypePipe,
    NzModalModule,
    DeviceOrderPayTypePipe,
    DeviceOrderStatusPipe,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './device-order-list.component.html',
  styleUrl: './device-order-list.component.less'
})
export class DeviceOrderListComponent implements OnInit {
// 角色类型 0：超级管理员  1：公司  2：代理商  3：业务员  4：商户  5：门店
  roleType: number;

  baseUrl = environment.baseUrl;

  loadingObject = {
    spinning: false,
    msg: '加载中'
  };

  dataObject: any = {};
  tableLoading = true;
  searchForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};
  isCollapse = false;
  deviceOrderStatusArray = [];
  deviceOrderTypeArray = [];
  deviceTypeArray = [];
  deviceListModal = false;
  orderAssignList = [];

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private commonsService: CommonsService,
              private deviceOrderService: DeviceOrderService,
              private store: BrowserStorageService,
              private form: UntypedFormBuilder) { }

  ngOnInit(): void {
    this.roleType = this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType;

    this.searchForm = this.form.group({
      companyName: [null],
      agentName: [null],
      salesmanName: [null],
      merName: [null],
      storeName: [null],
      orderNo: [null],
      orderType: [null],
      deviceType: [null],
      status: [null],
    });

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

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

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

    this.requestData(1);
  }

  toggleCollapse(): void {
    this.isCollapse = !this.isCollapse;
  }

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

  /**
   * 展示设备列表模态框
   */
  showDeviceListModal(dataList: []) {
    this.orderAssignList = dataList;
    this.deviceListModal = true;
  }

  /**
   * 关闭设备列表模态框
   */
  closeDeviceListModal() {
    this.orderAssignList = [];
    this.deviceListModal = false;
  }
}
