import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {TySalesmanService} from '../../../../services/ty-salesman.service';
import {ADMIN_INFO_OBJECT} from "../../../../services/local-storage.namespace";
import {LocalStorageService} from "../../../../services/local-storage.service";
import {ActivatedRoute} from "@angular/router";

@Component({
  selector: 'app-ty-salesman-list',
  templateUrl: './ty-salesman-list.component.html',
  styleUrls: ['./ty-salesman-list.component.scss']
})
export class TySalesmanListComponent implements OnInit {

  dataObject: any = {};
  tableLoading = true;
  searchForm: FormGroup;
  pageNum: number;
  whereObject: any = {};

  tyAgentId: number;

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private tySalesmanService: TySalesmanService,
              private store: LocalStorageService, // 数据请求
              private activatedRoute: ActivatedRoute,
              private form: FormBuilder) {
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      salesmanName: [null],
      salesmanPhone: [null],
    });

    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['tyAgentId'] != null) {
        this.tyAgentId = queryParams['tyAgentId'];
      }
    });

    if (this.store.get(ADMIN_INFO_OBJECT)['highTyAgent']['id'] != null) {
      this.tyAgentId = this.store.get(ADMIN_INFO_OBJECT)['highTyAgent']['id'];
    }

    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['tyAgentId'] = this.tyAgentId;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.tySalesmanService.getSalesmanList(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();
  }
  /**
   * 弹出密码重置对话框
   */
  showPwdResetConfirm(key: string): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否重置业务员密码！',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'danger',
      nzOnOk: () => this.pwdReset(key)
    });
  }

  /**
   * 密码重置
   *
   */
  pwdReset(key: string) {
    this.tySalesmanService.salesmanPwdReset({ salesmanKey : key}, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '重置成功！密码为：123456'
        });
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

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

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