<template>
    <el-dialog :title="'分配标签'" :close-on-click-modal="false" :close-on-press-escape="false"
        :visible.sync="visible" lock-scroll class="JNPF-dialog JNPF-dialog_center" width="600px">
        <el-form ref="dataForm" v-loading="formLoading" :model="dataForm" :rules="dataRule"
            label-width="90px">
            <el-form-item label="标签" prop="type">
                <el-select v-model="dataForm.type" placeholder="请选择标签" filterable>
                    <el-option v-for="item in typeOptions" :key="item.enCode" :label="item.fullName"
                        :value="item.enCode">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
            <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
                {{$t('common.confirmButton')}}</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { createPosition, updatePosition, getPositionInfo } from '@/api/permission/position'

export default {
    data() {
        return {
            visible: false,
            formLoading: false,
            btnLoading: false,
            typeOptions: [],
            dataForm: {
                type: '',
            },
            organizeIdTree: [],
            dataRule: {
                type: [
                    { required: true, message: '岗位类型不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        init(row) {
            this.visible = true
            this.organizeIdTree = []
            this.formLoading = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                // 获取岗位类型
                this.$store.dispatch('base/getDictionaryData', { sort: 'PositionType' }).then(res => {
                    this.typeOptions = res
                })

                this.formLoading = false
            })
        },
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.btnLoading = true
                    const formMethod = this.dataForm.id ? updatePosition : createPosition
                    formMethod(this.dataForm).then(res => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refreshDataList')
                            }
                        })
                    }).catch(() => {
                        this.btnLoading = false
                    })
                }
            })
        }
    }
}
</script>
