<template>
    <div>
        <el-dialog
            :title="addDictDialog.title"
            :visible="true"
            width="600px"
            append-to-body
            :close-on-click-modal="false"
            @close="handleClose"
        >
        <el-scrollbar class="bor">
            <div class="dialog-content">
                
                <el-form ref="ruleForm" 
                        :model="queryParam" 
                        :rules="rulesClient" 
                        label-width="120px" 
                        class="demo-ruleForm">
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="24">
                            <el-form-item label="字典标签" prop="type">
                                <el-select v-model="queryParam.type"
                                           clearable placeholder="请选择"
                                           class="width100">
                                    <el-option
                                            v-for="item in operateMethodOptions"
                                            :key="item.code"
                                            :label="item.desc"
                                            :value="item.code">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="24">
                                <el-form-item label="字典名称" prop="codeName">
                                    <el-input
                                    v-model.trim="queryParam.codeName"
                                    placeholder="请输入字典名称"
                                    clearable maxlength="20"
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="24">
                                <el-form-item label="字典编码" prop="code">
                                    <el-input
                                    v-model.trim="queryParam.code"
                                    placeholder="请输入字典编码"
                                    clearable  maxlength="20"
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="24">
                            <el-form-item label="排序" prop="seq">
                              <el-input-number v-model="queryParam.seq" :min="1" :max="999" label="请输入" class="width100"></el-input-number>
                            </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="24">
                            <el-form-item label="备注" prop="remark">
                                <el-input
                                type="textarea"
                                :rows="3"
                                placeholder="请输入内容"
                                maxlength="60"
                                v-model="queryParam.remark">
                            </el-input>
                            </el-form-item>
                            </el-col>
                        </el-row>
                        
                </el-form>
            </div>
        </el-scrollbar>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose()">取消</el-button>
            <el-button type="primary" @click="handleSubmit()">确认</el-button>
        </span>
        </el-dialog>
        
    </div>
</template>
<script>
import api from "@/services/basicManage";
    export default {
        components: {
            uploadFile: () => import('../../../../components/uploadFile.vue'),//上传
        },
        props: {
            addDictDialog: {
                type: Object,
                default: () => {
                    return {}
                },
            },
        },
        data() {
            return {
                queryParam:{
                    type:'',
                    typeName:'',
                    code:'',
                    codeName:'',//名称
                    remark:'',
                    seq:225
                },
                rulesClient: {
                    type: [
                        { required: true, message: '请选择字典标签', trigger: 'change',},
                    ],
                    code: [
                        { required: true, message: '请输入字典编码', trigger: 'change',},
                    ],
                    codeName: [
                        { required: true, message: '请输入字典名称', trigger: 'change',},
                    ]
                   
                },
                operateMethodOptions:[{code:"financial",desc:"金融产品类型"}],
                
            };
        },
        mounted () {
            this.getDictTypeList()
            if(Object.keys(this.addDictDialog.data).length>0)
            {
                this.queryParam = JSON.parse(JSON.stringify(this.addDictDialog.data));
            }
            
        },
        methods: {
            getDictTypeList(){
                api.getDictTypeEnum({}).then(res => {
                    if (!res.code) {
                        this.operateMethodOptions = res;
                    }
                })
            },
            handleSubmit(){
                
                if(!this.$clickThrottle()) { return }
                let _this=this;
                 this.$refs.ruleForm.validate((valid) => {
                    if(valid) {

                        let typeitem=this.operateMethodOptions.find(item => {
                            return item.code === _this.queryParam.type
                        })
                        _this.queryParam.typeName=typeitem.desc;
                        if(Object.keys(this.addDictDialog.data).length>0)
                        {
                            api.updateDictById(_this.queryParam).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '编辑数据字典成功！',
                                    type: 'success'
                                });
                                this.handleClose()
                                this.$parent.getDictList(1)
                            })
                        }
                        else
                        {
                            api.addDict(_this.queryParam).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '新增数据字典成功！',
                                    type: 'success'
                                });
                                this.handleClose()
                                this.$parent.getDictList(1)
                            })
                        }
                        
                    }
                 })
            },
            handleClose() {
                this.$emit('update:addDictDialog', null)
            }
        }
    };
</script>
<style scoped>
.dialog-content{
    padding: 16px 107px 16px 72px;
    max-height:500px
}

</style>