$(function () {
    $("#jqGrid").jqGrid({
        url: '../sys/smslog/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '发送人', name: 'userName', index: 'user_name', width: 80},
            {label: '发送编号', name: 'sendId', index: 'send_id', width: 60},
            {label: '发送内容', name: 'content', index: 'content', width: 250},
            {label: '手机号', name: 'mobile', index: 'mobile', width: 50},
            {
                label: '发送时间', name: 'stime', index: 'stime', width: 60, formatter: function (value) {
                return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            }
            },
            // {label: '用户签名', name: 'sign', index: 'sign', width: 80},
            {
                label: '发送状态', name: 'sendStatus', index: 'send_status', width: 30, formatter: function (value) {
                if (value === 1) {
                    return '<span class="label label-success">成功</span>';
                }
                return '<span class="label label-danger">失败</span>';
            }
            },
            // {label: '无效号码数', name: 'invalidNum', index: 'invalid_num', width: 80},
            // {label: '成功提交数', name: 'successNum', index: 'success_num', width: 80},
            // {label: '黑名单数', name: 'blackNum', index: 'black_num', width: 80},
            {label: '返回消息', name: 'returnMsg', index: 'return_msg', width: 60}],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        config: {},
        ruleValidate: {
            domain: [
                {required: true, message: '发送域名不能为空', trigger: 'blur'}
            ],
            name: [
                {required: true, message: '用户名不能为空', trigger: 'blur'}
            ],
            pwd: [
                {required: true, message: '接口密钥不能为空', trigger: 'blur'}
            ],
            sign: [
                {required: true, message: '签名不能为空', trigger: 'blur'}
            ]
        },
        q: {
            sendId: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        addConfig: function (event) {
            vm.showList = false;
            vm.title = "短信配置";
            vm.getConfig();
        },
        updateConfig: function (event) {
            let url = "../sys/smslog/saveConfig";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.config),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        getConfig: function () {
            Ajax.request({
                url: "../sys/smslog/config",
                async: true,
                successCallback: function (r) {
                    vm.config = r.config;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'sendId': vm.q.sendId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                sendId: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.updateConfig()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        sendSms: function () {
            openWindow({
                type: 2,
                title: '发送短信',
                area: ['800px', '400px'],
                content: ['sendsms.html'],
                cancel: function (index) {
                    vm.reload();
                }
            });
        }
    }
});