import API from 'API'
import lib from 'lib'


const Api = new API();

export default {

    mounted () {
        this.type = lib.getParam('type') || 'top'       
        switch (this.type) {
            case 'top' : lib.setTitle('置顶主题')  ; break;
            case 'open' : lib.setTitle('打开主题')  ; break;
            case 'close' : lib.setTitle('关闭主题')  ; break;          
                        
        }  
        this.fid = lib.getParam('fid') || ''
        this.tid = lib.getParam('tid') || ''
        this.formhash = lib.getParam('formhash') || ''
        
        
    },


    data() {
        return {
            showTimeList: false, //显示选择时间列表   
            showLevelList:false, //显示选择等级列表
            showReasonList:false, //显示选择等级列表             
            isOpt: false,        //是否自定义时间
            reasonMap: this.$store.state.reasonMap,
            topLevelList:this.$store.state.topLevelList,
            type:  'top',  //操作类型
            reason:'',
            text:'',
            form: {
                time:'none',
                timestamp: new Date()
            },
            topLevel: '',
            fid : '',
            tid : '',
            formhash : '',
            timeMap:{
                none: '无',
                week: '一周',
                day: '一天',
                month: '一个月',
                months: '三个月',
                opt: '自定义'
            },
            apiUrl:{
                top: '/api/mobile/?module=topicadmin&action=stick&version=2',    //接口链接
                open: '/api/mobile/?module=topicadmin&action=open&version=2',
                close: '/api/mobile/?module=topicadmin&action=close&version=2'
            } 
        }
        
    },

    methods: {
        submit() {
            let expirationstick = this.formateDate(new Date())
            let param = {
                fid: this.fid,
                tid: this.tid,
                formhash: this.formhash,
                reason: this.text,
            }
            if(this.type == 'top') {
                param.sticklevel = this.topLevel
            }
            switch (this.type) {
                case 'top' : param.expirationstick = expirationstick  ; break;
                case 'open' : param.expirationopen = expirationstick  ; break;
                case 'close' : param.expirationclose = expirationstick  ; break;          
                            
            }
            Api.postData(this.apiUrl[this.type], param).done( ret => {
                //判断状态
                lib.showDialog(ret.Message.messagestr)
            }).fail( ret => {
                //提示错误信息
                lib.showDialog(ret.Message.messagestr)
                
            })
        },
        
        selectTime(key) {
            if(key == 'opt') {
                this.$refs.picker.open();
            }else{
                this.isOpt = false;
            }
            this.form.time = key
            this.showTimeList = false            
        },
        selectReason(reason) {
            this.reason = reason
            this.showReasonList = false            
        },
        selectLevel(level) {
            this.topLevel = level
            this.showLevelList = false            
        },
        confirmDate(time) {
            this.isOpt = true
            this.timestamp = time
        },
        formateDate(date) {
            let newDate = date
            switch(this.form.time) {
                case 'week' : newDate.setDate(newDate.getDate() + 7); break
                case 'day' : newDate.setDate(newDate.getDate() + 1); break
                case 'month' : newDate.setMonth(newDate.getMonth() + 1); break
                case 'months' : newDate.setMonth(newDate.getMonth() + 3); break
                case 'none' : return 0; break
                case 'opt' : 
                    newDate = this.form.timestamp
                    newDate.setHours(new Date().getHours() )
                    newDate.setMinutes(new Date().getMinutes() )
                    newDate.setSeconds(new Date().getSeconds())
                    break
            }
            let o = {
                "y": newDate.getFullYear(),   //年份
                "M": newDate.getMonth() + 1, //月份 
                "d": newDate.getDate(), //日 
                "h": newDate.getHours(), //小时 
                "m": newDate.getMinutes(), //分 
                "s": newDate.getSeconds(), //秒 
            };

            let timeString = `${o.y}-${o.M}-${o.d} ${o.h}:${o.m}:${o.s}`
            return timeString
        }
    
        
    },
    computed: {
        formatDate() {
            return {
                date: this.form.timestamp.getDate(),
                month: this.form.timestamp.getMonth()+1,
                year: this.form.timestamp.getFullYear()
            }
        }
    },
    watch: {
        reason (cur, old) {
            if(this.reason) {
                this.text = this.reason
            }else{
                this.text = ''
            }
        }
    }
}