Object.assign = require('object.assign');
require('es6-promise').polyfill();
// 全局统一布局
import 'muse-ui/lib/styles/base.less';
import 'assets/css/ali-iconfont/iconfont.css';
import 'assets/css/ali-iconfont/iconfont.js';
import 'assets/css/lj-base.less';

// import Eruda from 'eruda'
// Eruda.init();
// window.api = {safeArea: {}};

import {
    // Alert,
    AppBar,
    AutoComplete,
    Avatar,
    Badge,
    // BottomNav,
    BottomSheet,
    // Breadcrumbs,
    Button,
    // Card,
    Checkbox,
    Chip,
    DateInput,
    DataTable,
    Dialog,
    Divider,
    Drawer,
    // ExpansionPanel,
    Form,
    Grid,
    // GridList,
    // Helpers,
    // Icon,
    List,
    LoadMore,
    Menu,
    // Pagination,
    Paper,
    // Picker,
    Popover,
    Progress,
    Radio,
    Select,
    SlidePicker,
    // Slider,
    // Snackbar,
    // Stepper,
    SubHeader,
    Switch,
    Tabs,
    TextField,
    // Tooltip,
    theme
} from 'muse-ui';

import Vue from 'vue'
import vTap from 'v-tap'

import {
    ActionSheet,
    Button as VanButton,
    Badge as VanBadge,
    Checkbox as VanCheckbox,
    CheckboxGroup,
    Cascader,
    Calendar,
    Cell,
    CellGroup,
    Col,
    Collapse,
    CollapseItem,
    DropdownMenu, 
    DropdownItem,
    DatetimePicker,
    Dialog as VanDialog,
    Divider as VanDivider,
    Empty,
    Form as VanForm,
    Field,
    Grid as VantGrid,
    GridItem as VantGridItem,
    Icon,
    Image,
    Loading,
    List as VanList,
    NumberKeyboard,
    Overlay,
    Popup,
    Picker,
    PullRefresh,
    Popover as VanPopover,
    Progress as VanProgress,
    Row,
    Radio as VanRadio,
    RadioGroup as VanRadioGroup,
    Search,
    Switch as VanSwitch,
    Step,
    Steps,
    Stepper,
    Skeleton,
    Slider,
    Sticky,
    Swipe,
    SwipeItem,
    SwipeCell,
    Toast,
    Tab,
    Tabs as VanTabs,
    Tag,
    NoticeBar
    // Lazyload,
} from 'vant'

Vue.use(ActionSheet);
Vue.use(VanButton);
Vue.use(VanBadge);
Vue.use(VanCheckbox);
Vue.use(CheckboxGroup);
Vue.use(Cascader);
Vue.use(Calendar);
Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(Col);
Vue.use(Collapse);
Vue.use(CollapseItem);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(DatetimePicker);
Vue.use(VanDialog);
Vue.use(VanDivider);
Vue.use(Empty);
Vue.use(VanForm);
Vue.use(Field);
Vue.use(VantGrid);
Vue.use(VantGridItem);
Vue.use(Icon);
Vue.use(Image);
Vue.use(Loading);
Vue.use(VanList);
Vue.use(NumberKeyboard);
Vue.use(VanPopover);
Vue.use(VanProgress);
Vue.use(Overlay);
Vue.use(PullRefresh);
Vue.use(Popup);
Vue.use(Picker);
Vue.use(Row);
Vue.use(VanRadio);
Vue.use(VanRadioGroup);
Vue.use(Search);
Vue.use(VanSwitch);
Vue.use(Step);
Vue.use(Steps);
Vue.use(Stepper);
Vue.use(Skeleton);
Vue.use(Slider);
Vue.use(Sticky);
Vue.use(Swipe);
Vue.use(SwipeItem);
Vue.use(SwipeCell);
Vue.use(Toast);
Vue.use(Tab);
Vue.use(VanTabs);
Vue.use(Tag);
Vue.use(NoticeBar);
// Vue.use(Lazyload, {
//     lazyComponent: true,
// });

import 'vant/lib/dropdown-menu/style';
import 'vant/lib/dropdown-item/style';
import 'vant/lib/search/style';
import 'vant/lib/button/style';
import 'vant/lib/badge/style';
import 'vant/lib/field/style';
import 'vant/lib/row/style';
import 'vant/lib/cascader/style';
import 'vant/lib/cell/style';
import 'vant/lib/cell-group/style';
import 'vant/lib/col/style';
import 'vant/lib/popup/style';
import 'vant/lib/picker/style';
import 'vant/lib/index-bar/style';
import 'vant/lib/index-anchor/style';
import 'vant/lib/toast/style';
import 'vant/lib/action-sheet/style';
import 'vant/lib/swipe-cell/style';
import 'vant/lib/checkbox/style';
import 'vant/lib/list/style';
import 'vant/lib/loading/style';
import 'vant/lib/tab/style';
import 'vant/lib/tabs/style';
import 'vant/lib/empty/style';
import 'vant/lib/collapse/style';
import 'vant/lib/collapse-item/style';
import 'vant/lib/stepper/style';
import 'vant/lib/icon/local.css'
import 'vant/lib/datetime-picker/style';
import 'vant/lib/icon/style';
import 'vant/lib/divider/style';
import 'vant/lib/dialog/style';
import 'vant/lib/switch/style';
import 'vant/lib/pull-refresh/style';
import 'vant/lib/skeleton/style';
import 'vant/lib/sticky/style';
import 'vant/lib/popover/style';
import 'vant/lib/swipe/style';
import 'vant/lib/tag/style';
import 'vant/lib/swipe-item/style';
import 'vant/lib/progress/style';
import 'vant/lib/image/style';
import 'vant/lib/grid/style';
import 'vant/lib/grid-item/style';
import 'vant/lib/calendar/style';
import 'vant/lib/number-keyboard/style';
import 'vant/lib/step/style';
import 'vant/lib/steps/style';
import 'vant/lib/slider/style';
import 'vant/lib/form/style';
import 'vant/lib/notice-bar/style';
import 'vant/lib/overlay/style';

// Vue.use(Alert);
Vue.use(AppBar);
Vue.use(AutoComplete);
Vue.use(Avatar);
Vue.use(Badge);
// Vue.use(BottomNav);
Vue.use(BottomSheet);
// Vue.use(Breadcrumbs);
Vue.use(Button);
// Vue.use(Card);
Vue.use(Checkbox);
Vue.use(Chip);
Vue.use(DateInput);
Vue.use(DataTable);
Vue.use(Dialog);
Vue.use(Divider);
Vue.use(Drawer);
// Vue.use(ExpansionPanel);
Vue.use(Form);
Vue.use(Grid);
// Vue.use(GridList);
// Vue.use(Helpers);
// Vue.use(Icon);
Vue.use(List);
Vue.use(LoadMore);
Vue.use(Menu);
// Vue.use(Pagination);
Vue.use(Paper);
// Vue.use(Picker);
Vue.use(Popover);
Vue.use(Progress);
Vue.use(Radio);
Vue.use(Select);
Vue.use(SlidePicker);
// Vue.use(Slider);
// Vue.use(Snackbar);
// Vue.use(Stepper);
Vue.use(SubHeader);
Vue.use(Switch);
Vue.use(Tabs);
Vue.use(TextField);
// Vue.use(Tooltip);
Vue.use(vTap); //添加vue tap事件

import $lj from 'components/lj-public'

Vue.filter('strFilter', function (str) {
    str = $lj.strLastOne(str);
    return str;
});

Vue.filter('dateFilter', function (date) {
    date = $lj.dateformat(date, 'yyyy-MM-dd');
    return date;
});
Vue.filter('dateFilter2', function (date) {
    date = $lj.dateformat(date, 'yyyy/MM/dd');
    return date;
});
Vue.filter("timeFilter", function (date) {
    return $lj.dateformat(date, 'yyyy-MM-dd hh:mm');
});
Vue.filter('timeFilter2', function (date) {
    date = $lj.dateformat(date, 'M/d hh:mm');
    return date;
});
Vue.filter("moneyFilter", function (money) {
    return $lj.moneyFormat(money);
});
Vue.filter("moneyWan", function (money) {
    return $lj.moneyFormat(money,2,true);
});
Vue.filter("numberFilter", function (number) {
    if (isNaN(Number(number))) {
        return number;
    }
    return Number(number).toFixed(2);
});
Vue.filter("dateRangeFilter", function (sdate, edate) {
    let result = $lj.dateformat(sdate, 'M月d日 hh:mm');
    if (edate) {
        result += "-";
        let append = $lj.dateformat(edate, 'M月d日 hh:mm');
        if (result.substring(0, result.indexOf(" ")) === append.substring(0, append.indexOf(" "))) {
            result += append.substring(append.indexOf(" ") + 1);
        } else {
            result += append;
        }
    }
    return result;
});
Vue.filter("dateRangeSimpleFilter", function (sdate, edate) {
    let result = $lj.dateformat(sdate, 'M/d hh:mm');
    if (edate) {
        result += "-";
        let append = $lj.dateformat(edate, 'M/d hh:mm');
        if (result.substring(0, result.indexOf(" ")) === append.substring(0, append.indexOf(" "))) {
            result += append.substring(append.indexOf(" ") + 1);
        } else {
            result += append;
        }
    }
    return result;
});
Vue.filter("numberUnitFilter", function (number) {
    if (isNaN(Number(number))) {
        return number;
    }
    let unit = "B";
    let resultNumber = number;
    if (resultNumber >= 1000){
        resultNumber /= 1000;
        unit = "K";
    }
    if (resultNumber >= 1000){
        resultNumber /= 1000;
        unit = "M";
    }
    if (resultNumber >= 1000){
        resultNumber /= 1000;
        unit = "G";
    }
    if (resultNumber >= 1000){
        resultNumber /= 1000;
        unit = "T";
    }
    return Number(resultNumber).toFixed(2) + unit;
});

Vue.filter("thousandSeparatorFilter", function (number, max) {
    let rtNum = number;
    let ifplus = false;
    if (max && Number(number) > max) {
        rtNum = max;
        ifplus = true;
    }
    rtNum = rtNum.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
    if (ifplus) {
        rtNum += "+";
    }
    return rtNum
});

//数字格式化为百分比
Vue.filter('percentage', function(value, decimals = 2) {  
    if (!value) return '0%';  
    value = parseFloat(value);  
    if (isNaN(value)) return '-';  
    const factor = Math.pow(10, decimals);  
    const fcValue = Math.round(value * factor) / factor;  
    return `${(fcValue * 100).toFixed(decimals)}%`;  
  });

// 防抖：提交以后禁用按钮一段时间，防止重复提交
Vue.directive('noMoreClick', {
    inserted(el, binding) {
        el.addEventListener('click', e => {
            el.classList.add('is-disabled')
            el.disabled = true
            setTimeout(() => {
                el.disabled = false
                el.classList.remove('is-disabled')
            }, 800)
        })
    }
});

/**
 * 比例指令，让元素能够按照一定的比例显示
 */
Vue.directive('proportion', {
    inserted: function (el, binding) {
        var w = el.offsetWidth;
        var h = w * binding.value;
        el.style.height = h + 'px';
        el.__resize = function () {
            var w = el.offsetWidth;
            var h = w * binding.value;
            el.style.height = h + 'px';
        }
        window.addEventListener('resize', el.__resize);
    },

    componentUpdated: function (el, binding) {
        var w = el.offsetWidth;
        var h = w * binding.value;
        el.style.height = h + 'px';
    },

    unbind: function (el) {
        window.removeEventListener('resize', el.__resize);
    }
});

window.$lj = $lj;

// 通用token
$lj.token = $lj.getStorage("token") || "";

// muse-ui 的color赋值使用
$lj.museUIStyleVariable = {
    // 单选按钮颜色
    radioButtonColor: "#000000"
};

theme.add('lj-light', {
    primary: '#2196f3',
    secondary: '#ff4081',
    success: '#4caf50',
    warning: '#fdd835',
    info: '#2196f3',
    error: '#f44336',
    track: '#bdbdbd'
}, 'light');
theme.use('lj-light');

import ProxyCreateTemplateSingleton from './L1APP/pda/bluetooth-print-template.js'

window.$BlueToothPrintTemplate = new ProxyCreateTemplateSingleton();

export default Vue;