import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {IconService} from '../../../services/icon.service';
import {NzMessageService, NzModalService,  NzUploadChangeParam} from 'ng-zorro-antd';
import {RechargeService} from '../../../services/recharge.service';
import {DiscountService} from '../../../services/discount.service';
import {CouponService} from '../../../services/coupon.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {LocalStorageService} from '../../../services/local-storage.service';
import {CompanyService} from '../../../services/company.service';

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

    FILE_URL = environment.imageUrl;
    WEB_SERVE_URL = environment.baseUrl;
    searchForm: FormGroup; // 搜索框
    validateForm: FormGroup; // 添加框
    requestData = []; // 列表数据
    childData = []; // 列表数据
    importErrorStudentArray = [];
    errorStudentVisible = false;
    isVisibleChild = false;
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    isVisible = false;
    id: number;
    edit = false;
    data = {};
    roleType: number;
    companyData = []; // 列表数据
    constructor(
        private form: FormBuilder,
        private recharge: RechargeService,
        private discount: DiscountService,
        private modal: NzModalService,
        private coupon: CouponService,
        private iconService: IconService,
        private message: NzMessageService,
        private company: CompanyService,
        private store: LocalStorageService, // 数据请求
    ) {
    }

    ngOnInit(): void {
        this.init();
        this.roleType = this.store.get(ADMIN_INFO_OBJECT)['secRole']['roleType'];
        const whereObject = {};
        whereObject['pageNum'] = 1;
        whereObject['pageSize'] = 10000;
        this.company.selectCompanyList(whereObject, data => {
            if (data['return_code'] === '000000') {
                this.companyData = data['return_data'].list;
            } else {
                this.message.error(data['return_msg']);
            }
        });

    }


    public init(): void {
        this.searchForm = this.form.group({
            payStatus: [null], // 支付状态
            rechargeStatus: [null], // 充值状态
            companyId: [null], // 充值状态
            orderNo: [null], // 订单号
            operatorType: [null], // 运营商类型
            laborUnionCard: [null],  // 工会卡号
            rechargeContent: [null], // 充值内容
            userPhone: [null],   // 用户电话
            rechargeType: [null], // 充值类型
            payTime: [null],   // 支付时间
            payType: [null], // 支付方式
            createTime: [null],  // 创建时间
        });
        this.validateForm = this.form.group({
            type: [null, [Validators.required]],
            price: [null, [Validators.required]],
            realPrice: [null, [Validators.required]],
        });

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

    // 查询列表
    public getRequest(reset: boolean = false, whereObject: object) {
        if (whereObject['payTime'] != null && whereObject['payTime'].length !== 0) {
            whereObject['payTimeS'] = whereObject['payTime'][0].getTime();
            whereObject['payTimeE'] = whereObject['payTime'][1].getTime();
        }

        if (whereObject['createTime'] != null && whereObject['createTime'].length !== 0) {
            whereObject['createTimeS'] = whereObject['createTime'][0].getTime();
            whereObject['createTimeE'] = whereObject['createTime'][1].getTime();
        }
        this.loading = false;
        if (reset) {
            this.pageNum = 1;
        }
        whereObject['pageNum'] = this.pageNum;
        whereObject['pageSize'] = this.pageSize;
        this.recharge.getOrderList(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 getDetail(id: number): void {
        this.id = id;
        this.recharge.getOrderById(id, data => {
            if (data['return_code'] === '000000') {
                console.log(data);
                this.data = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });

        this.isVisible = true;
    }


    handleCancel(): void {
        this.isVisible = false;
    }


    // 下载模板
    downloadTemplate(whereObject) {
        if (whereObject['payTime'] != null && whereObject['payTime'].length !== 0) {
            whereObject['payTimeS'] = whereObject['payTime'][0].getTime();
            whereObject['payTimeE'] = whereObject['payTime'][1].getTime();
        }
        if (whereObject['createTime'] != null && whereObject['createTime'].length !== 0) {
            whereObject['createTimeS'] = whereObject['createTime'][0].getTime();
            whereObject['createTimeE'] = whereObject['createTime'][1].getTime();
        }
        this.recharge.exportOrderList(whereObject, data => {
            if (data['return_code'] === '000000') {
                window.location.href = this.FILE_URL + data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    handleChange(info: NzUploadChangeParam): void {
        if (info.file.status === 'done') {
            if (info.file.response.return_code === '000000') {
                this.loading = false;
                if (info.file.response.return_data == null || info.file.response.return_data.errorTotal === 0) {
                    this.message.success('导入成功');
                    this.getRequest(true, this.searchForm.value);
                } else {
                    this.modal.warning({
                        nzTitle: '提示',
                        nzOkText: '查看失败数据',
                        nzContent: '只有部分数据导入成功',
                        nzOnOk: () => this.showErrorStudentModal(info.file.response.return_data)
                    });
                }
            } else {
                this.loading = false;
                this.message.error(info.file.response.return_msg);
            }
        } else if (info.file.status === 'error') {
            this.message.error('上传错误');
        }
    }

    //  打开模态框
    showErrorStudentModal(data: []) {
        this.getRequest(true, this.searchForm.value);
        this.importErrorStudentArray = data['errorData'];
        this.errorStudentVisible = true;
    }

    // 查询子订单
    public getChildOrder(id: number): void {
        this.isVisibleChild = true;
        this.recharge.getChildOrder(id, data => {
            if (data['return_code'] === '000000') {
                this.childData = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

}
