import {getAddNoticeEmail,getCode,getList,getUpdateInfo,deleteEmail} from "../../api/alerts";
import {getImageUrl} from "../../utils/publicMethods";
export default {
    data() {
      return{
        receiveData:{
            img:"",
            maId:"",
            coin:"",
            ma:"",
        },
        dialogVisible:false,
        params:{
            email:"",
            remark:"",
            code:"",
            maId:""
        },
        tableData:[
            {
                email:"5656",
            }
        ],
        alertsLoading:false,      

        addMinerLoading:false,
        btnDisabled: false,
        btnDisabledClose: false,
        btnDisabledPassword: false,
        bthText: "user.obtainVerificationCode",
        bthTextClose: "user.obtainVerificationCode",
        bthTextPassword: "user.obtainVerificationCode",
        time: "",
        countDownTime: 60,
        timer: null,
        countDownTimeClose: 60,
        timerclose: null,
        countDownTimePassword: 60,
        timerPassword: null,
        listParams:{
            maId:"",
            limit:10,
            page:1,

        },
        modifyDialogVisible:false,
        modifyRemark:"",
        modifyParams:{
            id:"",
            remark:"",  
        },
        deleteLoading:false,
        userEmail:"",

      }
    },

    computed: {    
        countDownPassword() {
            const minutes = Math.floor(this.countDownTimePassword / 60);
            const seconds = this.countDownTimePassword % 60;
            const m = minutes < 10 ? "0" + minutes : minutes;
            const s = seconds < 10 ? "0" + seconds : seconds;
            return `${s}`;
            // return`${s}`
        },

    },
    created() {
   
        if (window.sessionStorage.getItem("alerts_time")) {
            this.countDownTimePassword = Number(window.sessionStorage.getItem("alerts_time"));
            this.startCountDownPassword()
            this.btnDisabledPassword = true;
            this.bthTextPassword = `user.again`
        }

    },


    mounted() {
        let userEmail=localStorage.getItem("userEmail")
        this.userEmail= JSON.parse(userEmail)
        window.addEventListener("setItem", () => {
            let userEmail=localStorage.getItem("userEmail")
            this.userEmail= JSON.parse(userEmail)
        });
        this.params.email = this.userEmail
        if (this.$route.query) {
            this.receiveData = this.$route.query;
            this.listParams.maId = this.receiveData.id
            this.params.maId = this.receiveData.id
        }
        this.fetchList(this.listParams)
        this.registerRecoveryMethod('fetchList', this.listParams);

    },
    methods:{
        getImageUrl(path) {
            return getImageUrl(path);
          },
        async fetchAddNoticeEmail(params){
            // this.addMinerLoading = true
            this.setLoading('addMinerLoading', true);
            const data = await getAddNoticeEmail(params)
            if (data && data.code == 200) {
                this.$message({
                    type: "success",
                    message:this.$t("alerts.addedSuccessfully"),
                    
                });
                this.fetchList(this.listParams)

                this.dialogVisible = false
                
                for (const key in this.params) {
                 
                    
                    if (key !== "maId") {
                          this.params[key] =""
                    }
                  
                }
            }
            this.setLoading('addMinerLoading', false);
            
        },
        async fetchList(params){
            // this.alertsLoading=true
            this.setLoading('alertsLoading', true);
            const data = await getList(params)
            if (data && data.code == 200) {
               this.tableData = data.rows

            }
            this.setLoading('alertsLoading', false);
            
        },
        async fetchCode(params){
            const data = await getCode(params)
            if (data && data.code == 200) {
                this.$message({
                    type: "success",
                    message:this.$t("user.verificationCodeSuccessful"),//this.$t("user.addSuccess") 
                    
                });
                
            }
            
        },
        async fetchUpdateInfo(params){
                // this.addMinerLoading = true
                this.setLoading('addMinerLoading', true);
            const data = await getUpdateInfo(params)
            if (data && data.code == 200) {
                this.$message({
                    type: "success",
                    message:this.$t("alerts.modifiedSuccessfully"),//this.$t("user.addSuccess")
                    
                });
                this.modifyDialogVisible = false
                this.fetchList(this.listParams)
            }

            this.setLoading('addMinerLoading', false);
            
        },
        async fetchDeleteEmail(params){
            // this.deleteLoading = true
            this.setLoading('deleteLoading', true);
            const data = await deleteEmail(params)
            if (data && data.code == 200) {
                this.$message({
                    type: "success",
                    message: this.$t("alerts.deleteSuccessfully"),//this.$t("user.addSuccess")
                    
                });
              
                this.fetchList(this.listParams)
            }

            this.setLoading('deleteLoading', false);

        },

        add(){
            this.dialogVisible =true
        },
        confirmAdd(){
             //邮箱格式验证
        const emailRegex =/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/; 
        this.params.email = this.params.email.trim()     
        let isMailbox = emailRegex.test(this.params.email);
        
        if (!this.params.email || !isMailbox) {
            this.$message({
                message: this.$t(`user.emailVerification`),
                type: "error",
                customClass: "messageClass",
                showClose: true
            });
            return
        }
        if ( !this.params.code) {
            this.$message({
                message: this.$t(`personal.eCode`),
                type: "error",
                customClass: "messageClass",
                showClose: true
            });
            return
        }

            this.fetchAddNoticeEmail(this.params)
        },
        modify(item){
       
            this.modifyParams.id = item.id
            this.modifyParams.remark = item.remark
            this.modifyDialogVisible =true
        },
        confirmModify(){
            if (!this.modifyParams.remark) {
                this.$message({
                    message:this.$t("alerts.modificationReminder"),
                    type: "error",
                    customClass: "messageClass",
                    showClose: true
                });

                return
            }
            this.fetchUpdateInfo( this.modifyParams)
        },
        handelDelete(item){
            this.fetchDeleteEmail({id:item.id})
        },
        handelCode() {
            //邮箱格式验证
            const emailRegex =/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/; 
            this.params.email = this.params.email.trim()     
            let isMailbox = emailRegex.test(this.params.email);
            
            if (!this.params.email || !isMailbox) {
                this.$message({
                    message: this.$t(`user.emailVerification`),
                    type: "error",
                    customClass: "messageClass",
                    showClose: true
                });
                return
            }
            if (this.listParams.maId !== 0 && !this.listParams.maId) {
                this.$message({
                    message: this.$t("alerts.acquisitionFailed"),
                    type: "error",
                    customClass: "messageClass",
                    showClose: true
                });
                return
            }
         
            this.fetchCode({email:this.params.email,maId:this.listParams.maId})

            if (window.sessionStorage.getItem("alerts_time") == null) {
                this.startCountDownPassword()
            } else {
                this.countDownTimePassword = Number(window.sessionStorage.getItem("alerts_time"));
                this.startCountDownPassword()
            }
           
        },
        startCountDownPassword() {
            this.timerPassword = setInterval(() => {

                if (this.countDownTimePassword <= 1) {
                    //当监测到countDownTime为0时,清除计数器并且移除sessionStorage,然后执行提交试卷逻辑
                    clearInterval(this.timerPassword);
                    sessionStorage.removeItem("alerts_time");

                    this.countDownTimePassword = 60
                    this.btnDisabledPassword = false;
                    this.bthTextPassword = `user.obtainVerificationCode`
                } else if (this.countDownTimePassword > 0) {
                    //每秒让countDownTimePassword -1秒,并设置到sessionStorage中
                    this.countDownTimePassword--;
                    this.btnDisabledPassword = true;
                    this.bthTextPassword = `user.again`
                    window.sessionStorage.setItem("alerts_time", this.countDownTimePassword);
                }
            }, 1000);
        },
    }
 
}