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 HrmPayrollDetail{
	@observable loading = false;
	@observable notAuth = false;
	@observable data = [];
	@observable payrollDetail = {};
	@observable displayType = "";

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

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

	@computed get longTxtColIndexs(){
		const indexs = [];

		this.data.forEach(item => {
			item.forEach( (val,index) => {
				if (val.length > 15) {
					indexs.push(index)
				}
			})
		})

		return [...new Set(indexs )];
	}

	@computed get cols(){
		return this.data[0].length;
	}

	@computed get colDatas() {
		const arr = [];

		this.data.forEach((item, index) => {
			if (index === 0) {
			} else {
				item.forEach((val, index) => {
					if (!arr[index]) {
						arr.push([]);
					}
					arr[index].push(val)
				})
			}
		});

		return arr;
	}

	@computed get rowDatas(){
		const arr = [];

		for(let i =0;i < this.data.length; i++){
			arr.push([]);
		}

		this.data.forEach((item,oindex)=>{
			item.forEach((val,index)=>{
				if (index > 0) {
					arr[oindex].push(val);
				}
			} )
		});

		return arr;
	}


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

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

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

				data && Object.keys(data).forEach(key => {
					if (key === 'data') {
						this[key] = JSON.parse(data[key]);
					} else {
						this[key] = data[key];
					}
				})

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

			this.loading = false;
		})
	}

	get DialogButtons(){
		return [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@65463m@${'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);
			}
		})
	}


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

export const hrmPayrollDetailStore = new HrmPayrollDetail();