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 MobWFStartViewEngine from 'src/engine/view/mob-wfstart-view-engine';
import { EditForm } from '@widget/qj/start-form-form/start-form-form';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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



    /**
     * 返回
     *
     * @memberof QJMobWFStartView
     */
    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 QJMobWFStartView
     */
    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 QJMobWFStartView
     */
    public  async openActionSheet(data:any){

        
    }

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

    

    /**
     * 实例化对象
     *
     * @memberof QJMobWFStartView
     */
    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 QJMobWFStartView
     */
    public ngOnInit() {
       
    }

    /**
     * 确认
     *
     * @memberof QJMobWFStartView
     */
    public  onOk(){
        this.form.startWorkFlow(this.form.$data).then((response:any) =>{
            if (!response ) {
                return;
            }
            this.$activeData = [{...response}];
            this.backView();
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
        });
    }

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


