import {Component, OnInit} from '@angular/core';
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
import {NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzMessageService} from "ng-zorro-antd/message";
import {SalesmanService} from "../../../services/salesman.service";
import {AgentService} from "../../../services/agent.service";
import {CompanyService} from "../../../services/company.service";
import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";
import {RouterLink} from "@angular/router";
import {SalesmanStatusPipe} from "../../../pipes/salesman-status.pipe";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {NzFormDirective, 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 {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-salesman-list',
  standalone: true,
  imports: [
    RouterLink,
    SalesmanStatusPipe,
    DatePipe,
    BreadcrumbComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzSelectComponent,
    NzOptionComponent,
    ReactiveFormsModule,
    NgIf,
    NzInputDirective,
    NzButtonComponent,
    NzIconDirective,
    NzTableComponent,
    NzThMeasureDirective,
    NzCellFixedDirective,
    NgForOf,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './salesman-list.component.html',
  styleUrl: './salesman-list.component.less'
})
export class SalesmanListComponent implements OnInit {
  dataObject: any = {};
  tableLoading = true;
  searchForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};

  userObjectType: number;
  companyArray = [];
  agentArray = [];

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private salesmanService: SalesmanService,
              private agentService: AgentService,
              private companyService: CompanyService,
              private localStorageService: BrowserStorageService,
              private form: UntypedFormBuilder) {
    this.userObjectType = Number(this.localStorageService.get(ADMIN_INFO_OBJECT)['secUser']['objectType']);
  }

  ngOnInit(): void {
    if (this.userObjectType != null && this.userObjectType === 0) {
      this.getCompanyList();
    } else if (this.userObjectType != null && this.userObjectType === 1) {
      this.getAgentList(this.localStorageService.get(ADMIN_INFO_OBJECT)['bsCompany']['id']);
    }
    this.searchForm = this.form.group({
      companyId: [null],
      agentId: [null],
      name: [null],
      phone: [null],
      status: [null],
    });
    this.requestData(1);
  }

  /**
   * 获取公司列表
   */
  getCompanyList() {
    this.companyService.selectCompanyList({pageNum: 1, pageSize: 99 }, data => {
      if (data['return_code'] === '000000') {
        this.companyArray = data['return_data']['list'];
      } else {
        this.modal.error(data['return_msg']);
      }
    });
  }

  /**
   * 获取代理商列表
   */
  getAgentList(companyId: number) {
    this.agentService.queryAgentList({ companyId: companyId, pageNum: 1, pageSize: 99 }, data => {
      if (data['return_code'] === '000000') {
        this.agentArray = data['return_data']['list'];
      } else {
        this.modal.error(data['return_msg']);
      }
    });
  }

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

  /**
   * 弹出删除对话框
   */
  showDeleteConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除该业务员',
      nzOkText: '是',
      nzCancelText: '否',
      nzOnOk: () => this.deleteData(id)
    });
  }

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

  /**
   * 弹出删除对话框
   */
  showDisableSalesmanConfirm(id: number, disable: boolean): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否' + disable ? '禁用' : '启用' + '业务员',
      nzOkText: '是',
      nzCancelText: '否',
      nzOnOk: () => this.disableSalesman(id, disable)
    });
  }

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