import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {AuditService} from '../../../services/audit.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {CommonsService} from '../../../services/commons.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {LocalStorageService} from '../../../services/local-storage.service';
import {TyAgentService} from '../../../services/ty-agent.service';
import {TySalesmanService} from '../../../services/ty-salesman.service';
import {OrderService} from '../../../services/order.service';

@Component({
    selector: 'app-order-st',
    templateUrl: './order-st.component.html',
    styleUrls: ['./order-st.component.scss']
})
export class OrderStComponent implements OnInit {
    searchForm: FormGroup; // 搜索框
    loading = true;
    requestData = {}; // 列表数据
    roleType: number; // 角色类型
    adminFlag: number; // 是否主角色 1 是 0 不是
    companyData = [];
    agentData = [];
    salesmanData = [];
    priceData: any = {};
    payTypeData: any = {};
    gasOilNoData: any = {};

    constructor(
        private form: FormBuilder,
        private audit: AuditService,
        private iconService: IconService,
        private message: NzMessageService,
        private store: LocalStorageService, // 数据缓存
        private order: OrderService,
        private tyAgentService: TyAgentService,
        private tySalesmanService: TySalesmanService,
        private common: CommonsService,
    ) {
    }

    ngOnInit(): void {
        this.roleType = this.store.get(ADMIN_INFO_OBJECT)['secRole']['roleType'];
        this.adminFlag = this.store.get(ADMIN_INFO_OBJECT)['secUser']['adminFlag'];
        this.searchForm = this.form.group({
            createTime: [null],
            organizationId: [null],
            agentId: [null],
            gasSalesmanId: [null],
            payType: [null],
            gasOilNo: [null],
            gasOilType: [null],
        });
        this.getRequest(true, this.searchForm.value);
        if (this.adminFlag == 1 && this.roleType == 5) {
            const object = {
                companyId: this.store.get(ADMIN_INFO_OBJECT)['bsCompany'].id,
            };
            this.common.getOrganizationList(object, data => {
                if (data['return_code'] === '000000') {
                    this.companyData = data['return_data'];
                } else {
                    this.message.error('查询公司失败');
                }
            });
        } else if (this.adminFlag == 0 && this.roleType == 5) {
            this.getAgentList(this.store.get(ADMIN_INFO_OBJECT)['bsOrganization']['id']);
        } else if (this.roleType == 8) {
            this.getSalesmanList(this.store.get(ADMIN_INFO_OBJECT)['highTyAgent']['id']);
        }

    }

    // 查询列表
    public getRequest(reset: boolean = false, whereObject: object) {
        this.loading = true;
        if (whereObject['createTime'] != null && whereObject['createTime'].length !== 0) {
            whereObject['createTimeS'] = whereObject['createTime'][0].getTime();
            whereObject['createTimeE'] = whereObject['createTime'][1].getTime();
        }

        if (this.roleType == 5 && this.adminFlag == 0) {
            console.log(this.store.get(ADMIN_INFO_OBJECT));
            whereObject['organizationId'] = this.store.get(ADMIN_INFO_OBJECT)['bsOrganization']['id'];
        }
        if (this.roleType == 8) {
            whereObject['agentId'] = this.store.get(ADMIN_INFO_OBJECT)['highTyAgent']['id'];
        }
        if (this.roleType == 9) {
            whereObject['gasSalesmanId'] = this.store.get(ADMIN_INFO_OBJECT)['highTySalesman']['id'];
        }

        this.order.TyOrderStatistics(whereObject, data => {
            this.loading = false;
            if (data['return_code'] === '000000') {
                this.requestData = data['return_data'];
                this.getPieChart(this.requestData);
            } else {
                this.message.error(data['return_msg']);
            }
        });

    }

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

    // 查询代理商
    public getAgentList(e): void {
        if (e != null) {
            const object = {
                orgId: e,
                pageNum: 1,
                pageSize: 10000
            };
            this.tyAgentService.getAgentList(object, data => {
                if (data['return_code'] === '000000') {
                    this.agentData = data['return_data'].list;
                } else {
                    this.message.error('查询代理商失败');
                }
            });
        }
    }

    // 查询业务员
    public getSalesmanList(e): void {
        if (e != null) {
            const object = {
                tyAgentId: e,
                pageNum: 1,
                pageSize: 10000
            };
            this.tySalesmanService.getSalesmanList(object, data => {
                if (data['return_code'] === '000000') {
                    this.salesmanData = data['return_data'].list;
                } else {
                    this.message.error('查询代理商失败');
                }
            });
        }

    }


    // 饼状图
    public getPieChart(data) {
        // 交易金额分布图
        this.priceData = {
            title: {
                text: '交易金额分布图',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            series: [
                {
                    name: '金额分布',
                    type: 'pie',
                    radius: '50%',
                    data: data['priceGorp'],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        // 交易金额分布图
        this.payTypeData = {
            title: {
                text: '交易方式分布图',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            series: [
                {
                    name: '交易方式分布',
                    type: 'pie',
                    radius: '50%',
                    data: data['payTypeGorp'],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        // 交易金额分布图
        this.gasOilNoData = {
            title: {
                    text: '交易类型分布图',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            series: [
                {
                    name: '交易类型分布',
                    type: 'pie',
                    radius: '50%',
                    data: data['gasOilNoGorp'],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
    }

}
