import { observable, action, computed } from "mobx";
import { WeaLocaleProvider } from "ecCom";
import { toJS } from "mobx";
import { message } from "antd";
import * as api from "../apis/sendBless.js";
import { i18n } from "../public/i18n.js";
const getLabel = WeaLocaleProvider.getLabel;

export class HrmSendBlessStore {
    prefixUrl = "/hrm/hrm_e9/image/birth/";
    greetingCardUrlSuffixs = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
    greetingCardUrl = this.greetingCardUrlSuffixs.map(suffix => `${this.prefixUrl}${suffix}.png`);

    @observable startIndex = 0;
    @observable tagGroupDatas = [];
    @observable blessing = "";
    @observable browserConditionParam = {};
    @observable checkedImg = "";
    @observable birthTargets = [];

    get defaultBless() {
        return i18n.label['528863']()
    }

    @computed get greetingCardShow() {
        const index = this.startIndex;

        return this.greetingCardUrl.slice(index * 3, (index + 1) * 3).map(url => (this.checkedImg == url) ? ({ url, isChecked: true }) : ({ url, isChecked: false }));
    }

    @computed get canPrev() {
        return this.startIndex > 0;
    }

    @computed get canNext() {
        const gclen = this.greetingCardUrlSuffixs.length;
        return this.startIndex < Math.ceil(gclen / 3) - 1;
    }

    @computed get blessOptions() {
        return this.birthTargets.filter(target => target.id != this.targetId);
    }

    @computed get birthIds() {
        return this.birthTargets.map(data => data.id);
    }

    @action prev = () => {
        this.canPrev && this.startIndex--;
    }

    @action next = () => {
        this.canNext && this.startIndex++;
    }

    @action getBlessingForm = () => {
        const params = { targetId: this.targetId, createDate: this.createDate, fromModule:this.fromModule,recordId:this.recordId};
        api.getBlessingForm(params).then(res => {
            ["blessing", "browserConditionParam"].forEach(v => {
                if (res[v]) {
                    this[v] = res[v];
                }
            })
            if (window.e9_locale.userLanguage != '7') {
                this.blessing = i18n.label['529463']()
            }
            const { browserConditionParam } = res;
            if (browserConditionParam) {
                this.tagGroupDatas = browserConditionParam.replaceDatas
            }
        })
    }

    sendBless = () => {
        const validDatas = this.tagGroupDatas.filter(unhandledData => this.birthTargets.find(validData => unhandledData.id == validData.id));

        if (!validDatas.length) {
            message.warning(i18n.label['528864']());
            return;
        }

        const params = {
            imgUrl: this.checkedImg,
            target: validDatas.map(data => data.id).join(","),
            blessing: this.blessing ? this.blessing : this.defaultBless
        };

        api.saveBlessingForm(params).then(res => {
            if (res.status == "1") {
                var dialog = top.window.getParentDialog();
                if (dialog) {
                    dialog.close();
                    dialog.callback({ msg: i18n.label['528865']().replace("{param}", validDatas.length) })
                }
            } else {
                message.error(res.message);
            }
        })
    }


    @action handleImgClick = (srcName) => {
        this.checkedImg = srcName
    }

    @action handleTagGroupChange = (datas) => {
        this.tagGroupDatas = datas;
    }

    @action addBlessTarget = (data) => {
        if (!this.tagGroupDatas.find(oldData => oldData.id == data.id)) {
            this.tagGroupDatas.push(data);
        }
    }

    @action checkAll = () => {
        this.tagGroupDatas = this.birthTargets;
    }

    @action setBirthTargets = (datas) => {
        this.birthTargets = JSON.parse(datas);
    }

    setQueryDatas = (query) => {
        const { datas, targetId, createDate,fromModule,recordId } = query;

        this.targetId = targetId;
        this.birthTargets = datas && JSON.parse(datas);
        this.createDate = createDate;
        this.fromModule = fromModule || 'mobile';
        this.recordId = recordId
    }

    handleTextareaChange = (val) => {
        this.blessing = val;
    }

    dovalidate = (data) => {
        return !this.birthIds.find(id => data.id == id);
    }



}