import React, {
	Component
} from 'react';
import { observer } from 'mobx-react';
import {
    WeaDialog,
    WeaTab,
    WeaButtonIcon,
    WeaLocaleProvider
} from 'ecCom';
import {
	WeaTableNew
} from 'comsMobx';
import findIndex from 'lodash/findIndex';
const {
	WeaTable
} = WeaTableNew;

@observer
export default class TemplateEdit extends Component {
	constructor(props) {
        super(props);
    }

    doEdit = (id, record) => {
        this.props.store.editTemplate(record);
    }

    doSaveAs = (id) => {
        this.props.store.saveAs(id);
    }

    doDelete = (id) => {
        this.props.store.deleteTemplate({id});
    }

    onOperatesClick = (record, index, operate) => {
		const func = operate.href ? operate.href.split(':')[1].split('(')[0] : '';
		const id = record.id ? record.id : '';
		this[func] && this[func](id, record);
	}

	renderCol = (cols) => {
		let idx = findIndex(cols, item => item.dataIndex === 'name');
		idx != -1 && (cols[idx].render = (text, record) => {
			return <a onClick={() => this.doEdit(record.id, record)}>{WeaLocaleProvider.formatMultiLang(record.name)}</a>
		})
	}

	render() {
		const {
            store
        } = this.props;
        const {
            editDialogParams,
            multiDelete,
            templateStore,
            editTemplate,
            deleteTemplate,
            onTemplateSearchChange,
        } = store;
        const tableProps = {
			scroll: {
				y: 380
			}
		}

		return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@wa8k9l`}  {...editDialogParams} buttons={[]} moreBtn={{datas: []}}>
                <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@r3uolh`}
                    datas={[]}
                    keyParam="viewcondition"  //主键
                    searchType='base'
                    onSearch={value=>onTemplateSearchChange(value)}
                    buttons={[
                    <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@qra9zd@1`} buttonType='add' type="primary" onClick={() => editTemplate()}/>,
                    <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@da5fyb@2`} buttonType='del' type="primary" onClick={() => deleteTemplate({})} disabled={multiDelete}/>,
                    ]}
                />
                <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@lho4e1`} comsWeaTableStore={templateStore} 
                    {...tableProps} 
                    needScroll={true}
                    hasOrder
                    onOperatesClick={this.onOperatesClick}
                    getColumns={this.renderCol}
                />
            </WeaDialog>
        )
	}
}