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

const Api = new API();


export default {
    data() {
        return {
            showReasonList: false,
            showTimeList: false,
            reason: '',
            text: '',
            date: new Date(),
            timeMap:{
                none: '无',
                week: '一周',
                day: '一天',
                month: '一个月',
                months: '三个月',
                opt: '自定义'
            },
            form: {
                time:'none',
                timestamp: new Date()
            },
            font: {
                bold: false,
                ita: false,
                ul: false,
                color:0,
                // style:'1'
            },
            colorList:[      //颜色选择列表
                '#000',
                '#f3493d',
                '#e69b31',
                '#c16e20',
                '#44da4b',
                '#3db3f2',
                '#523df2',
                '#ce3df2',
                '#ff2b68'
            ],
            isActiveColor: false,    //颜色选择框 
            reasonMap : this.$store.state.reasonMap,
            fid : '',
            tid : '',
            formhash : '',
            apiUrl: '/api/mobile/?module=topicadmin&action=highlight&version=2',
            isOpt: false,
        }
    },
    mounted () {
        lib.setTitle('高亮主题')        
        this.fid = lib.getParam('fid') || ''
        this.tid = lib.getParam('tid')
        this.formhash = lib.getParam('formhash')
        
        
    },


    methods: {
        openDatePick() {
            this.$refs.picker.open();
        },
        confirmDate(time) {
            this.isOpt = true
            this.timestamp = time
        },
        showColorPicker() {
            this.isActiveColor = !this.isActiveColor
            this.showReasonList = false
        },
        selectColor(index) {
            this.font.color = index
            this.isActiveColor = false;
        },
        selectReason(reason) {
            this.reason = reason
            this.showReasonList = false            
        },
        submit() {
            let expirationstick = this.formateDate(new Date())
            let param = {
                fid: this.fid,
                tid: this.tid,
                reason: this.text,
                formhash: this.formhash,
                expirationhighlight: expirationstick,
                highlight_color : this.font.color,
                highlight_style: this.highlight_style
            }
            Api.postData(this.apiUrl, param).done( ret => {
                lib.showDialog(ret.Message.messagestr)
                
            }).fail( ret => {
                lib.showDialog(ret.Message.messagestr)
                //提示错误信息
                console.log(ret)
            })
        },

        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
        },

        selectTime(key) {
            if(key == 'opt') {
                this.$refs.picker.open();
            }else{
                this.isOpt = false;
                this.form.time = key
            }
            
            this.showTimeList = false            
        },
    },

    computed: {
        formatDate() {
            return {
                date: this.date.getDate(),
                month: this.date.getMonth()+1,
                year: this.date.getFullYear()
            }
        },
        highlight_style() {
            return [
                
                
                this.font.ul ? 1 : 0,
                this.font.bold ? 1 : 0,
                this.font.ita ? 1 : 0,
            ]
        }
    },
    watch: {
        reason (cur, old) {
            if(this.reason) {
                this.text = this.reason
            }else{
                this.text = ''
            }
        }
    }
}