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, Validators} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {GasChannelConfigService} from '../../../services/gas-channel-config.service';

@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;

    gasChannelConfigChannelArray = [];
    configOilMerNumModal = false;
    configOilMerNumForm: FormGroup;

    channelId = '1';
    tripartitePlatformModal = false;
    tripartitePlatformForm: FormGroup;

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

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

        this.common.getDictionary('GAS_CHANNEL_CONFIG_CHANNEL', data => {
            this.gasChannelConfigChannelArray = 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.configOilMerNumForm = this.form.group({
            codeType: ['OIL_WX_MER'],
            codeValue: [null, [Validators.required]],
        });

        this.tripartitePlatformForm = this.form.group({
            payPlatformType: ['1', [Validators.required]],
            payPlatformMerName: [null, [Validators.required]],
            payPlatformMerNo: [null, [Validators.required]],
            profitSharingStatus: ['false', [Validators.required]],
            profitSharingRatio: [null],
            profitSharingReceiversNo: [null],
            profitSharingReceiversName: [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;
    }

    showConfigOilMerNum() {
        this.common.mappingSysNameOl('OIL_WX_MER', data => {
            this.configOilMerNumForm.patchValue(data['return_data']);
        });
        this.configOilMerNumModal = true;
    }

    closeConfigOilMerNum() {
        this.configOilMerNumModal = false;
    }

    submitConfigOilMerNum() {
        for (const i in this.configOilMerNumForm.controls) {
            this.configOilMerNumForm.controls[i].markAsDirty();
            this.configOilMerNumForm.controls[i].updateValueAndValidity();
        }
        if (this.configOilMerNumForm.status == null || this.configOilMerNumForm.status !== 'VALID') {
            this.modal.warning({
                nzTitle: '提示',
                nzContent: '请填写所有必填项',
            });
            return;
        }

        this.common.editConfig(this.configOilMerNumForm.value, data => {
            if (data['return_code'] === '000000') {
                this.modal.success({
                    nzTitle: '提示',
                    nzContent: '操作成功',
                });
                this.closeConfigOilMerNum();
            } else {
                this.modal.error({
                    nzTitle: '提示',
                    nzContent: data['return_msg'],
                });
            }
        });
    }

    // 打开配置第三方模态框
    showTripartitePlatformModal () {
        this.selectChannel(this.channelId);
        this.tripartitePlatformModal = true;
    }

    // 关闭配置第三方模态框
    closeTripartitePlatformModal () {
        this.tripartitePlatformModal = false;
    }

    // 提交第三方
    submitTripartitePlatform() {
        for (const i in this.tripartitePlatformForm.controls) {
            this.tripartitePlatformForm.controls[i].markAsDirty();
            this.tripartitePlatformForm.controls[i].updateValueAndValidity();
        }
        if (this.tripartitePlatformForm.status == null || this.tripartitePlatformForm.status !== 'VALID') {
            this.modal.warning({
                nzTitle: '提示',
                nzContent: '请填写所有必填项',
            });
            return;
        }

        if (this.tripartitePlatformForm.value.profitSharingStatus === 'true'
            && (this.tripartitePlatformForm.value.profitSharingRatio == null ||
                this.tripartitePlatformForm.value.profitSharingReceiversNo == null ||
                this.tripartitePlatformForm.value.profitSharingReceiversName == null)) {
            this.modal.warning({
                nzTitle: '提示',
                nzContent: '请填写所有必填项',
            });
            return;
        }

        this.tripartitePlatformForm.value['channelId'] = this.channelId;
        this.gasChannelConfigService.editTripartitePlatform(this.tripartitePlatformForm.value, data => {
            if (data['return_code'] === '000000') {
                this.modal.success({
                    nzTitle: '提示',
                    nzContent: '保存成功',
                });
                this.closeTripartitePlatformModal();
            } else {
                this.modal.error({
                    nzTitle: '提示',
                    nzContent: data['return_msg'],
                });
            }
        });

    }

    // 选择商户号
    selectChannel(channelId: string) {
        this.channelId = channelId;
        this.gasChannelConfigService.getDetail(Number(channelId), this.tripartitePlatformForm.value['payPlatformType'], data => {
            if (data['return_data'] != null) {
                data['return_data']['channelId'] = String(data['return_data']['channelId']);
                data['return_data']['payPlatformType'] = String(data['return_data']['payPlatformType']);
                data['return_data']['profitSharingStatus'] = String(data['return_data']['profitSharingStatus']);
                this.tripartitePlatformForm.patchValue(data['return_data'])
            } else {
                const param = {
                    channelId: channelId,
                    payPlatformType: String(this.tripartitePlatformForm.value['payPlatformType']),
                    profitSharingStatus: 'false'
                }
                this.tripartitePlatformForm.reset();
                this.tripartitePlatformForm.patchValue(param);
            }
        });
    }
}

