import {Component, OnInit} from '@angular/core';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {LocalStorageService} from '../../../services/local-storage.service';
import {IndexService} from '../../../services/index.service';
import {environment} from '../../../../environments/environment';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';


@Component({
    selector: 'app-index',
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.less']
})
export class IndexComponent implements OnInit {
    FILE_URL = environment.imageUrl;
    roleType: number;
    loading = true;
    isVisible = false;
    isVisibleAdd = false;
    isVisibleOil = false;
    balance: number;
    price: number;
    code = 23;
    listOfData;
    countData: any = {};
    data: any = {};
    orderData = {};
    balanceData = {
        amounts: 0
    };
    rechargeData = {};
    gasCountData: any = {
        history: {},
        today: {},
    };
    merGasCount: any = {
        amount: 0,
        storeList: []
    };
    recordList = [];
    tyBalance;
    webSocket: WebSocket;
    adminInfoObject: {};
    constructor(
        private store: LocalStorageService, // 数据缓存
        private message: NzMessageService, // 信息提示
        private router: Router,
        private index: IndexService
    ) {
    }

    ngOnInit(): void {
        this.adminInfoObject = this.store.get(ADMIN_INFO_OBJECT);
        this.roleType = this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType;
        if (this.roleType === 1) {
            this.getIndexCount();
            this.getLineCount();
            this.queryCompanyAccountInfo2JD();
        }
        if (this.store.get(ADMIN_INFO_OBJECT)['highAgent'] != null) {
            this.code = this.store.get(ADMIN_INFO_OBJECT)['highAgent'].id;
        }
        if (this.store.get(ADMIN_INFO_OBJECT)['bsCompany'] != null && this.store.get(ADMIN_INFO_OBJECT)['bsCompany'].id === 3) {
            this.code = 60;
            this.getHLTBalance();
            this.orderByIndex();
            this.HLTOrderByIndex();
            this.rechargeOrderByIndex();
            this.HLTOrderByList();
        }

        if (this.store.get(ADMIN_INFO_OBJECT)['merchantStore'] != null) {
            if (this.store.get(ADMIN_INFO_OBJECT)['merchantStore'].type === 1) {
                this.getGasStatistical();
                this.getGasAccount();
            }
        }

        if (this.store.get(ADMIN_INFO_OBJECT)['secUser']['objectType'] === 2) {
            this.getMerGasStatistical();
        }
    }

    // 查询余额
    public getHLTBalance(): void {
        const paramsObject = {
            codeType: 'HLT_BALANCE'
        };
        this.index.getHLTBalance(paramsObject, data => {
            if (data['return_code'] === '000000') {
                this.loading = false;
                this.balance = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public orderByIndex(): void {
        this.index.orderByIndex(this.code, data => {
            if (data['return_code'] === '000000') {
                this.orderData = data['return_data'];
                this.loading = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    // 查询团油余额
    public queryCompanyAccountInfo2JD() {
        this.index.queryCompanyAccountInfo2JD(data => {
            if (data['return_code'] === '000000') {
                console.log(data);
                this.tyBalance = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public HLTOrderByIndex(): void {
        this.index.HLTOrderByIndex(this.code, data => {
            if (data['return_code'] === '000000') {
                this.balanceData = data['return_data'];
                this.loading = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public rechargeOrderByIndex(): void {
        this.index.rechargeOrderByIndex(this.code, data => {
            if (data['return_code'] === '000000') {
                this.rechargeData = data['return_data'];
                this.loading = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public HLTOrderByList(): void {
        this.index.HLTOrderByList(this.code, data => {
            if (data['return_code'] === '000000') {
                this.listOfData = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public getIndexCount(): void {
        this.index.getIndexCount(data => {
            if (data['return_code'] === '000000') {
                this.loading = false;
                this.countData = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public getLineCount(): void {
        this.index.getLineCount(data => {
            console.log(data);
            if (data['return_code'] === '000000') {
                this.data = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    legend: {},
                    xAxis: {
                        type: 'category',
                        data: data['return_data']['date']
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '订单金额',
                            data: data['return_data']['orderPriceSum'],
                            type: 'line'
                        },
                        {
                            name: '话费订单金额',
                            data: data['return_data']['orderRechargePriceSum'],
                            type: 'line'
                        },
                        {
                            name: '订单数量',
                            data: data['return_data']['orderCount'],
                            type: 'line'
                        },
                        {
                            name: '话费订单数量',
                            data: data['return_data']['orderRechargeCount'],
                            type: 'line'
                        },
                        {
                            name: '用户注册数量',
                            data: data['return_data']['userCount'],
                            type: 'line'
                        },
                        {
                            name: '话费订单金额',
                            data: data['return_data']['orderRechargePriceSum'],
                            type: 'line'
                        }
                    ]
                };
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    // 下载模板
    downloadTemplate() {
        this.index.exportHLTOrderByList(this.code, data => {
            if (data['return_code'] === '000000') {
                window.location.href = this.FILE_URL + data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }


    handleOk(): void {
        if (this.price == null) {
            this.message.error('请输入充值金额');
            return;
        }
        const paramsObject = {
            codeType: 'HLT_BALANCE',
            price: this.price
        };
        this.index.addHLTBalance(paramsObject, data => {
            if (data['return_code'] === '000000') {
                this.getHLTBalance();
                this.isVisibleAdd = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    // 查询油站预存余额
    public getGasAccount(): void {
        this.index.getAccount(data => {
            if (data['return_code'] === '000000') {
                this.balanceData = data['return_data'];
                this.loading = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    // 查询加油站统计
    public getGasStatistical(): void {
        this.index.getGasStatistical(data => {
            if (data['return_code'] === '000000') {
                this.gasCountData = data['return_data'];
                this.getGasAccountRecord();
                this.loading = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    // 查询加油站统计
    public getMerGasStatistical(): void {
        this.index.getMerGasStatistical(data => {
            if (data['return_code'] === '000000') {
                this.merGasCount = data['return_data'];
                this.loading = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    // 查询预存账户油站记录
    public getGasAccountRecord(): void {
        const paramsObject = {
            pageNum: 1,
            pageSize: 10000
        };
        this.index.getAccountRecordList(paramsObject, data => {
            if (data['return_code'] === '000000') {
                this.recordList = data['return_data'].list;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    public edit(ids) {
        this.router.navigate(['/admin/index/test'], {
            queryParams: {
                id: ids
            }
        }).then(r => console.log(r));
    }
}

