import {
	observable,
	action,
	computed
} from 'mobx';
import { WeaLocaleProvider} from 'ecCom';
import { Button, message} from 'antd';
import {WeaTableNew} from 'comsMobx';
import * as api from '../apis/suggestDetail';
const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export class SuggestDetailStore { 
	targetId = "";
	detailInfo = "";
	@observable suggestDetail = {};
	@observable selectedKey = "0";
	@observable tableStore = new TableStore();
	@observable isTable = false;
	@observable isHandle = false;
	@observable loading = true;

	@computed get btns(){
		const {suggesttype } = this.suggestDetail;

		const btns = [
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@44865l@1`} onClick={this.markAsRead}>{getLabel('518814','标记为已处理')}</Button>
		];

		if (suggesttype === "1") {
			btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fgfrfd@2`} type="primary" onClick={this.confirmAdd}>{getLabel('518813','确认添加') }</Button>);
		}else{
			btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@lqj8fo@3`} type="primary" onClick={this.confirmDel}>{getLabel('518815','确认删除') }</Button>);
		};

		return (!this.isTable && !this.isHandle) ? btns : [];
	}

	@computed get detailId(){
		const {randomFieldId, id} = this.suggestDetail;

		return randomFieldId || id;
	}

	@action getSuggestList = () => {
		const params ={
			cmd: "suggest",
			status:"0",
			targetId:this.targetId,
		};
		if (this.detailInfo) {
			Object.assign(params,{
				detailInfo:this.detailInfo,
			})
		}
		this.tableStore= new TableStore();
		this.loading = true;
		api.getSuggestList(params).then(res => {
			const { sessionkey,datas } = res;
			if (sessionkey) {
				this.tableStore.getDatas(sessionkey);
				this.isTable = true;
			}
			if (datas ) {
				if (datas[0]) {
					this.suggestDetail = datas[0];
					this.isTable = false;
				}else{
					this.isHandle = true;
				}
			}
			this.loading = false;
		})
	}

	@action markAsRead = () => {
		this.save({cmd:"doChange"})
	}

	@action confirmAdd = () => {
		this.save({cmd:"doAdd"})
	}

	@action confirmDel = () => {
		const params = {
			cmd:"doDel",
			id:this.detailId
		}
		api.del(params).then(res => {
			const {status} = res;

			if (status == 1) {
				message.success(res.message);
				this.isHandle = true;
			}else{
				message.warning(res.message || "error");
			}
		})
	}

	save = (datas = {}) => {
		const params = {
			id: this.detailId,
			...datas
		};
		api.save(params).then(res => {
			const {status} = res;

			if (status == 1) {
				message.success(res.message);
				this.isHandle = true;
			}else{
				message.warning(res.message || "error");
			}
		})
	}

	@action setSelectedKey = (key) => {
		this.selectedKey = key;
	}

	createDialog = () => {
		const { groupidspan } = this.suggestDetail;

		let url = `/spa/hrm/index_mobx.html#/main/hrm/operateGroup?groupId=${groupidspan}&groupType=2`;
		if (this.f_weaver_belongto_userid) {
			url = `${url}&f_weaver_belongto_userid=${this.f_weaver_belongto_userid}`
		}

		const dia = ecCom.WeaTools.createDialog({
			url,
			title: getLabel(126252, "常用组设置"),
			icon:'icon-coms-hrm',
			iconBgcolor:'#217346',
			style:{width:600,height:600},
			callback: (datas)=>{ // 数据通信
				//0表示当前保存的是基本信息，需要关闭弹框并刷新当前页面
				if (datas === 0) {
					dia.close();
					this.getSuggestList();
				}
			},
			onCancel: ()=>{ // 关闭通信
			}
		});
		dia.show();
	}

	setQuery = (query) => {
		Object.keys(query).forEach(key => {
			if (query[key]) {
				this[key] = query[key];
			}
		})
	}
}