/**
 * Created with JetBrains WebStorm.
 * User: dfwang
 * Date: 14-6-16
 * Time: 下午12:02
 * To change this template use File | Settings | File Templates.
 */
define(function (require, exports) {
    var  _$object = require('editor/assert/js/utils/object').instance;
    var _h_$table = function(){
        this.canvas=null;
        this.dom = null; //返回的fabric对象
        this.fobject = null; //未用
        this.phtml =null;  //保存在json里的html

        //----------------- 打开后出来的iframe
        this.tablecot = null;
    };
    _h_$table.prototype.addElemet=function(_json){
        //PAGECURRENT.c.canvas.deactivateAll();

          this.dom = _json;

          if(_json){
              this.phtml = _json.hcontent;
              this.showEditorBtn();
          }else
          {
              this.render();
          }

    }
    _h_$table.prototype.render = function()
    {
        var _that = this;
        var _str = $('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'+
            '<div class="modal-dialog" style="width: 72%">'+
            '<div class="modal-content"> '+
            '<div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'+
            '<h4 class="modal-title" id="myModalLabel">表格</h4>'+
            '</div>'+
            '<div class="modal-body" style="height: 500px;padding: 0">'+
            '<iframe src="editor/assert/js/plugins/table/editor.html" width="100%" height="100%" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="" allowtransparency="yes"></iframe>'+
            '</div> '+
            '<div class="modal-footer"> '+
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>'+
            '<button type="button" class="btn btn-primary" id="add_table_bt">添加</button>'+
            '</div>'+
            '</div>'+
            '</div>'+
            '</div>');
        $('body').append(_str);
        setTimeout(function(){
            if(_that.tablecot)
            {
                _that.phtml = '<table>'+_that.tablecot.find('table').html()+'</table>';
            }
            _that.tablecot = $(window.frames[0].frames['ueditor_0'].document.body);
            if(_that.phtml) _that.tablecot.prepend(_that.phtml);
        },1000)
    //    this.tablecot =
        _str.on('hidden.bs.modal',function(e){
            _str.remove();
            _that.remove();
        })
        $('#add_table_bt').on('click',function(e)
        {
            var _tabledom = _that.tablecot.find('table');
            html2canvas(_tabledom, {
                onrendered: function (canvas) {
                    var _hc = {'hcontent':_that.phtml || 'table_'+commands._randomString(10)};
                    var _img = canvas.toDataURL('png');
                    if(!_that.phtml)
                    {
                        _that.dom =  _that.print(_img);
                        _that.dom.baseClass= _that;
                    }

                    /*var _a = {};
                    _a[_hc[1].hcontent] ='<table>'+_tabledom.html()+'</table>';
                    PAGECURRENT.c.addJsonProperty(_a);*/
                    _that.update(_img,_hc);
                    PAGECURRENT.c.activeObject = _that;
                }
            });
            _str.modal('hide');
        })
        commands.createHTPop(_str);
    }
    /**
     * 添到对象到舞台
     * @param img
     */
    _h_$table.prototype.print = function(img)
    {
        var _img = new Image();
        _img.src = img;
        var imgInstance = new fabric.Table(_img,{left:50,top:100,width:PAGECURRENT.c._canvas.width-100});
        return imgInstance;
    }
    commands._extend(_h_$table,_$object);
    exports.instance = _h_$table;
})
