<template>
    <!-- 日期时间 -->
    <div>
        <basic-edit></basic-edit>
        <div>
            <div class="title">默认时间：</div>
            <div class="default">
                <el-select
                    :class="{ w100p: formItem.date.valueType === 2 }"
                    v-model="formItem.date.valueType"
                    @change="$event < 3 ? formItem.date.value = '' : ''"
                >
                    <el-option
                        v-for="(item, index) in typeList"
                        :key="index"
                        :label="item.name"
                        :value="item.value"
                    ></el-option>
                </el-select>
                <el-date-picker
                    placeholder="请选择日期"
                    :type="dateType"
                    :format="formItem.date.type"
                    :value-format="formItem.date.type"
                    v-model="formItem.date.value"
                    v-if="formItem.date.valueType === 1"
                    popper-class="__form-date-popper__"
                ></el-date-picker>
                <el-input
                    :value="formItem.date.value"
                    @input="formItem.date.value = posInt($event)"
                    v-if="formItem.date.valueType > 2"
                >
                    <template slot="append">天</template>
                </el-input>
            </div>
        </div>
        <type-edit
            title="日期类型"
            :list="dateList"
            v-model="formItem.date.type"
            @change="formItem.date.value = ''"
            @item-change="dateType = $event.type"
        ></type-edit>
        <verify-edit start-now></verify-edit>
    </div>
</template>

<script>
import { posInt } from '@/assets/utils/formatString.js'
export default {
    name: 'FormDate',
    components: {
        BasicEdit: () => import('@/common/formDesign/components/formEdit/components/BasicEdit'),
        TypeEdit: () => import('@/common/formDesign/components/formEdit/components/TypeEdit'),
        VerifyEdit: () => import('@/common/formDesign/components/formEdit/components/VerifyEdit')
    },
    data () {
        return {
            posInt,
            formItem: this.$store.state.form.formItem,
            typeList: [
                { name: '指定日期', value: 1 },
                { name: '填写当天', value: 2 },
                { name: '填写日前', value: 3 },
                { name: '填写日后', value: 4 }
            ],
            dateList: [
                { name: '年-月-日-时-分', value: 'yyyy/MM/dd HH:mm', type: 'datetime' },
                { name: '年-月-日-时', value: 'yyyy/MM/dd HH', type: 'datetime' },
                { name: '年-月-日', value: 'yyyy/MM/dd', type: 'date' },
                { name: '年-月', value: 'yyyy/MM', type: 'month' },
                { name: '年', value: 'yyyy', type: 'year' }
            ],
            dateType: 'datetime'
        }
    },
    created () {
        this.init()
    },
    methods: {
        // 初始化
        init () {
            if (!this.formItem.verify) {
                this.$set(this.formItem, 'date', {
                    value: '',
                    valueType: 1,
                    type: 'yyyy/MM/dd HH:mm'
                })
            } else {
                this.dateType = this.dateList.find(item => item.value === this.formItem.date.type).type
            }
        }
    }
}
</script>

<style lang="less">
.__form-date-popper__ {
    margin-left: -100px;
    .popper__arrow {
        margin-left: 100px;
    }
}
</style>
<style lang="less" scoped>
.default {
    display: flex;
    .el-select {
        width: 120px;
        margin-right: 18px;
        &.w100p {
            width: 100%;
            margin-right: 0;
        }
    }
    .el-input {
        width: 220px;
    }
}
.el-radio {
    display: block;
    margin-top: 10px;
}
.el-radio-group {
    width: 100%;
}
</style>
