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 {AuditService} from '../../../services/audit.service';
import {CommonsService} from '../../../services/commons.service';

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

    searchForm: FormGroup; // 搜索框
    requestData = []; // 列表数据
    total: number; // 页码
    pageNum = 1; // 页码
    pageSize = 10; // 条码
    loading = true;
    isVisible = false;
    id: number;
    objectType;
    data = {
        approve: {},
        object: {}
    };
    approveObject: any;
    constructor(
        private form: FormBuilder,
        private audit: AuditService,
        private iconService: IconService,
        private message: NzMessageService,
        private common: CommonsService,
    ) {
    }

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

    }

    public init(): void {
        this.searchForm = this.form.group({
            objectType: [null],
            objectName: [null],
            approveSerialNo: [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.audit.getApproveList(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 getDetail(id: number , objectType: number): void {
        this.id = id;
        this.objectType = objectType;
        this.audit.getApproveDetail(id, data => {
            if (data['return_code'] === '000000') {
                this.data = data['return_data'];
                if (objectType === 3) {
                    console.log();
                    this.approveObject = JSON.parse(data['return_data']['approve'].approveObject);
                }
                this.isVisible = true;
            } else {
                this.message.error(data['return_msg']);
            }
        });

    }

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

    public approveProcessed(statusD: number, id: number): void {
        this.id = id;
        const p = {
            approveId: this.id,
            status: statusD,
            remarks: '',
        };
        this.common.showConfirm('是否确定', r => {
            if (r) {

                this.audit.approveProcessed(p, data => {
                    if (data['return_code'] === '000000') {
                        this.isVisible = false;
                        this.getRequest(true, this.searchForm.value);
                        this.message.success('操作成功');
                    } else {
                        this.message.error(data['return_msg']);
                    }
                });
            }
        });

    }
}
