import { observable, action, computed } from "mobx"
import { WeaLocaleProvider } from 'ecCom'
import { i18n } from "../../public/i18n";

//编号流水规则
export default class NumberFlowRule {
    @observable numberCompositionStore = {}
    @observable dateChecked = '0'
    @observable dateOptionValue = 'year#'
    @observable orgChecked = '0'
    @observable orgOptionValue = 'sub#'
    @observable postChecked = '0'

    @computed get dateOptions() {
        const { dateRelatedFieldIds } = this.numberCompositionStore
        const idToName = {
            'year#': i18n.label.year(),
            'month#': i18n.label.month(),
            'date#': i18n.label.daytime(),
        }
        return dateRelatedFieldIds.map(id => ({ key: id, showname: idToName[id] }))
    }

    @computed get orgOptions() {
        const { orgRelatedFieldIds } = this.numberCompositionStore
         const idToName = {
            'sub#': i18n.label.subcompany(),
            'dept#': i18n.label.department(),
        }
        return orgRelatedFieldIds.map(id => ({ key: id, showname: idToName[id] }))
    }

    @computed get flowRuleSwitchDatas() {
        const datas = []
        const { hasDateRelatedField, hasOrgField ,hasPostField} = this.numberCompositionStore
        
        //编号组成中包含日期相关的字段
        if (hasDateRelatedField) {
            datas.push({
                key: 'date',
                label: i18n.label.dateFlowAlone(),
                helpfulTip: i18n.label.flowRuleSwitchHelpfultip(),
                checked: this.dateChecked,
                options: this.dateOptions,
                value: this.dateOptionValue,
                onCheck: this.handleCheck,
                onSelect: this.handleSelect
            })
        }
        if (hasOrgField) {
            datas.push({
                key: 'org',
                label: i18n.label.orgFlowAlone(),
                helpfulTip:(this.orgOptions.length > 1) ? i18n.label.orgFlowSwitchHelpfultip() : i18n.label['531833'](),
                checked: this.orgChecked,
                options: this.orgOptions,
                value: this.orgOptionValue,
                onCheck: this.handleCheck,
                onSelect: this.handleSelect
            })
        }
        if (hasPostField) {
            datas.push({
                key: 'post',
                label: i18n.label.postFlowAlone(),
                helpfulTip: i18n.label.postFlowSwitchHelpfultip(),
                checked: this.postChecked,
                options: [],
                value: '',
                onCheck: this.handleCheck,
                onSelect: this.handleSelect
            })
        }

        return datas
    }

    @action handleCheck = (val, key) => {
        switch (key) {
            case 'date':
                this.dateChecked = val
                this.dateOptionValue = this.dateOptionValue || 'year#'
                break
            case 'org':
                this.orgChecked = val
                this.orgOptionValue = this.orgOptionValue || 'sub#'
                break
            case 'post':
                this.postChecked = val
                break
        }
    }

    @action handleSelect = (val, key) => {
        switch (key) {
            case 'date':
                this.dateOptionValue = val
                break
            case 'org':
                this.orgOptionValue = val
                break
        }
    }

    @action rely = (store) => {
        this.numberCompositionStore = store
    }

    @action closeSwitch = () => {
        this.dateChecked = '0'
    }

    @action setDateChecked = (val) => {
        this.dateChecked = val
    }

    @action setDateOptionValue = (val) => {
        this.dateOptionValue = val
    }

    @action setOrgChecked = (val) => {
        this.orgChecked = val
    }

    @action setOrgOptionValue = (val) => {
        this.orgOptionValue = val
    }

    @action setPostChecked = (val) => {
        this.postChecked = val
    }

}