import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {TyAgentOilStationService} from '../../../../services/ty-agent-oil-station.service';
import {ADMIN_INFO_OBJECT} from "../../../../services/local-storage.namespace";
import {LocalStorageService} from "../../../../services/local-storage.service";
import {environment} from "../../../../../environments/environment";
import {TyAgentService} from "../../../../services/ty-agent.service";
import {TySalesmanService} from "../../../../services/ty-salesman.service";
import {OrganizationService} from "../../../../services/organization.service";

@Component({
  selector: 'app-ty-order-list',
  templateUrl: './ty-order-list.component.html',
  styleUrls: ['./ty-order-list.component.scss']
})
export class TyOrderListComponent implements OnInit {
  FILE_URL = environment.imageUrl;
  roleType;
  adminFlag;

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

  orgIdArray = [];
  tyAgentArray = [];
  tySalesmanArray = [];

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private tyAgentOilStationService: TyAgentOilStationService,
              private tyAgentService: TyAgentService,
              private tySalesmanService: TySalesmanService,
              private organizationService: OrganizationService,
              private form: FormBuilder) {
    this.roleType = Number(this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType);
    this.adminFlag = Number(this.store.get(ADMIN_INFO_OBJECT)['secUser'].adminFlag);
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      orderNo: [null],
      goodsName: [null],
      orgName: [null],
      orgId: [null],
      agentName: [null],
      agentId: [null],
      salesmanName: [null],
      salesmanId: [null],
      memPhone: [null],
      payTimeArray: [null],
      orderStatus: [null],
    });
    this.requestData(1);

    if (this.roleType === 5 && this.adminFlag === 1) {
      this.requestOrgId();
    }

    if (this.roleType === 5 && this.adminFlag === 0) {
      this.requestAgent(this.store.get(ADMIN_INFO_OBJECT)['secUser'].organizationId);
    }

    if (this.roleType === 8) {
      this.requestSalesman(this.store.get(ADMIN_INFO_OBJECT)['highTyAgent'].id);
    }
  }

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

  /**
   * 请求数据
   */
  requestOrgId() {
    this.organizationService.getOrganizationList(this.store.get(ADMIN_INFO_OBJECT)['secUser'].companyId, data => {
      if (data['return_code'] === '000000') {
        this.orgIdArray = data['return_data'];
      } else {
        this.modal.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 请求数据
   */
  requestAgent(orgId: number) {
    if (orgId != null) {
      this.tyAgentService.getAgentList({ orgId: orgId, pageNum: 1, pageSize: 9999}, data => {
        if (data['return_code'] === '000000') {
          this.tyAgentArray = data['return_data']['list'];
        } else {
          this.modal.error(data['return_msg']);
        }
        this.tableLoading = false;
      });
    }
  }

  /**
   * 请求数据
   */
  requestSalesman(tyAgentId: number) {
    if (tyAgentId != null) {
      this.tySalesmanService.getSalesmanList({ tyAgentId: tyAgentId, pageNum: 1, pageSize: 9999}, data => {
        if (data['return_code'] === '000000') {
          this.tySalesmanArray = data['return_data']['list'];
        } else {
          this.modal.error(data['return_msg']);
        }
        this.tableLoading = false;
      });
    }
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    if (whereObject['payTimeArray'] != null) {
      whereObject['payTimeS'] = new Date(whereObject['payTimeArray'][0]).getTime();
      whereObject['payTimeE'] = new Date(whereObject['payTimeArray'][1]).getTime();
    } else {
      whereObject['payTimeS'] = null;
      whereObject['payTimeE'] = null;
    }
    this.whereObject = whereObject;
    this.requestData(1);
  }

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

  /**
   * 导出订单
   */
  excelOrder() {
    this.tyAgentOilStationService.excelOrder(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        window.location.href = this.FILE_URL + 'temporary/' + data['return_data'];
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

}
