define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    function queryParams (data, isPrefix) {
        isPrefix = isPrefix ? isPrefix : false
        let prefix = isPrefix ? '?' : ''
        let _result = []
        for (let key in data) {
        let value = data[key]
        // 去掉为空的参数
        if (['', undefined, null].includes(value)) {
            continue
        }
        if (value.constructor === Array) {
            value.forEach(_value => {
            _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
            })
        } else {
            _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
        }
        }

        return _result.length ? prefix + _result.join('&') : ''
    }
    var inof = {}
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'donate/index' + location.search,
                    add_url: 'donate/add',
                    edit_url: 'donate/edit',
                    del_url: 'donate/del',
                    multi_url: 'donate/multi',
                    import_url: 'donate/import',
                    table: 'donate',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                onClickRow:function(row, $element, field)
                {
                    console.log($element)
                    info = row
                    console.log(row,'row')
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cid', title: __('Cid')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'full_name', title: __('Full_name'), operate: 'LIKE'},
                        {field: 'sex', title: __('Sex'), searchList: {"0":__('Sex 0'),"1":__('Sex 1')}, formatter: Table.api.formatter.normal},
                        {field: 'birthday', title: __('Birthday'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'id_card', title: __('Id_card'), operate: 'LIKE'},
                        {field: 'vocation', title: __('Vocation'), operate: 'LIKE'},
                        {field: 'education', title: __('Education'), searchList: {"0":__('Education 0'),"1":__('Education 1'),"2":__('Education 2'),"3":__('Education 3'),"4":__('Education 4'),"5":__('Education 5'),"6":__('Education 6'),"7":__('Education 7'),"8":__('Education 8'),"9":__('Education 9')}, formatter: Table.api.formatter.normal},
                        {field: 'category_id', title: __('Category_id'), operate: 'LIKE'},
                        {field: 'tel', title: __('Tel'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'executor', title: __('Executor'), operate: 'LIKE'},
                        {field: 'executor_id_card', title: __('Executor_id_card'), operate: 'LIKE'},
                        {field: 'executor_tel', title: __('Executor_tel'), operate: 'LIKE'},
                        {field: 'executor_mobile', title: __('Executor_mobile'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"-1":__('Status -1'),"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'SN', title: __('Sn'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[{
                            name: 'daying',
                            title: '打印',
                            text: '打印',
                            icon: 'fa',
                            classname: 'btn btn-xs btn-info daying'
                            // url: 'info'
                        }]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $("#table").on("click",".daying",function () {
                // alert('点击了打印')
                window.open("http://red.liuniu946.com/dru/donation.html?" + queryParams(info))
            })
        },
        add: function () {
             $("#c-user_id").data("params", function (obj) {
                return {custom: {cid: $("#c-cid").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
             $("#c-user_id").data("params", function (obj) {
                return {custom: {cid: $("#c-cid").val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});