import {Component, OnInit} from '@angular/core';
import {environment} from '../../../../environments/environment';
import {CompanyService} from '../../../services/company.service';
import {IconService} from '../../../services/icon.service';
import {CommonsService} from '../../../services/commons.service';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';

@Component({
    selector: 'app-oils-discount',
    templateUrl: './oils-discount.component.html',
    styleUrls: ['./oils-discount.component.scss']
})
export class OilsDiscountComponent implements OnInit {

    WEB_SERVE_URL = environment.imageUrl;
    searchForm: FormGroup; // 搜索框
    getForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    isVisible = false;
    roleTypeArray;

    constructor(
        private form: FormBuilder,
        private company: CompanyService,
        private iconService: IconService,
        private message: NzMessageService,
        private router: Router,
        private common: CommonsService
    ) {
    }

    ngOnInit(): void {
        this.common.getDictionary('GAS_OIL_TYPE', data => {
            this.roleTypeArray = data['return_data'];
        });
        this.init();
    }

    public init(): void {
        this.searchForm = this.form.group({
            oilNo: [null],
        });
        this.getForm = this.form.group({
            oilNo: [null],
            priceRate: [null],
        });
        this.getRequest(true, this.searchForm.value);
    }

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

        this.loading = false;
        if (reset) {
            this.pageNum = 1;
        }
        whereObject['pageNum'] = this.pageNum;
        whereObject['pageSize'] = this.pageSize;
        this.common.getList(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 getForbiddenUser(id): void {
        this.common.showConfirm('是否删除', data => {
            if (data) {
                this.common.delete(id, dataUser => {
                    this.getRequest(false, this.searchForm.value);
                });
            }
        });
    }


    // 修改
    public getAdd(): void {
        this.isVisible = true;
    }

    getEdit(id: number): void {
        this.isVisible = true;
        this.common.getDetailById(id , data => {
           this.getForm.patchValue(data['return_data']);
           this.getForm.value.id = id;
        });

    }

    handleOk(): void {
        console.log(this.getForm.value);
        this.common.editGasDiscountOilPrice(this.getForm.value, data => {
            if (data['return_code'] === '000000') {
                this.getRequest(false, this.searchForm.value);
            } else {
                this.message.error(data['return_msg']);
            }
        });
        this.isVisible = false;
    }

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

}

