import { Component, OnInit,AfterViewInit,Output,Input,EventEmitter,ViewChild,ElementRef } from '@angular/core';
import { Subject,Unsubscribable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { NavController,ModalController } from '@ionic/angular';
import { AppNotification } from '@global/service/Notification';
import { App } from '@global/service/App';
import { Http } from '@global/service/Http';
import { ComUtil } from '@global/util/ComUtil';
import { AppEnvironment } from 'src/environments/AppEnvironment';


import MobWFActionViewEngine from 'src/engine/view/mob-wfaction-view-engine';
import { EditForm } from '@widget/qj/wfform-form/wfform-form';

@Component({
  selector: 'view-qjmob-wfaction-view',
  templateUrl: './qjmob-wfaction-view.page.html',
  styleUrls: ['./qjmob-wfaction-view.page.scss']
})
export class QJMobWFActionViewPage implements OnInit,AfterViewInit {

    /**
     * 子组件EditForm
     *
     * @public
     * @type {Subject}
     * @memberof QJMobWFActionView
     */
    @ViewChild(EditForm)
    private form: EditForm;

    /**
     * 视图订阅对象
     *
     * @public
     * @type {Subject}
     * @memberof QJMobWFActionView
     */
    public viewState: Subject<ViewState> = new Subject();

    /**
     * 路由传递参数
     *
     * @public
     * @type {void}
     * @memberof QJMobWFActionView
     */
    public $route ={};

    /**
     * 当前视图激活数据
     *
     * @public
     * @type {void}
     * @memberof QJMobWFActionView
     */
    public $activeData:Array<any> =[];

    /**
     * 是否为模态框弹出
     *
     * @memberof QJMobWFActionView
     */
    @Input() isModalMode:boolean =false;

    /**
     * 是否嵌入分页导航视图
     *
     * @memberof TestEntity2MobPickupView
     */
    @Input() isTabexp:boolean =false;

    /**
     * 视图值变化事件
     *
     * @public
     * @type {EventEmitter}
     * @memberof QJMobWFActionView
     */    
    @Output() public viewdataschange =new EventEmitter();

    /**
     * 视图参数
     *
     * @public
     * @type {Subject}
     * @memberof QJMobWFActionView
     */
    @Input() public viewdata:any;

    /**
     * 父数据对象
     *
     * @protected
     * @type {*}
     * @memberof QJMobWFActionView
     */
    protected srfparentdata: any = {};

    /**
     * 是否为内敛视图
     *
     * @public
     * @type {EventEmitter}
     * @memberof QJMobWFActionView
     */ 
    @Input() public isembeddedView:boolean =false;

    /**
     * 视图值加载完成事件
     *
     * @public
     * @type {EventEmitter}
     * @memberof QJMobWFActionView
     */ 
    @Output() public viewload =new EventEmitter();

    /**
     * 视图的关联试图Map
     *
     * @public
     * @type {Map}
     * @memberof QJMobWFActionView
     */
    public RefViewMap =new Map();

    /**
     * 视图引擎
     *
     * @private
     * @type {Engine}
     * @memberof QJMobWFActionView
     */
    private engine: MobWFActionViewEngine = new MobWFActionViewEngine();

    /**
     * 引擎初始化
     *
     * @private
     * @memberof QJMobWFActionView
     */
    private engineInit(): void {
        this.engine.init({
            view: this,
            form: this.form,
        });
    }


     /**
     * angular生命周期(在ngOnInit之后)
     *
     * @memberof QJMobWFActionView
     */
    public ngAfterViewInit(){
        this.engineInit();
        
        this.registerRefView();
    }


    /**
     * form 部件 wfstart 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public form_wfstart($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'wfstart', $event);
    }

    /**
     * form 部件 save 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public form_save($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'save', $event);
    }

    /**
     * form 部件 wfsubmit 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public form_wfsubmit($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'wfsubmit', $event);
    }

    /**
     * form 部件 remove 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public form_remove($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'remove', $event);
    }

    /**
     * form 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public form_load($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'load', $event);
    }



    /**
     * 返回
     *
     * @memberof QJMobWFActionView
     */
    public backView() {
        let res:any;
        if(this.isModalMode){
            if(this.$activeData && this.$activeData.length >0){
                res = { ret: 'OK', result:this.$activeData };
            }else{
                res = { ret: 'NO', result:[] };
            }
            this.$modalCtrl.dismiss(res, 'close');
        }else{
            this.$navCtrl.back();
            if(this.$activeData && this.$activeData.length >0){
                res = { ret: 'OK', result:this.$activeData };
            }else{
                res = { ret: 'NO', result:[] };
            }
            this.$app.closeCallback(res);
        }
    }

    /**
     * 嵌入视图点击更多
     *
     * @public
     * @memberof QJMobWFActionView
     */
    public async click_more(){
        let leftopts:Array<any> =[
    ];
        let rightopts:Array<any> =[
    ];
        let opts:Array<any>=leftopts.concat(rightopts);
        const result: string = await this.$app.openActionSheet({items:opts});
        let _this =this;
    }

    /**
     * 点击toolbar的操作组事件
     *
     * @public
     * @memberof QJMobWFActionView
     */
    public  async openActionSheet(data:any){

        
    }

    /**
     * 注册关联视图
     *
     * @public
     * @memberof QJMobWFActionView
     */    
    public registerRefView(){
    }   

    

    /**
     * 实例化对象
     *
     * @memberof QJMobWFActionView
     */
    constructor(public $http:Http,public $navCtrl: NavController, public $notification: AppNotification, public $activatedRoute: ActivatedRoute,public $modalCtrl:ModalController,public $app:App) {
        this.$activatedRoute.queryParams.subscribe(params => {
        this.$route ="";
        if(params){
            this.$route =JSON.stringify(params);
        }
        })
    }

    /**
     * angular生命周期(组件初始化)
     *
     * @memberof QJMobWFActionView
     */
    public ngOnInit() {
       
    }

    /**
     * 视图抛出值变化事件
     *
     * @public
     * @memberof QJMobWFActionView
     */
    public viewDataChanges($event:any){
        this.viewdataschange.emit($event);
    }

    /**
     * 确定
     *
     * @public
     * @memberof QJMobWFActionView
     */
    public onOK($event:any){
        let _this:any = this;
        this.wf_Step(_this.engine.viewdata.item,$event,this.form.$data).then((res:any) =>{
            if(!res){
                this.$notification.error(_this.engine.viewdata.item.name+'失败');
                return;
            }
            this.$notification.success(_this.engine.viewdata.item.name+'成功');
            this.$activeData = [{...res}];
            this.backView();
        }).catch((error:any) =>{
            this.$notification.error(_this.engine.viewdata.item.name+'失败');
        })
    }

    /**
     * 工作流步骤
     *
     * @private
     * @param {*} item
     * @param {*} $event
     * @param {*} [data]
     * @returns {Promise<any>}
     * @memberof QJMobWFActionView
     */
    private async wf_Step(item: any, $event: any, data?: any): Promise<any> {
        let _this:any = this;
        const arg: any = { args: {} };
        if (data) {
            Object.assign(arg, data);
        }
        if (_this.engine && _this.engine.viewdata && _this.engine.viewdata.srfkey) {
            Object.assign(arg, { srfkey: _this.engine.viewdata.srfkey });
        }
        if (!arg.srfkey || Object.is(arg.srfkey, '')) {
            return;
        }
        Object.assign(arg, { link: item.id });
        if (!arg.link || Object.is(arg.link, '')) {
            return;
        }

        const url: string = 'mob/demo/qj/wflink';
        const post: Promise<any> = this.$http.post(url, arg);
        return post;
    }
}


