import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {environment} from '../../../../environments/environment';
import {MerchantStoreService} from '../../../services/merchant-store.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {LocalStorageService} from '../../../services/local-storage.service';

@Component({
    selector: 'app-oli',
    templateUrl: './oli.component.html',
    styleUrls: ['./oli.component.scss']
})
export class OliComponent implements OnInit {
    validateForm!: FormGroup;
    requestData = []; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    id = null;
    name: string;
    WEB_SERVE_URL = environment.baseUrl;
    isVisible = false;
    oilGunVisible = false;
    oilVisible = false;
    gasOilType = [];
    oilGunList = [];
    oilNo;
    gunNo;
    formatterPercent = (value: number) => `¥ ${value == null ? 0 : value}`;
    parserPercent = (value: string) => value.replace('¥ ', '');

    constructor(
        private form: FormBuilder,
        private merchantStore: MerchantStoreService,
        private iconService: IconService,
        private message: NzMessageService,
        private store: LocalStorageService, // 数据请求
        private router: Router,
        private fb: FormBuilder,
        private commonsService: CommonsService,
        private modal: NzModalService,
        private activatedRoute: ActivatedRoute,
        private common: CommonsService
    ) {
    }

    ngOnInit(): void {
        this.init();
        this.validateForm = this.fb.group({
            storeId: [this.store.get(ADMIN_INFO_OBJECT)['merchantStore'].id, [Validators.required]],
            priceGun: [null, [Validators.required]],
            preferentialMargin: [0],

        });
    }

    public init(): void {
        this.commonsService.getDictionary('GAS_OIL_TYPE', data => {
            this.gasOilType = data['return_data'];
        });
        this.getRequest(true, {});
    }


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

        this.loading = false;
        if (reset) {
            this.pageNum = 1;
        }
        whereObject['storeId'] = this.store.get(ADMIN_INFO_OBJECT)['merchantStore'].id;
        this.merchantStore.getOilPriceListByStore(whereObject, data => {
            if (data['return_code'] === '000000') {
                this.requestData = data['return_data'];
                this.total = data['return_data'].length;
            } else {
                this.message.error(data['return_msg']);
            }
        });


    }


    // 是否删除油品
    public getDelete(oilNo): void {
        const message = '是否删除油品';
        this.common.showConfirm(message, data => {
            if (data) {
                this.merchantStore.delOilPrice({
                    storeId: this.store.get(ADMIN_INFO_OBJECT)['merchantStore'].id,
                    oilNo: oilNo
                }, dataReturn => {
                    if (dataReturn['return_code'] === '000000') {
                        this.message.success('操作成功');
                        this.getRequest(false, {});
                    } else {
                        this.message.warning(dataReturn['return_msg']);
                    }
                });
            }
        });
    }

    // 编辑油品
    public editGasOilPrice(): void {
        if (this.oilNo == null) {
            this.message.error('请选择油品！');
            return;
        }
        this.validateForm.value.oilNo = this.oilNo;
        this.merchantStore.editGasOilPrice(this.validateForm.value, data => {
            if (data['return_code'] === '000000') {
                this.isVisible = false;
                this.getRequest(true, {});
                this.message.success('添加成功');
            } else {
                this.message.warning(data['return_msg']);
            }
        });
    }

    // 获取油站油品价格
    public getOilPriceDetail(e): void {
        const whereObject = {
            storeId: this.store.get(ADMIN_INFO_OBJECT)['merchantStore'].id,
            oilNo: e
        };
        this.merchantStore.getOilPriceDetail(whereObject, data => {
            if (data['return_code'] === '000000') {
                this.validateForm.patchValue(data['return_data']);
            } else {
                this.message.warning(data['return_msg']);
            }

        });
    }

    // 配置油品油枪
    public getGunNoListByOilPrice(id): void {
        this.id = id;
        this.merchantStore.getGunNoListByOilPrice(id , data => {
            if (data['return_code'] === '000000') {
                this.oilGunList = data['return_data'];
                this.oilGunVisible = true;
            } else {
                this.message.warning(data['return_msg']);
            }
        });
    }

    // 是否删除油枪列表
    public delGunNo(id): void {
        const message = '是否删除油枪';
        this.common.showConfirm(message, data => {
            if (data) {
                this.merchantStore.delGunNo({
                    gunNoId: id
                }, dataReturn => {
                    if (dataReturn['return_code'] === '000000') {
                        this.message.success('操作成功');
                        this.getGunNoListByOilPrice(this.id);
                    } else {
                        this.message.warning(dataReturn['return_msg']);
                    }
                });
            }
        });
    }

    // 配置油枪号
    public addGasOilPrice(): void {
        if (this.gunNo == null) {
            this.message.error('请输入油枪号');
            return;
        }
        this.merchantStore.addGasOilPrice({
            oilPriceId: this.id,
            gunNo: this.gunNo
        } , data => {
            if (data['return_code'] === '000000') {
                this.message.success('操作成功');
                this.oilVisible = false;
            } else {
                this.message.warning(data['return_msg']);
            }
        });
    }

    public getGasOilPrice(id): void {
        this.id = id;
        this.oilVisible = true;
    }
}
