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

@Component({
    selector: 'app-user-list',
    templateUrl: './user-list.component.html',
    styleUrls: ['./user-list.component.scss']
})
export class UserListComponent implements OnInit {
    WEB_SERVE_URL = environment.baseUrl;
    searchForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    regTime;

    constructor(
        private form: FormBuilder,
        private user: UserService,
        private iconService: IconService,
        private message: NzMessageService,
        private common: CommonsService
    ) {
    }

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

    public init(): void {
        this.searchForm = this.form.group({
            name: [null],
            status: [null],
            phone: [null],
            regTime: [null],
        });
        this.getRequest(true, this.searchForm.value);
    }

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

        if (whereObject['regTime'] != null && whereObject['regTime'].length !== 0) {
            whereObject['regTimeStart'] = whereObject['regTime'][0].getTime();
            whereObject['regTimeEnd'] = whereObject['regTime'][1].getTime();
        }

        this.loading = false;
        if (reset) {
            this.pageNum = 1;
        }
        whereObject['pageNum'] = this.pageNum;
        whereObject['pageSize'] = this.pageSize;
        this.user.getListUser(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, status: any): void {
        const message = (status === 1 ? '是否禁用当前用户' : '是否启用当前用户');

        this.common.showConfirm(message, data => {
            if (data) {
                this.user.forbiddenUser(id, dataUser => {
                    this.getRequest(false , this.searchForm.value);
                });
            }
        });
    }

    // 是否设置成代理商
    setUpAgent(id , isAgent: boolean) {
        const message = (isAgent === true ? '当前用户禁用推广员' : '启用推广员');

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

                });
            }
        });
    }

}
