<template>
    <!-- 单位类型 新增、编辑 表单组件 -->
    <div>
        <el-form
            label-width="120px"
            size="medium"
            ref="unitsTypeParamsRef"
            :model="unitsTypeParams"
            :rules="unitsTypeParamsRules"
        >
            <el-form-item label="单位类型名称" prop="scrapNum">
                <el-input v-model="unitsTypeParams.scrapNum" />
            </el-form-item>

            <el-form-item label="启用状态" prop="pro_type">
                <el-switch
                    active-text="启用"
                    inactive-text="禁用"
                    v-model="unitsTypeParams.pro_type"
                ></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSubmit">确定</el-button>
                <el-button
                    @click="
                        () => {
                            this.$emit('closeDialog')
                        }
                    "
                    >取消</el-button
                >
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: 'FormUnitsType',
    props: {
        editParams: {
            type: Object,
            default: () => null,
        },
    },
    mounted() {
        if (this.editParams) {
            Object.assign(this.unitsTypeParams, this.editParams)
        }
    },
    data() {
        return {
            unitsTypeParams: {
                scrapNum: '',
                pro_type: '',
            },
            // 校验规则
            unitsTypeParamsRules: {
                scrapNum: [
                    {
                        required: true,
                        message: '请输入单位类型名称',
                        trigger: 'blur',
                    },
                ],
                pro_type: [
                    {
                        required: true,
                        message: '请选择施工单位',
                        trigger: 'blur',
                    },
                ],
            },
        }
    },
    methods: {
        /** 确认按钮 */
        onSubmit() {
            this.$refs.unitsTypeParamsRef.validate((valid) => {
                if (valid) {
                    console.log('校验通过')
                    // 1. 表单校验通过后调后台 Api
                    // 2. 成功之后通知父组件关闭弹框
                    this.$emit('closeDialog')
                }
            })
        },
    },
}
</script>
