{% extends "manage/apps/index.html" %}
{% block add_form_extra %}
{% endblock %}
{% block edit_form_extra %}
{% endblock %}
{% block sub_javascript %}
    <script>
        function init() {
        }
        $(function () {
            var table_id = '#mTable',
                fm_add = '#fm-add',
                dialog_add = '#dialog-add',
                dialog_edit = '#dialog-edit',
                search_url = "{% url 'manage-contacts-search' %}",
                add_url = "{% url 'manage-contacts-add' %}",
                edit_url = "{% url 'manage-contacts-edit' %}",
                delete_url = "{% url 'manage-contacts-delete' %}",
                ban_url = "{% url 'manage-contacts-ban' %}",
                reset_url = "{% url 'manage-contacts-reset' %}";

            renderTable({
                url: search_url,
                columns: [[
                    {field: 'name', title: '名称', width: 100},
                    {field: 'email', title: '邮箱', width: 100},
                    {
                        field: 'action', title: '操作', width: 120, formatter: function (val, row, index) {
                        var actions = [];
                        actions.push("<a class='button-inline edit' href='" + edit_url + ("?id=" + row.id) + "' data-index='" + index + "'>编辑</a>");
                        actions.push("<a class='button-inline action-confirm' href='" + delete_url + ("?id=" + row.id) + "' data-hit='确定删除联系人吗'>删除</a>");
                        return actions.join('<span class="text-explode">|</span>');
                    }
                    }
                ]]
            });

            init();
            $(document).on("click", '#add', function (e) {
                e.preventDefault();
                $(fm_add).form('clear');
                $(fm_add).attr('action', add_url);
                $('select').trigger('change.select2');
                $(dialog_add).dialog('open').dialog('setTitle', '新建');
            });
            $(document).on('click', '.edit', function (e) {
                e.preventDefault();
                var index = $(this).attr('data-index');
                var action = $(this).attr('href');
                var row = $(table_id).datagrid('getData').rows[index];
                $(dialog_edit).find('form').form('clear');
                $(dialog_edit).find('form').form('load', row);
                $('select').trigger('change.select2');
                $(dialog_edit).find('form').attr('action', action);
                $(dialog_edit).dialog('setTitle', '编辑').dialog('open');
            });

            $(document).on('click', '.action-confirm', function (e) {
                e.preventDefault();
                var $this = $(this);
                var hit = $this.attr('data-hit');
                var action = $this.attr('href');
                myPostConfirm('提示', hit, action, {'csrfmiddlewaretoken': CSRF_TOKEN}, function () {
                    $(table_id).datagrid("reload");
                });
            });

            $(document).on('click', '.dialog-close', function (e) {
                e.preventDefault();
                var $this = $(this);
                var dialog = $this.attr('data-dialog');
                $(dialog).dialog('close');
            });

            $(document).on('click', '#save-add, #save-edit', function (e) {
                e.preventDefault();
                var fm_id = $(this).attr('data-fm');
                var dialog = $(this).attr('data-dlg');
                var action = $(fm_id).attr('action');
                var data = getFormParams(fm_id);
                data['csrfmiddlewaretoken'] = CSRF_TOKEN;
                post(action, data, function (result) {
                    if (result.errcode == 200) {
                        showMsg('操作成功');
                        $(dialog).dialog('close');
                        $(table_id).datagrid("reload");
                    } else {
                        showMsg(result.errmsg);
                    }
                });
            });
        });
    </script>
{% endblock %}