import verification, {VerificationType} from '$utils/verification';
import popup from "$utils/tool/popup";
export default <LibMixins>{

    data(){
      return {
          buttonAsync:false,
          passwordValue:''
      }
    },

    watch:{
      title:function () {
          if (!this.title) {
              return this.triggerCancel();
          }
      },
        password:function () {
          this.changePassword(this.password);
        },
    },

    methods:{

        changePassword(value){
            if (this.passwordValue !== value) {
                this.passwordValue = value;
            }
        },

        triggerCancel(){

            if (this.value) {

                this.$attrs.cancel && this.$attrs.cancel();

                return this.close();
            }

        },

        triggerConfirm(callback){

            if (this.trim && !verification.trigger[VerificationType.empty](this.passwordValue)) {
                return popup.$toast(this.placeholder);
            }

            if (this.value) {

                let resultData = this.$attrs.confirm && this.$attrs.confirm(this.passwordValue);

                if (resultData instanceof Promise) {
                    this.storageResultData = resultData;
                    this.buttonAsync = true;
                    callback();
                } else {
                    return this.close();
                }
            }

        },

        triggerWaiting(obj){
            this.storageResultData.then((value)=>{

                if (value === false) {
                    obj.none();
                    return this.close();
                } else {
                    obj.success();
                    this.triggerWaitingTime = setTimeout(()=> this.close(),this.successAsyncTime);
                }

            }).catch(()=>{
                obj.none();
                this.buttonAsync = false;
            });
        }

    },

    beforeUnmount() {
        clearTimeout(this.triggerWaitingTime);
    }

}