/* ******************* 丰田纺织流程代码块 可选择流程中的字段配置流程标题生成规则******************* */

class ConfigWorkflowTitle {

    constructor(config) {
        this.config = config
    }


    /**
     * 初始化参数
     */
    init = () => {
        let baseInfo = WfForm.getBaseInfo();
        if (baseInfo.workflowid != this.config.workflowId) {
            return
        }
        let filedArr = []
        this.config.rules.filter(item => item.type === RulesType.FIELD_VALUE
            || item.type === RulesType.SELECT_VALUE
            || item.type === RulesType.RADIO_VALUE).forEach(item => filedArr.push(item.fieldName))
        this.addListenerEvent(filedArr)
        this.addListenerSubmitEvent()
    }


    /**
     * 监听提交流程动作
     */
    addListenerSubmitEvent = () => {
        Utils.registerAction(WfForm.OPER_SAVE + "," + WfForm.OPER_SUBMIT, callback => {
            this.changeWorkflowTitle()
            callback()
        })
    }

    /**
     * 修改流程标题字段值
     */
    changeWorkflowTitle = () => {
        let workflowTitle = []
        this.config.rules.forEach(item => {
            workflowTitle.push(item.type.run(item))
        })
        WfForm.changeFieldValue(this.config.titleFieldName === 'field-1' ? 'field-1' :
            Utils.convertNameToIdUtil(this.config.titleFieldName), {
            value: workflowTitle.join("")
        })
    }

    /**
     * 添加监听方法
     * @param fileNameArr 需要监听的字段数组
     */
    addListenerEvent = (fileNameArr) => {
        console.log(Utils.convertNameToIdUtil(fileNameArr))
        WfForm.bindFieldChangeEvent(Utils.convertNameToIdUtil(fileNameArr), (obj, id, value) => {
            this.changeWorkflowTitle()
        })
    }

}

class RulesType {
    // 固定值
    static FIX_STRING = {
        value: 0,
        run: item => item.value
    }
    // 字段值
    static FIELD_VALUE = {
        value: 1,
        run: item => Utils.getFiledValueByName(item.fieldName)
    }
    // 下拉框显示值
    static SELECT_VALUE = {
        value: 2,
        run: item => $(`div[data-fieldname='${item.fieldName}'] .ant-select-selection-selected-value`).text()
    }
    // 单选按钮
    static RADIO_VALUE = {
        value: 3,
        run: item => $(`div[data-fieldname='${item.fieldName}'] .ant-radio.ant-radio.ant-radio-checked.ant-radio-checked`).next().text()
    }
    // 当前日期 yyyy-mm-dd
    static CURRENT_DATE = {
        value: 4,
        run: () => {
            const date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDay();
            if (month < 10) {
                month = '0' + month
            }
            if (day < 10) {
                day = '0' + day
            }
            return year + "-" + month + "-" + day
        }
    }
    // 当前时间 HH:mm:ss
    static CURRENT_TIME = {
        value: 5,
        run: () => {
            const date = new Date();
            let hours = date.getHours();
            let minutes = date.getMinutes();
            let seconds = date.getSeconds();
            return hours + ":" + minutes + ":" + seconds
        }
    }
    // 当前年份
    static CURRENT_YEAR = {
        value: 6,
        run: () => new Date().getFullYear()

    }
    // 当前月份
    static CURRENT_MONTH = {
        value: 7,
        run: () => new Date().getMonth() + 1 < 10 ? new Date().getMonth() + 1 : '0' + new Date().getMonth() + 1

    }
    // 当前天数
    static CURRENT_DAY = {
        value: 8,
        run: () => new Date().getDay() < 10 ? new Date().getDay() : '0' + new Date().getDay()

    }
    // 当前小时
    static CURRENT_HOUR = {
        value: 9,
        run: () => new Date().getHours()

    }
    // 当前分钟
    static CURRENT_MINUTE = {
        value: 10,
        run: () => new Date().getMinutes()

    }
    // 当前秒数
    static CURRENT_SECOND = {
        value: 11,
        run: () => new Date().getSeconds()
    }
    // 当前时间戳
    static CURRENT_TIME_STAMP = {
        value: 12,
        run: () => new Date().getTime()
    }
    // 流水号
    static RANDOM = {
        value: 13,
        run: item => {
            let result = []
            let range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
            for (let i = 0; i < item.length; i++) {
                let index = parseInt(Math.random(0, 10) * 10)
                result.push(range[index])
            }
            return result.join("")
        }
    }
    static BROWSER_SHOW_NAME = {
        value: 14,
        run: item => {
            let fieldId = Utils.convertNameObjToId(item.fieldName)
            return WfForm.getBrowserShowName(fieldId)
        }
    }
}


window.RulesType = RulesType
window.ConfigWorkflowTitle = ConfigWorkflowTitle

/* ******************* 可选择流程中的字段配置流程标题生成规则 end ******************* */