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

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

    searchForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    id = null;
    storeId = null;
    name: string;
    price: string;
    WEB_SERVE_URL = environment.baseUrl;
    importErrorStudentArray = [];
    errorStudentVisible = false;
    isVisibleAdd = false;
    isSpinning = false;

    constructor(
        private form: FormBuilder,
        private merchantStore: MerchantStoreService,
        private iconService: IconService,
        private message: NzMessageService,
        private router: Router,
        private index: IndexService,
        private modal: NzModalService,
        private activatedRoute: ActivatedRoute,
        private common: CommonsService
    ) {
    }

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

    }

    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({
            storeName: [null],
            telephone: [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;
        if (this.id == null) {
            this.merchantStore.getStoreListByMerchant(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']);
                }
            });
        } else {
            whereObject['merchantId'] = this.id;
            this.merchantStore.getStoreListByCompany(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.merchantStore.deleteMerchantStore(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/merchantStore/store-edit'], {
            queryParams: {
                storeId: 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));
    }

    handleCancelError(): void {
        this.errorStudentVisible = false;
    }

    handleChange(info: NzUploadChangeParam): void {
        this.isSpinning = true;
        if (info.file.status !== 'uploading') {
            console.log(info.file, info.fileList);
        }
        if (info.file.status === 'done') {
            if (info.file.response.return_code === '000000') {
                this.loading = false;
                this.isSpinning = false;
                if (info.file.response.return_data.errorTotal === 0) {
                    this.message.success('导入成功');
                    this.getRequest(true, this.searchForm.value);
                } else {
                    this.modal.warning({
                        nzTitle: '提示',
                        nzOkText: '查看失败数据',
                        nzContent: '只有部分数据导入成功',
                        nzOnOk: () => this.showErrorStudentModal(info.file.response.return_data)
                    });
                }
            } else {
                this.isSpinning = false;
                this.loading = false;
                this.message.error(info.file.response.return_msg);
            }
        } else if (info.file.status === 'error') {
            this.message.error('上传错误');
        }
    }

    //  打开模态框
    showErrorStudentModal(data: []) {
        this.getRequest(true, this.searchForm.value);
        this.importErrorStudentArray = data['errorData'];
        this.errorStudentVisible = true;
    }

    // 增加余额
    public getRecharge(id): void {
        this.storeId = id;
        this.isVisibleAdd = true;
    }


    handleOk(): void {
        if (this.price == null) {
            this.message.error('请输入充值金额');
            return;
        }
        const paramsObject = {
            storeId: this.storeId,
            amount: this.price
        };
        this.index.recharge(paramsObject, data => {
            if (data['return_code'] === '000000') {
                this.getRequest(false, this.searchForm.value);
                this.isVisibleAdd = false;
            } else {
                this.message.error(data['return_msg']);
            }
        });
    }
}
