import Ajax from "../ajax.js";
import Modal from "../modal.js"
import ClassSelect from "../select2.js";
import Alert from "../alerts/alert.js";

class ModalDirectMessageClient {
    constructor(model="presupuesto",domItem = null) {
        this.item = domItem
        this.modal = new Modal(domItem)
        this.alert = new Alert(this.item.find("#alertDirectMessage"))
        this.modelId = this.item.data("id");
        this.selectItem = this.item.find("#select-clients")
        this.model = model
        this.selectClientUser = new ClassSelect(this.selectItem,`/chat/direct/client/users/select/${this.model}/${this.modelId}`,"Seleccione contacto",true)
        this.messageInput = this.item.find("#new-direct-message-cliente-text")
        this.title = this.item.find("#new-direct-message-cliente-title")
        this.btnSubmitMessage = this.item.find("#submit-new-direct-message-client")
    }
    init() {
        this.selectClientUser.init()
        this.modal.item.on("hidden.bs.modal",this.reset_close.bind(this))
        this.modal.item.on("shown.bs.modal",this.reset_show.bind(this))
        this.btnSubmitMessage.on("click",this._handleStoreChatDirectMessage.bind(this))
    }
    reset(){
        this.messageInput.val("")
        this.title.val("")
        this.selectClientUser.reset();
    }
    reset_close(){
        this.messageInput.val("")
        this.title.val("")
        this.selectClientUser.reset();
        this.alert.hide()
        this.alert.setHeadingTitle("")
        this.alert.setContent("")
        this.alert.setAsSuccess()
    }
    reset_show(){
        this.messageInput.val("")
        this.title.val("")
        this.selectClientUser.reset();
        this.alert.hide()
        this.alert.setHeadingTitle("")
        this.alert.setContent("")
        this.alert.setAsSuccess()
    }
    _handleStoreChatDirectMessage() {
        const data = { "message": this.messageInput.val(), "title": this.title.val() , "users" : this.selectClientUser.getVal() }
        if (data.message) {
            const ajax = new Ajax(
                `/messages/direct`,
                data,
                null,
                this._handleStoreNewDirectMessageSuccess.bind(this),
                this._handleStoreNewDirectMessageError.bind(this)
            )
            ajax.post()
        }else{
            this.alert.show()
            this.alert.setAsWarning()
            this.alert.setHeadingTitle("Tienes que añadir un mensaje")
        }
    }
    _handleStoreNewDirectMessageSuccess(response) {
        try {
            this.alert.setAsSuccess()
            this.alert.setHeadingTitle(response.message)
            this.alert.setContent(response.message)
            this.alert.show()
        } catch (error) {
        } finally {
            this.reset()
        }

    }
    _handleStoreNewDirectMessageError(error){
        this.alert.setHeadingTitle(error.message)
        this.alert.setAsError()
        this.alert.setContent(JSON.stringify(error.errors))
        this.alert.show()
    }
}

export default ModalDirectMessageClient