import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '_@angular_forms@9.0.7@@angular/forms';
import {OrderService} from '../../../services/order.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from '_ng-zorro-antd@9.3.0@ng-zorro-antd';
import {Router} from '_@angular_router@9.0.7@@angular/router';
import {CompanyService} from '../../../services/company.service';
import {MerchantService} from '../../../services/merchant.service';
import {LocalStorageService} from '../../../services/local-storage.service';
import {CommonsService} from '../../../services/commons.service';
import {WebsocketService} from '../../../services/websocket.service';

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

    WEB_SERVE_URL = environment.baseUrl;
    FILE_URL = environment.imageUrl;
    searchForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    companyData = []; // 列表数据
    merchantData = []; // 列表数据
    data = {}; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    id: number; // 订单ID
    isVisible = false;
    isVisibleDetail = false;
    refundContent: string;
    roleType: number;
    constructor(
        private form: FormBuilder,
        private order: OrderService,
        private webSocketService: WebsocketService,
        private iconService: IconService,
        private message: NzMessageService,
        private router: Router,
        private company: CompanyService,
        private merchant: MerchantService,
        private store: LocalStorageService, // 数据请求
        private common: CommonsService
    ) {
    }

    ngOnInit(): void {
        this.init();
        // 接收消息
        this.webSocketService.messageSubject.subscribe(
            data => {
                console.log(data);
                this.getRequest(true, this.searchForm.value);
            },
            err => console.log(err)
        );
    }

    public init(): void {
        this.searchForm = this.form.group({
            orderNo: [null],
            status: [null],
            createTime: [null],
        });
        this.getRequest(true, this.searchForm.value);
    }

    // 查询列表
    public getRequest(reset: boolean = false, whereObject: object) {
        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.order.getGasOrderListByOil(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();
    }

    showModal(id): void {
        this.id = id;
        this.isVisible = true;
    }

    handleOk(): void {
        this.common.showConfirm('确认订单是否退款', item => {
            if (item) {
                const params = {
                    orderId: this.id ,
                    refundContent: this.refundContent
                };
                this.order.refuelingOrderRefund(params, data => {
                    if (data['return_code'] === '000000') {
                        this.getRequest(true, this.searchForm.value);
                        this.isVisible = false;
                        this.message.success('退款成功');
                    } else {
                        this.message.error(data['return_msg']);
                    }
                });
            }
        });
    }

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

    getDetail(orderNo: string): void {
        this.order.getGasOrderDetail(orderNo , data => {
            if (data['return_code'] === '000000') {
                this.isVisibleDetail = true;
                this.data = data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }

    /**
     * 导出订单
     */
    excelOrder() {
        this.order.exportGasOrder(this.searchForm.value, data => {
            if (data['return_code'] === '000000') {
                window.location.href = this.FILE_URL + 'temporary/' + data['return_data'];
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }
}
