//查询退款申请单
function getQuotation(page, limit, startTime, endTime, offerName){
    layer.load(3);
    var url = "";
    if(idVal == "0"){ //未审核
        url = "/wxRefund/getUserRefund";
    } else { //已审核
        url = "/wxRefund/getAuditRefund";
    }
    $.ajax({
        url: url,
        type: "get",
        data: {
            "page": page,
            "limit": limit,
            "startTime": startTime,
            "endTime": endTime,
            "offerName": offerName,
            "refundState": idVal
        },
        headers: {"token": localStorage.getItem("token")},
        dataType: "json",
        success: (data) => {
            if(data.msg == "200"){
                $(".content_div div").remove();
                $.each(data.data, (index, item) => {
                    //状态显示
                    var buttonElem = "";
                    if(item.refundState == "0"){ //未审核
                        buttonElem = "<button class=\"ignore_button button_div_head\" onclick='ignoreClick(this)'><img src=\"/images/demandForSingle/ban.png\"/><span data-locale=\"jujue\">"+ paramReturn("拒绝", parent.type) +"</span></button>\n" +
                            "<button class=\"offer_button button_div_head\" onclick='offerClick(this)'><img src=\"/images/refund/tongyi.png\"/><span data-locale=\"tongyituikuan\">"+ paramReturn("同意退款", parent.type) +"</span></button>\n";
                    } else if(item.refundState == "1"){ //已同意
                        buttonElem = "<button class=\"offer_button button_div_head audit_refund\"><img src=\"/images/refund/yitongyi.png\"/><span data-locale=\"yitongyi\">"+ paramReturn("已同意", parent.type) +"</span></button>\n";
                    } else if(item.refundState == "2"){ //拒绝
                        buttonElem = "<button class=\"ignore_button button_div_head audit_refund\"><img src=\"/images/refund/refused.png\"/><span data-locale=\"yijujue\">"+ paramReturn("已拒绝", parent.type) +"</span></button>\n" +
                            "<button class=\"offer_button button_div_head\" onclick='offerClick(this)'><img src=\"/images/refund/tongyi.png\"/><span data-locale=\"tongyituikuan\">"+ paramReturn("同意退款", parent.type) +"</span></button>\n";
                    }
                    $(".content_div").append("<div payType="+ item.payType +" offerCode="+ item.offerCode +" uuid="+ item.releaseUuid +" class=\"content_column\">\n" +
                        "<div class=\"content_div_input\">\n" +
                        "<div class=\"head_name\" style=\"width: 5%;\">"+ (index + 1) +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 20%;\">"+ item.offerName +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 15%;\">"+ item.uuid +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 10%;\">"+ item.jdrq.split(" ")[0] +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 10%;\">"+ (item.refundType == "1"  ? ""+ paramReturn("部分退款", parent.type) +"" : ""+ paramReturn("全部退款", parent.type) +"") +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 10%;\">"+ fixedReturn("refundReason", item.refundReason) +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 10%;\">"+ item.refundFee +"</div>\n" +
                        "<div class=\"head_name\" style=\"width: 20%;\">\n" +
                        buttonElem +
                        "</div>\n" +
                        "</div>\n" +
                        "</div>")
                });
                //自定义样式分页
                laypage.render({
                    elem: 'demo2'
                    ,count: data.count
                    ,theme: '#1E9FFF'
                    , limit: limit
                    , curr: page
                    , jump: function (obj,first) {
                        if (!first) {
                            getQuotation(obj.curr,obj.limit, startTime, endTime, offerName);
                        }
                    }
                    , prev: '<em><i class="layui-icon">&#xe603;</i></em>'
                    , next: '<em><i class="layui-icon">&#xe602;</i></em>'
                });
            }
            layer.closeAll("loading");
        },
        error: () => {
            layer.alert(paramReturn("服务器异常，请联系管理员", parent.type), {icon: 2});
            layer.closeAll("loading");
        }
    });
}
//同意退款
function offerClick(obj){
    layer.confirm("确认此操作吗？", {
        btn: ['确定', '取消'],
        title: false,
        skin: 'layclass'
    }, function () {
        layer.load(3);
        var uuid = $(obj).parent().parent().parent().attr("uuid");
        var offerCode = $(obj).parent().parent().parent().attr("offerCode");
        var payType = $(obj).parent().parent().parent().attr("payType");
        var url = "/wxRefund/refusedRefund";
        if(payType == "微信支付"){
            url = "/wxRefund/refusedRefund";
        } else if(payType == "支付宝支付"){
            url = "https://www.tuyatrip.com/api/zfbRefund/refusedRefund";
        }
        sendPrivateText(offerCode);
        $.ajax({
            url: url,
            type: "put",
            data: {
                "refundState": "1",
                "releaseUuid": uuid
            },
            headers: {"token": localStorage.getItem("token")},
            dataType: "json",
            success: (data) => {
                if(data.msg == "200"){
                    layer.msg(data.data, {icon: 1});
                    sendPrivateText(offerCode);
                } else {
                    layer.msg(data.data, {icon: 2});
                }
                getQuotation(1, 7, startTime, endTime);//查询报价单数据渲染
                layer.closeAll("loading");
            },
            error: () => {
                layer.alert(paramReturn("服务器异常，请联系管理员", parent.type), {icon: 2});
                layer.closeAll("loading");
            }
        });
    },function (){
        layer.closeAll()
    })
}
//拒绝
function ignoreClick(obj){
    layer.confirm("确认此操作吗？", {
        btn: ['确定', '取消'],
        title: false,
        skin: 'layclass'
    }, function () {
        layer.load(3);
        var uuid = $(obj).parent().parent().parent().attr("uuid");
        var offerCode = $(obj).parent().parent().parent().attr("offerCode")
        $.ajax({
            url: "/wxRefund/refusedRefund",
            type: "put",
            data: {
                "refundState": "2",
                "releaseUuid": uuid
            },
            headers: {"token": localStorage.getItem("token")},
            dataType: "json",
            success: (data) => {
                layer.closeAll("loading");
                if(data.msg == "200"){
                    layer.msg(data.data, {icon: 1});
                    getQuotation(1, 7, startTime, endTime);//查询报价单数据渲染
                    sendPrivateText(offerCode);
                }
            },
            error: () => {
                layer.alert(paramReturn("服务器异常，请联系管理员", parent.type), {icon: 2});
                layer.closeAll("loading");
            }
        });
    },function (){
        layer.closeAll()
    });
}
//注册并登录环信账号
function registerUser(){
    var options = {
        username: parent.username.toLowerCase(),
        password: '123456',
        nickname: parent.nickname,
        appKey: WebIM.config.appkey,
        success: function () { },
        error: function (err) {
            let errorData = JSON.parse(err.data);
        },
    };
    conn.registerUser(options);

    var options1 = {
        user: parent.username.toLowerCase(),
        pwd: '123456',
        appKey: WebIM.config.appkey
    };
    conn.open(options1);
    //
    setTimeout(function (){
        let options = {
            nickname: parent.nickname,
            avatarurl: parent.headImg,
        }
        //修改用户信息
        conn.updateOwnUserInfo(options);
        //查询列表
        conn.getSessionList().then((res) => {

        })
    },2000);
}
// 单聊发送文本消息
function sendPrivateText(offerCode) {
    if(typeof offerCode == "null"){
        console.log("发送消息失败")
        return false;
    }
    let id = conn.getUniqueId();                 // 生成本地消息id
    let msg = new WebIM.message('txt', id);      // 创建文本消息
    msg.set({
        msg: '我已对您的退款申请进行了操作，请注意查看',                         // 消息内容
        to: offerCode,                          // 接收消息对象（用户id）
        chatType: 'singleChat',              // 设置为单聊
        success: function (id, serverMsgId) {

        },
        fail: function(e){
            console.log("失败");
        }
    });
    conn.send(msg.body);
    $(".chat__room-info-input textarea").val("");
}