import { tpl, tbodyTpl } from './shopCategoryTpl';
import { Messager } from '@core/component/msgDistribute';
import LinkTool from '../../linkTool';
import PageTable from '@core/component/pageTable';
import {fetch} from '@core/component/httpClient';
import ShortPagination from '@core/component/shortPagination';
import Domain from '@core/util/domain';
import DefaultDatasM from '@core/component/form/DefaultDatasM';
const isv_v1 = true
const ShopCategory = (($) => {
    const DefaultConfig = {
        tpl,
        tbodyTpl,
    };
    class ShopCategory {
        constructor(config) {
            this._config = this._getConfig(config);
            this.el = this._config.node;
            this.$el = $(this.el);
            let html = template.render(this._config.tpl, this._config);
            this.$el.html(html);
            this.pageTable = new PageTable({
                node: this.$el.find('.J_table')[0],
                className: "cd-mytable",
                theadRenderer: ['分类ID', '分类名称', '分类链接'],
                itemRenderer: this._config.tbodyTpl,
                pageSize: 100,
                loading: true,
                fixedThead: true,
                columnWidth: ["20%", "30%", "50%"],
                frontEndPaging: false,
                pagination: ShortPagination,
                onDomReady: ($node) => {
                    console.log("onDomReady:");
                },
                onPageChange: (data) => {
                    this.queryData(data.selected);
                },
                onDomUpdate: this._config.onDomUpdate
            })
            this.queryData();
            this._addEvent();
        }
        
        async queryData() {
            let { platformId, shopType } = this._config.env;
            (shopType === 3 || shopType === 4) && (shopType = 0); 
            let data;
            if(window.page_data.isSDKM){
                data = DefaultDatasM.linkTool.listCate;
            }
            else{
                if (isv_v1) {
                    data = {
                        "result": true,
                        "msg": "",
                        "code": 200,
                        "data": {
                            "messages": [],
                            "messageMap": {},
                            "list": [{
                                "cid": 15321777,
                                "cateName": "Jack And Rose Tank’sDog",
                                "children": [{
                                    "cid": 15321778,
                                    "cateName": "女士长袖",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=15321778"
                                }, {
                                    "cid": 17421122,
                                    "cateName": "122",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17421122"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=15321777"
                            }, {
                                "cid": 16487918,
                                "cateName": "情man转转转",
                                "children": [{
                                    "cid": 16487919,
                                    "cateName": "长袖",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=16487919"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=16487918"
                            }, {
                                "cid": 17445638,
                                "cateName": "♫◎卍㏇♡",
                                "children": [{
                                    "cid": 19275197,
                                    "cateName": "测试",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19275197"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17445638"
                            }, {
                                "cid": 17561167,
                                "cateName": "一二三四五六七八九十十一",
                                "children": [{
                                    "cid": 17561168,
                                    "cateName": "<tag on*=*/>",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17561168"
                                }, {
                                    "cid": 17561169,
                                    "cateName": "一二三四五六",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17561169"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17561167"
                            }, {
                                "cid": 18219952,
                                "cateName": "test",
                                "children": [{
                                    "cid": 18422921,
                                    "cateName": "巴拉巴拉",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18422921"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219952"
                            }, {
                                "cid": 18219953,
                                "cateName": "1",
                                "children": [{
                                    "cid": 18219954,
                                    "cateName": "方法",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219954"
                                }, {
                                    "cid": 18219955,
                                    "cateName": "3",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219955"
                                }, {
                                    "cid": 18219956,
                                    "cateName": "4",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219956"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219953"
                            }, {
                                "cid": 18909206,
                                "cateName": "分类",
                                "children": [{
                                    "cid": 18909207,
                                    "cateName": "11",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18909207"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18909206"
                            }, {
                                "cid": 19307172,
                                "cateName": "xiaoyao0402",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19307172"
                            }, {
                                "cid": 19368014,
                                "cateName": "12333",
                                "children": [{
                                    "cid": 19368015,
                                    "cateName": "212",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19368015"
                                }, {
                                    "cid": 19368016,
                                    "cateName": "33",
                                    "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19368016"
                                }],
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19368014"
                            }]
                        },
                        "messages": [],
                        "messageMap": {},
                        "list": [{
                            "cid": 15321777,
                            "cateName": "Jack And Rose Tank’sDog",
                            "children": [{
                                "cid": 15321778,
                                "cateName": "女士长袖",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=15321778"
                            }, {
                                "cid": 17421122,
                                "cateName": "122",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17421122"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=15321777"
                        }, {
                            "cid": 16487918,
                            "cateName": "情man转转转",
                            "children": [{
                                "cid": 16487919,
                                "cateName": "长袖",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=16487919"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=16487918"
                        }, {
                            "cid": 17445638,
                            "cateName": "♫◎卍㏇♡",
                            "children": [{
                                "cid": 19275197,
                                "cateName": "测试",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19275197"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17445638"
                        }, {
                            "cid": 17561167,
                            "cateName": "一二三四五六七八九十十一",
                            "children": [{
                                "cid": 17561168,
                                "cateName": "<tag on*=*/>",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17561168"
                            }, {
                                "cid": 17561169,
                                "cateName": "一二三四五六",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17561169"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=17561167"
                        }, {
                            "cid": 18219952,
                            "cateName": "test",
                            "children": [{
                                "cid": 18422921,
                                "cateName": "巴拉巴拉",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18422921"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219952"
                        }, {
                            "cid": 18219953,
                            "cateName": "1",
                            "children": [{
                                "cid": 18219954,
                                "cateName": "方法",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219954"
                            }, {
                                "cid": 18219955,
                                "cateName": "3",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219955"
                            }, {
                                "cid": 18219956,
                                "cateName": "4",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219956"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18219953"
                        }, {
                            "cid": 18909206,
                            "cateName": "分类",
                            "children": [{
                                "cid": 18909207,
                                "cateName": "11",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18909207"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=18909206"
                        }, {
                            "cid": 19307172,
                            "cateName": "xiaoyao0402",
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19307172"
                        }, {
                            "cid": 19368014,
                            "cateName": "12333",
                            "children": [{
                                "cid": 19368015,
                                "cateName": "212",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19368015"
                            }, {
                                "cid": 19368016,
                                "cateName": "33",
                                "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19368016"
                            }],
                            "cateUrl": "//conecli.com/search?shopId=170905&shopCategoryId=19368014"
                        }],
                        "message": "",
                        "msgLevel": 0
                    }
                } else {
                    data = await fetch({
                        url: Domain.backend + '/mshop/category/listCate.json',
                        data: {
                            platformId,
                            shopType,
                        },
                        dataType: 'jsonp',
                    });
                }
            }
            if (data.result === true) {
                this.pageTable.update({
                    loading: false,
                    dataProvider: data.data.list
                })
                this.$el.find('[' + this._config.linkAttr + ']:checked').trigger('change');
            } else {
                Message.error(data.msg);
            }
        }
        
        _addEvent() {
            let _this = this;
            
            this.linkChangeKey = Messager.on('linkChange', (req, res) => {
                this.$el.find('[' + _this._config.linkAttr + ']').each(function() {
                    let val = $(this).attr(_this._config.linkAttr);
                    if (val === res.link) {
                        $(this).closest('.J_rowItem').addClass('active');
                        this.checked = true;
                    } else {
                        this.checked = false;
                    }
                })
            })
            
            this.$el.on('click.shopCategory', '.J_firCate,.J_secCate', function() {
                $(this).find('>input[type=radio]').trigger('change');
            })
            
            this.$el.on('dblclick.shopCategory', '.J_firCate', function() {
                Messager.trigger('confirm');
            })
            
            this.$el.on('dblclick.shopCategory', '.J_secCate', function(e) {
                Messager.trigger('confirm');
                e.stopPropagation();
            })
            
            this.$el.on('click.shopCategory', '.J_arrow', function() {
                const $row = $(this).closest('.J_rowItem');
                if ($row.is('.active')) {
                    $row.removeClass('active');
                } else {
                    selectRow($row[0])
                }
                return false;
            })
            
            this.$el.on('click.shopCategory', '.J_rowItem', function() {
                const $this = $(this);
                selectRow(this);
            })
            
            this.$el.on('change.shopCategory', '[' + _this._config.linkAttr + ']', function(e) {
                let $this = $(this);
                let link = $this.attr(_this._config.linkAttr);
                let originalData = $this.attr(_this._config.originalDataAttr);
                let linkType = LinkTool.Type.ShopCategory;
                let extraData = JSON.parse($(this).attr('data-extra-data'));
                Messager.trigger('linkChange', { link, linkType, originalData, extraData });
            })
            
            this.$el.on('click.shopCategory', '.J_ok', function() {
                Messager.trigger('confirm');
            })
            
            this.$el.on('click.shopCategory', '.J_cancel', function() {
                Messager.trigger('cancel');
            })
            function selectRow(row) {
                let $row = $(row);
                $row.siblings().removeClass('active');
                $row.addClass('active');
            }
        }
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config);
        }
        
        _renderDom() {
            let config = this._config;
            let html = template.render(config.tpl, config);
            this.$el.html(html);
        }
        
        _getData() {
        }
        
        dispose() {
            Messager.off(this.linkChangeKey);
            this.$el.off('click.shopCategory').off('linkChange.shopCategory').off('change.shopCategory').off('dblclick.shopCategory');
        }
    }
    return ShopCategory;
})($);
export default ShopCategory;