import {
  Modal
} from "@hot/pc-es-cn-shop-ui";
let defaultConfig = {
  className:"cd-jimi-modal",
  width: 600,
  height: 500,
  draggable: false,
  content: '',
  officialCustomerServiceUrl:'//conecli.com/jd/chat?entry=jd_srv_vd',
  definedCustomerServiceUrl:'//conecli.com/agile/MTAwMjk3NDcz',
  source :'zyMrYb'
};
import Domain from '../util/domain';
export default function jimi(options){
    let dialog;
    options = {
        ...defaultConfig,
        ...options
    }
    function getLink(callback){
        let getDate = new Date();
        let systemTime;
        let link = options.officialCustomerServiceUrl;
        $.getJSON(Domain.zt+'/service.html?functionId=getServerTime&callback=?').done(function(data){
            if(data.code === 200){
                getDate = new Date(data.data);
            }
        }).always(function(){
            systemTime = {
                fullDate:getDate,
                weekDay : getDate.getDay(),
                hour:getDate.getHours()
            }
            if (systemTime.weekDay != 0 && systemTime.weekDay != 6){
                if(systemTime.hour >= 9 && systemTime.hour < 18){
                    link = options.definedCustomerServiceUrl
                }
            }
            callback && callback(link)
        });
    }
    getLink(function(link){
        let jimiSize = options.width + '-' + options.height,
            head = '<div class="cd-jimi-title"><span class="zb-jingling-logo"></span>京灵智能商家助手<a target="_blank" href="'+link+'" class="zb-custom-service right J_toCustomService">人工客服</a><span class="cd-close-jimi J_close-jimi" title="关闭">x</span></div>',
            url = '//conecli.com/index.action?source='+options.source+'&layoutSize=' + jimiSize;
        options.content = head+'<div class="zb-ui-loading J_uiLoading"><p class="loading-tip">正在加载京灵，请稍候...</p></div><iframe onload="jimiLoad()" id="'+options.id+'" frameborder="0" scrolling="no" src="'+url +'" width="'+options.width+'" height="'+options.height+'"></iframe>';
        dialog = new Modal(options);
        dialog.$dialog.on('click','.J_close-jimi',function(){
            dialog.close()
        });
    });
    window.jimiLoad = function(){
        dialog.$dialog.find('.J_uiLoading').remove();
    }
    setTimeout(function(){
        window.jimiLoad();
    },1000)
}