<template>
    <Drawer ref="drawerRef" v-model="visible" title="新增" :onOk="submit" :colesAfter="refresh">
        <a-form-model layout="vertical" :model="form" :rules="rules" ref="DrawerForm">
            <a-form-model-item label="班组" prop="shiftType">
                <a-select v-model="form.shiftType" :disabled="isView">
                    <a-select-option key="DAY" value="DAY">白班</a-select-option>
                    <a-select-option key="MIDDLE" value="MIDDLE">中班</a-select-option>
                    <a-select-option key="NIGHT" value="NIGHT">晚班</a-select-option>
                </a-select>
            </a-form-model-item>
            <a-form-model-item label="生成第二天线路" prop="isNextDay">
                <a-select v-model="form.isNextDay" :disabled="isView">
                    <a-select-option key="YES" value="YES">是</a-select-option>
                    <a-select-option key="NO" value="NO">否</a-select-option>
                </a-select>
            </a-form-model-item>
            <a-form-model-item label="生成第二天线路" prop="scheduleRule">
                <Acron v-model="form.scheduleRule" :disabled="isView" @input="onChang" />
            </a-form-model-item>
            <a-form-model-item prop="scheduleDescription">
                <a-input v-model="form.scheduleDescription" :disabled="true" />
            </a-form-model-item>
        </a-form-model>
    </Drawer>
</template>

<script>
import cronstrue from 'cronstrue/i18n';
import Lang from '@/utils/langUtils';
import Drawer from '@/components/table/drawer.vue';
import FormMixin from '@/components/FormMixin';
import { addRouteSchedulesApi } from '@/api';
import Acron from '@/components/Acron/index';

const langType = {
    en: 'en',
    zh: 'zh_CN',
};
export default {
    components: { Drawer, Acron },
    props: {
        refresh: Function,
        row: Object,
    },
    mixins: [FormMixin],
    data() {
        return {
            visible: false,
            form: {},
            rules: {
                shiftType: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
                isNextDay: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
                scheduleRule: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
            },
        };
    },
    async mounted() {},
    methods: {
        open() {
            this.form = {};
            this.visible = true;
        },
        add() {
            const { routeId } = this.row;
            return addRouteSchedulesApi({ ...this.form, routeId });
        },
        onChang(val) {
            const str = cronstrue.toString(val, { locale: langType[Lang.get().substr(0, 2)] });
            this.form = { ...this.form, scheduleDescription: `${str} 执行一次` };
        },
    },
};
</script>
