import {fetch} from '@core/component/httpClient';
import ShortPagination from '@core/component/shortPagination';
import update from 'immutability-helper';
import { Messager } from '@core/component/msgDistribute';
import domain from '@core/util/domain';
import {mainWapTpl, doctorDetailTpl} from './mainTpl';
import './style.scss';
class DoctorList{
    constructor(config){
        this.config = this.getConfig(config);
        this.$el = $(this.config.node);
        this.API = {
            queryDoctorDetail: domain .backend + '/mshop/edit/queryDoctorList.json'
        }
        if (this.config.linkObj && this.config.linkObj.originalData) {
            try {
                let originalData = JSON.parse(this.config.linkObj.originalData);
                this.doctorId = originalData.doctorId || '';
            } catch (error) {
                this.doctorId = '';
            }
        }
        this.render();
    }
    getConfig(config){
        return $.extend(true, {}, config);
    }
    render(){
        this.$el.html(template.render(mainWapTpl, {
            data: 1
        }));
        this.bindEvent();
        this.loadListData();
    }
    bindEvent(){
        const self = this;
        this.$el.on('click.doctorLink', '.J_search-btn', async function () {
            await self.loadListData({
                currentPageNo: 1
            }, true);
        });
        this.$el.on('click.doctorLink','tbody tr',function (e) {
            let $target = $(this);
            let doctorIds = parseInt($target.attr('data-doctorId'))
            
            $target.find('input').prop('checked', true);
            Messager.trigger('linkChange', {
                link: `//conecli.com/s/EvaluationManage/DoctorDetail?doctorId=${doctorIds}`,
                originalData: JSON.stringify({
                    doctorIds
                }),
                linkType: 20
            })
        });
        this.$el.on('click.doctorLink', '.J_cancel', function() {
            Messager.trigger('cancel');
        });
        this.$el.on('click.doctorLink', '.J_ok', function () {
            Messager.trigger('confirm');
        })
    }
    async loadListData(param = {currentPageNo: 1}){
        if (this.$el.find('.J_search-input').length > 0) {
            param.doctorIds = this.$el.find('.J_search-input').val().trim();
        }
        const data = await fetch({
            url: this.API.queryDoctorDetail,
            method: 'GET',
            data: param
        });
        if(data.code === 200 && data.data){
            const { result: doctorList } = data.data;
            const result = formartDoctorPrice(doctorList);
            delete data.data['result'];
            this.doctorData = {
                ...data.data,
                result
            }
            this.renderTable();
        }
        function formartDoctorPrice(doctorList){
            return doctorList.map((obj, index) => {
                let doctorServiceInfoList = obj.doctorServiceInfoList || obj.doctorServiceShowInfoList || [];
                doctorServiceInfoList = doctorServiceInfoList.map((val, i) => ({
                    ...val,
                    ['jdPrice']: val['jdPrice'].toFixed(2)
                }))
                return {
                    ...obj,
                    doctorServiceInfoList
                }
            })
        }
    }
    renderTable(){
        const self = this;
        let html = template.render(doctorDetailTpl, {
            ...this.doctorData,
            doctorId: this.doctorId
        })
        this.$el.find('.J_table tbody').html(html);
        let pageConfig = {
            current: this.doctorData.currentPage, 
            total: this.doctorData.pageCount,
            pageChange: function ({current}) {
                self.loadListData({currentPageNo: current})
            }
        }
        if (this.doctorData.pageCount > 1) {
            if (this.pageTable) {
                this.pageTable.dispose();
            }
            this.pageTable = new ShortPagination({
                node: this.$el.find('.J_min_page'),
                ...pageConfig
            })
        }
    }
    dispose() {
        this.$el.off('change.doctorLink');
        this.$el.off('click.doctorLink');
    }
}
export default DoctorList;