import {Component, OnInit} from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {DiscountService} from '../../../services/discount.service';
import {CouponService} from '../../../services/coupon.service';
import {IconService} from '../../../services/icon.service';
import {Router} from '@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {RechargeService} from '../../../services/recharge.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'app-price-list',
    templateUrl: './price-list.component.html',
    styleUrls: ['./price-list.component.scss']
})
export class PriceListComponent implements OnInit {
    WEB_SERVE_URL = environment.imageUrl;
    searchForm: FormGroup; // 搜索框
    validateForm: FormGroup; // 添加框
    requestData = []; // 列表数据

    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    isVisible = false;
    id: number;
    edit = false;

    isVisibleGoods = false;
    realPrice = 0;
    goodsArray;
    constructor(
        private form: FormBuilder,
        private recharge: RechargeService,
        private discount: DiscountService,
        private coupon: CouponService,
        private iconService: IconService,
        private message: NzMessageService,
        private router: Router,
        private common: CommonsService
    ) {
    }

    ngOnInit(): void {
        this.init();
    }

    public init(): void {
        this.searchForm = this.form.group({
            type: [null],
        });
        this.validateForm = this.form.group({
            type: [null, [Validators.required]],
            price: [null, [Validators.required]],
            realPrice: [{value: null, disabled: true}, [Validators.required]],
            // goodsId: [null, [Validators.required]],
            discount: [null, [Validators.required]],
            sort: [null, [Validators.required]],
            // channel: [null, [Validators.required]],
        });


        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.recharge.getListOutRechargePrice(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 getEdit(id?: number): void {
        if (id != null) {
            this.id = id;
            this.edit = true;
            this.recharge.findById(id, data => {
                this.validateForm.patchValue(data['return_data']);
                this.isVisible = true;
            });
        } else {
            this.validateForm.reset();
            this.isVisible = true;
            this.edit = false;
        }
    }

    // 查看详情
    public getDetail(id: number): void {
        this.router.navigate(['/admin/company/company-detail'], {
            queryParams: {
                companyId: id
            }
        }).then(r => console.log(r));
    }


    handleOk(): void {
        // tslint:disable-next-line:forin
        for (const i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
            if (this.validateForm.controls[i].errors != null) {
                this.message.error('必填项不能为空');
                return;
            }
        }
        this.validateForm.value.realPrice = this.validateForm.value.price * this.validateForm.value.discount / 100;
        if (this.edit) {
            this.validateForm.value['id'] = this.id;
            this.recharge.updatePrice(this.validateForm.value, data => {
                if (data['return_code'] === '000000') {
                    this.message.success('修改成功');
                    this.getRequest(false, this.searchForm.value);
                    this.isVisible = false;
                } else {
                    this.message.error(data['return_msg']);
                }
            });
        } else {
            this.recharge.insertPrice(this.validateForm.value, data => {
                if (data['return_code'] === '000000') {
                    this.message.success('添加成功');
                    this.getRequest(false, this.searchForm.value);
                    this.isVisible = false;
                } else {
                    this.message.error(data['return_msg']);
                }
            });
        }

    }

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

    handleCancelGoods(): void {
        this.isVisibleGoods = false;
    }
    public getForbiddenUser(id, status: any): void {

        const message = (status === 1 ? '是否禁用' : '是否启用');

        this.common.showConfirm(message, data => {
            if (data) {
                this.recharge.onAndOffShelves(id,  dataUser => {
                    if (dataUser['return_code'] === '000000') {
                        this.message.success('操作成功');
                        this.getRequest(false , this.searchForm.value);
                    } else {
                        this.message.error(dataUser['return_msg']);
                    }
                });
            }
        });
    }

    public getForbiddenUserD(id): void {

        this.common.showConfirm('是否删除', item => {
            if (item) {
                this.recharge.deletePrice(id, data => {
                    if (data['return_code'] === '000000') {
                        this.message.success('删除成功');
                        this.getRequest(false, this.searchForm.value);
                    } else {
                        this.message.error(data['return_msg']);
                    }
                });
            }
        });
    }

    public getMemberGoods(): void {
        this.isVisibleGoods = true;
        this.recharge.getMemberGoods(data => {
            this.goodsArray = data['return_data']['data'];
        });
    }

    public getPrice(): void {
        this.realPrice = this.validateForm.value.price * this.validateForm.value.discount / 100;
    }



}
