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

const Api = new API();


export default {
    data() {
        return {
            showReasonList: false,
            showSortList: false,            
            reasonMap: this.$store.state.reasonMap,
            sortList: [],
            reason: '',
            text:'',
            sort: {
                id: 0,
                name: ''
            },
            fid: '',                     //板块id
            tid: '',                     //主题id
            topiclist: '',               //帖子id
            formhash: '',                //提交参数 app提供
            apiUrl: '/api/mobile/?module=topicadmin&action=type&version=2'
        }
    },

    mounted () {
        lib.setTitle('帖子分类')
        this.fid = lib.getParam('fid') || ''
        this.tid = lib.getParam('tid')
        this.topiclist = lib.getParam('topiclist')
        this.formhash = lib.getParam('formhash')

        this.getSortList()
    },

    methods: {
        selectReason(reason) {
            this.reason = reason
            this.showReasonList = false            
        },
        selectSort(sort) {
            this.sort = sort;
            this.showSortList = false 
        },   
        submit() {
            if(!this.validate()) {
                lib.showDialog('请选择分类')
                return
            }
            this.doSort()
        },

        validate() {
            if(this.sort.name) {
                return true 
            } else {
                return false
            }
        },

        getSortList() {
            var param = {
                fid: this.fid,
                tid: this.tid,
                topiclist: this.topiclist,
                formhash: this.formhash,
                get_type: 1,
                reason: this.text
            }
            Api.postData(this.apiUrl, param).done( ret => {
                if(ret.Message) {
                    lib.showDialog(ret.Message.messagestr)
                }
                this.sortList = ret.Variables.types
                this.sort = this.sortList.find((item) => item.id == ret.Variables.selected_id)
            }).fail( ret => {
                lib.showDialog(ret.Message.messagestr)
                //提示错误信息
                console.log(ret)
            })

        },

        doSort() {
            var param = {
                fid: this.fid,
                tid: this.tid,
                topiclist: this.topiclist,
                formhash: this.formhash,
                typeid: this.sort.id,
                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)
            })
        }
    },
    watch: {
        reason (cur, old) {
            if(this.reason) {
                this.text = this.reason
            }else{
                this.text = ''
            }
        }
    }



}