import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';
import {CommonsService} from '../../../services/commons.service';
import {CouponService} from '../../../services/coupon.service';
import {MerchantService} from '../../../services/merchant.service';
import {ConfigService} from "../../../services/config.service";

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

    searchForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    id = null;
    name: string;
    merchantName: string;
    optionList: string[] = [];
    isLoading = false;
    pageNumMer = 1;
    goodsTypeData;
    brandData;
    constructor(
        private form: FormBuilder,
        private coupon: CouponService,
        private merchant: MerchantService,
        private iconService: IconService,
        private message: NzMessageService,
        private router: Router,
        private config: ConfigService,
        private activatedRoute: ActivatedRoute,
        private common: CommonsService
    ) {
    }

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

        const params = {
            pageNum: 1,
            pageSize: 10000
        };
        this.config.getListGoodsType(params , data => {
            this.goodsTypeData = data['return_data'].list;
        });

    }

    public getBrandByList(e): void {

        const params = {
            pageNum: 1,
            pageSize: 10000,
            goodTypeId: e
        };

        this.config.getBrandByList(params , data => {
            this.brandData = data['return_data'].list;
        });
    }

    public init(): void {
        this.activatedRoute.queryParams.subscribe(queryParams => {
            if (queryParams.merchantId != null) {
                this.id = queryParams.merchantId;
                this.name = queryParams.name;
            }
        });
        this.searchForm = this.form.group({
            merchantId: [null],
            couponName: [null],
            couponType: [null],
            goodsTypeId: [null],
            brandId: [null],
            displayArea: [null],
            status: [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.coupon.getCouponList(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 getDelete(id): void {
        const message = '是否删除门店';
        this.common.showConfirm(message, data => {
            if (data) {
                // this.coupon.disableMerchant(id, dataReturn => {
                //     if (dataReturn['return_code'] === '000000') {
                //         this.message.success('操作成功');
                //         this.getRequest(false, this.searchForm.value);
                //     } else {
                //         this.message.warning(dataReturn['return_msg']);
                //     }
                // });
            }
        });
    }

    // 修改
    public getEdit(id: number): void {
        this.router.navigate(['/admin/coupon/coupon-edit'], {
            queryParams: {
                couponId: id
            }
        }).then(r => console.log(r));
    }

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

    // 查询商户
    loadMore(): void {
        this.isLoading = true;
        const paramsObject = {
            pageNum: this.pageNumMer,
            pageSize: 10,
            merchantName: this.merchantName,
        };
        this.getMerchantList(paramsObject);
    }

    // 搜索商户
    search(e): void {
        this.optionList = [];
        this.isLoading = true;
        this.merchantName = e;
        this.pageNumMer = 1;
        const paramsObject = {
            pageNum: this.pageNumMer,
            pageSize: 10,
            merchantName: e,
        };
        this.getMerchantList(paramsObject);
    }

    // 查询商户
    public getMerchantList(paramsObject): void {
        this.merchant.getMerchantList(paramsObject , data => {
            if (data['return_code'] === '000000' && this.pageNumMer <= data['return_data'].pages ) {
                this.pageNumMer ++;
                this.isLoading = false;
                this.optionList = [...this.optionList, ...data['return_data'].list];
            } else {
                this.isLoading = false;
            }
        });
    }

    // 下架卡券
    public disableMerchant(id): void {
        const message =  '是否下架卡券';
        this.common.showConfirm(message, data => {
            if (data) {
                this.coupon.ofShelfApprove(id, dataReturn => {
                    if (dataReturn['return_code'] === '000000') {
                        this.message.success('操作成功');
                        this.getRequest(false , this.searchForm.value);
                    } else {
                        this.message.warning(dataReturn['return_msg']);
                    }
                });
            }
        });
    }

    // 上架
    public enableMerchant(id): void {
        const message =  '是否上架审核卡券';
        this.common.showConfirm(message, data => {
            if (data) {
                this.coupon.upShelfApprove(id, dataReturn => {
                    if (dataReturn['return_code'] === '000000') {
                        this.message.success('操作成功');
                        this.getRequest(false , this.searchForm.value);
                    } else {
                        this.message.warning(dataReturn['return_msg']);
                    }
                });
            }
        });
    }

    // 查看门店列表
    public getList(id: number , couponName: string , couponType: number): void {
        console.log(couponType);
        this.router.navigate(['/admin/coupon/salesCode-list'], {
            queryParams: {
                couponId: id,
                name: couponName,
                type: couponType,
            }
        }).then(r => console.log(r));
    }

    // 刷新中石化
    public getGuizhouSinopec(): void {
       this.coupon.getGuizhouSinopec(data => {
           if (data['return_code'] === '000000') {
               this.message.success('操作成功');
               this.getRequest(false , this.searchForm.value);
           } else {
               this.message.warning(data['return_msg']);
           }
       });
    }

}
