import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {IconService} from '../../../services/icon.service';
import {DistributionService} from '../../../services/distribution.service';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';

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

    WEB_SERVE_URL = environment.imageUrl;
    searchForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    agentData = []; // 代理商列表
    distributionFirst = []; // 一级分销
    distributionSecond = []; // 二级分销人员

    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    // 代理商id
    agentId: number;
    popularizeUserId: number;


    constructor(
        private form: FormBuilder,
        private distribution: DistributionService,
        private iconService: IconService,
        private message: NzMessageService,
    ) {
    }

    ngOnInit(): void {
        this.init();
        this.getInitData();
        this.getDistributionFirst();
        this.getDistributionSecond();
    }

    // 获取初始化数据
    private getInitData(): void {
        // 获取价格数据
        this.distribution.getDistributionAgent( data => {
            this.agentData = data['return_data'];
        });

    }

    public init(): void {
        this.searchForm = this.form.group({
            agentId : [null],
            popularizeUserId: [null],
            orderNo: [null],
            title: [null],
            userName: [null],
        });

        this.getRequest(true, this.searchForm.value);
    }

    // 查询列表
    public getRequest(reset: boolean = false, whereObject: object) {

        this.loading = false;
        if (reset) {
            this.pageNum = 1;
        }
        whereObject['pageNum'] = this.pageNum;
        whereObject['pageSize'] = this.pageSize;
        this.distribution.getDistributionIntegralList(whereObject, data => {
            if (data['return_code'] === '000000') {
                this.requestData = data['return_data'].list;
                this.total = data['return_data'].total;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

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

    // 查询一级分销
    public getDistributionFirst(agentId?: number) {
        this.agentId = agentId;
        const param = {
            agentId : this.agentId
        };
        this.distribution.getDistributionFirst(param , data => {
            this.distributionFirst = data['return_data'];
        });
    }

    // 查询二级分销
    public getDistributionSecond(popularizeUserId?: number) {
        this.popularizeUserId = popularizeUserId;
        const param = {
            agentId : this.agentId,
            popularizeUserId : this.popularizeUserId,
        };
        this.distribution.getDistributionSecond(param , data => {
            this.distributionSecond = data['return_data'];
        });
    }
}
