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

const Api = new API();


export default {    
    data() {
        return {
            showReasonList: false,
            showSortList: false,            
            showPicker: false,
            reasonMap: this.$store.state.reasonMap,
            getSortApi: '/api/mobile/?module=foruminfo&version=2&getthreadtypes=1', 
            apiUrl: '/api/mobile/?module=topicadmin&action=move&version=2',
            slots: [
                {
                  flex: 1,
                  values: [{name:'',id:0}],
                  className: 'slot1',
                  textAlign: 'center'
                }, {
                  flex: 1,
                  values: [{}],
                  className: 'slot3',
                  textAlign: 'center'
                }
              ],

            sortList: [],            
            reason: '',
            forum: {
                id: 0,
                name: ''
            },
            sort:{
                id:0,
                name:''
            },
            fid: '',                     //板块id
            tid: '',                     //主题id
            topiclist: '',               //帖子id
            formhash: '',                //提交参数 app提供
            
        }
    },

    mounted () {
        lib.setTitle('帖子移动')
        this.fid = lib.getParam('fid') || ''
        this.tid = lib.getParam('tid') 
        this.topiclist = lib.getParam('topiclist') || ''
        this.formhash = lib.getParam('formhash')
        this.getForum()

    },

    methods: {
        onValuesChange(picker, values) {
            if(values[0]) {
                if(values[0].sub) {
                    this.slots[1].values = values[0].sub
                }else {
                    this.slots[1].values = []
                }
                this.forum = values[1] ? values[1] : values[0]
            } 
            
        },
        selectReason(reason) {
            this.reason = reason
            this.showReasonList = false            
        },
        
        selectSort(sort) {
            this.sort = sort;
            this.showSortList = false 
        }, 
        submit() {
            this.doMove()
        },

        doMove() {
            var param = {
                threadtypeid: this.sort.id,
                moveto: this.forum.id,
                fid: this.fid,
                tid: this.tid,
                topiclist: this.topiclist,
                formhash: this.formhash,
                reason: this.text,
            }
            Api.postData(this.apiUrl,param).done(ret => {
                if(ret.Message.messagestr) {
                    lib.showDialog(ret.Message.messagestr)
                }else {
                    lib.showDialog('未知错误')
                    console.log(ret)
                }
            }).fail( ret => {
                lib.showDialog(ret.Message.messagestr)
                //提示错误信息
                console.log(ret)
            })
        },

        

        getForum() {
            var param = {
                get_move:1,
                fid: this.fid,
                tid: this.tid,
                topiclist: this.topiclist,
                formhash: this.formhash,
            }
            Api.postData(this.apiUrl,param).done(ret => {
                this.slots[0].values = ret.Variables.forums
                this.slots[0].values.forEach( item => {
                    if(item.id == this.fid) {
                        this.$refs.picker.setSlotValue(0,item)
                    }
                    if(item.sub) {
                        item.sub.unshift({id:item.id,name:{name:item.name}});
                        item.sub.forEach( _item => {
                            _item.name = _item.name.name
                            if(_item.id == this.fid ) {
                                this.$refs.picker.setSlotValue(0,item)
                                this.$refs.picker.setSlotValue(1,_item)
                            }   
                            
                        })
                    }
                })
            })
        },

        confirmForum() {
            this.showPicker = false;
            this.getSortList(this.forum.id)
        },

        getSortList(fid) {
            var param = {fid}
            Api.postData(this.getSortApi,param).done(ret => {
                if(ret.Variables) {
                    this.sortList = ret.Variables.types
                    this.sort = this.sortList[0]
                }
                
            }).fail(ret => {
                this.sortList = []
                console.log(ret)
            })
        }

        
    },
    watch: {
        reason (cur, old) {
            if(this.reason) {
                this.text = this.reason
            }else{
                this.text = ''
            }
        },
        
    }



}