import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {message,Button} from 'antd';
import {isEmpty} from "lodash";
import {DialogStore} from '../public/valhalla/stores/index.js';
import FinanceExpand from './financeExpand.js';
import * as api from '../apis/payrollDetail.js';
const getLabel = WeaLocaleProvider.getLabel;

class HrmPayrollDetailBp{
	@observable loading = false;
	@observable notAuth = false;
	@observable firstCol = [];
	@observable otherCols = [];
	@observable data = [];
	@observable payrollDetail = {};

	@observable financeExpand = new FinanceExpand(this.init);

	@computed get hasPayrollDetail(){
		return !isEmpty(this.payrollDetail)
	}

	init = () => {
		this.getPayrollDetail();
	}

	getPayrollDetail = () => {
		this.loading = true;

		api.readRecord(this.params).then(datas => {
			const {data,status,displayType} = datas;
			this.notAuth = (status == '0');
			if (status ==='1') {
				this.payrollDetail = data;
				this.displayType  = displayType;

				data && Object.keys(data).forEach(key => {
					this[key] = data[key];
				})

				this.splitData()
			}else{
				datas.message && message.warning(datas.message);
			}

			this.loading = false;
		})
	}

	splitData = () => {
		let arr = [];
		this.data && !isEmpty(this.data) && JSON.parse(this.data).forEach(item => {
			const datas = item.slice(1);
			//纵向
			if (this.displayType == '1' && datas.some(data => data)) {
				this.firstCol.push(item[0]);
				datas.map((val,index) => {
					if ( (this.otherCols.length -1) < index ) {
						this.otherCols.push([val]);
					}else{
						this.otherCols[index].push(val);
					}
				})
			}
			//横向
			if (this.displayType == '0') {
				item.map((val,index) => {
					if ( (arr.length -1) < index ) {
						arr.push([val]);
					}else{
						arr[index].push(val);
					}
				})
				this.firstCol = arr[0];
				arr.slice(1).map(data => {
					if(data.slice(1).some(d => d)){
						this.otherCols.push(data);
					}
				})
			}
		})
	}

	setTargetId = (params) => {
		this.params = params;
	}

	get DialogButtons(){
		return [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rg333o@${'save'}`} type="primary" onClick={this.saveFeedback}>{getLabel(-1,"保存")}</Button>]
	} 

	@observable fbDialogStore = new DialogStore(this);

	confirmRecord = () => {
		api.confirmRecord({targetId: this.params.targetId}).then(res => {
			if (res.status == "1") {
				message.success(getLabel('524279','确定成功'));
				this.getPayrollDetail();
			}else{
				message.error(res.message);
			}
		})
	}

	feedback = () => {
	    const config = {
	        title: getLabel('524278','反馈信息'),
	        style: { height: 200 }
	    }
	    this.remark = "";
	    this.fbDialogStore.init(config);
	    this.fbDialogStore.openDialog();
	}

	handleTextareaChange = (text) => {
		this.remark = text;
	}

	saveFeedback = () => {
		api.feedback({targetId: this.params.targetId, remark: this.remark}).then(res => {
			if (res.status == "1") {
				message.success(getLabel('524280','信息已反馈'));
				this.getPayrollDetail();
				this.fbDialogStore.closeDialog();
			}else{
				message.error(res.message);
			}
		})
	}

}

export const hrmPayrollDetailBpStore = new HrmPayrollDetailBp();