<template>
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="400px"
      @close="closeDialog(0)"
       @open="open"
    >
      <el-form
        ref="formInfo"
        :model="form"
        class="demo-form-inline"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="分组名称：" prop="name">
          <el-input v-model="form.name"  ></el-input>
        </el-form-item>
      
        <div style="text-align: right">
          <el-button type="primary" @click="submitForm('formInfo')"
            >保存</el-button
          >
        </div>
      </el-form>
    </el-dialog>
</template>
<script>
import { defineComponent, ref, watchEffect } from 'vue'

export default defineComponent({
  name: 'AddGroupCom',
  emits: ['closeDialog'],
  props: {
    flag: Boolean,
    dialogTitle: String,
    itemInfo: Object,
  },
  setup(props, context) {
    context

    const dialogVisible = ref(false)
    const formInfo = ref(null)

    const form = ref([])

    
    // open(): Dialog弹窗打开之前做的事
    const open = () => {
      form.value = props.itemInfo.value

    }

    const options = [
      {
        value: '选项1',
        label: '站点一',
      },
      {
        value: '选项2',
        label: '站点二',
      },
      {
        value: '选项3',
        label: '站点三',
      },
      {
        value: '选项4',
        label: '站点四',
      },
      {
        value: '选项5',
        label: '站点五',
      },
    ]

    // 关闭弹框
    const closeDialog = () => {
      context.emit('closeDialog', false)
      dialogVisible.value = false
    }

    watchEffect((fn, options) => {
      fn, options
      dialogVisible.value = props.flag
    })

    const roleValid = (rule, value, callback) => {
      rule
      if (value.length === 0) {
        callback(new Error('角色不能为空'))
      } else {
        callback()
      }
    }

    // 保存-修改操作
    const submitForm = () => {
      console.log(formInfo)
      formInfo.value.validate((valid) => {
        if (valid) {
          // 走保存请求
          this.$message({
            message: '操作成功！',
            type: 'success',
          })
          this.closeDialog(1)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }

    return {
      closeDialog,
      dialogVisible,
      options,
      roleValid,
      formInfo,
      form,
      open,
      submitForm,
      rules: {
        name: [
          // required  是否为必填项， trigger:表单验证的触发时机，失去焦点进行验证
          { required: true, message: "请输入分组名称", trigger: "blur" },
          {
            min: 3,
            max: 6,
            message: "用户名长度在 3 到 6 个字符",
            trigger: "blur",
          },
        ],
      },
    }
  },

})
</script>
 
<style scoped lang="scss">


</style>