<template>
    <!-- 基础信息弹出框 -->
  <el-dialog :model-value="dialogVisible" :title="title" v-bind="dialogSmall" @close="handleCancel" width="500" draggable>
    <div>
        <el-form :model="data" 
                  ref='formRuleRef'
                  :rules='rules'
                  >
        <el-form-item label="名称" prop='name' :label-width="100">
          <el-input v-model="data.name" placeholder="请输入名称" autocomplete="off" />
        </el-form-item>
        <el-form-item label="编码" prop='code' :label-width="100">
          <el-input v-model="data.code" placeholder="请输入编码" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" prop='type' :label-width="100">
          <el-select v-model="data.type" placeholder="请选择类型" autocomplete="off">
          <el-option
            v-for="item in dictStore.getDictData('roleType')"
            :key="item.id"
            :label="item.label"
            :value="item.code"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop='remark' :label-width="100">
          <el-input v-model="data.remark" :rows="2" type="textarea" placeholder="请输入备注" autocomplete="off" />
        </el-form-item>
      </el-form>
      </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOK">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>

  </template>
  
  <script lang="ts" setup>
  import { FormInstance,FormItemRule } from 'element-plus'
  import { ProTableInstance } from '@/components/ProTable/interface'
  import { ref,computed  } from 'vue'
  import { Role } from '@/api/role/interface'
  import {dialogSmall} from '@/utils/elementBind'
  import { useDictStore } from '@/stores/modules/dict'
  import {
    addRole,
    editRole,
  } from '@/api/role/role'

  const dictStore = useDictStore()
  const {dialogVisible,title,data,openType,getTableList}  = defineProps<{
    dialogVisible:boolean;
    title:string;
    openType:string;
    getTableList:Function;
    data:{
      id?: string;     //角色类型ID
      name: string;   //角色类型名称
      code: string;   //角色代码
      type: number;
      remark:string;   //角色描述
    }
  }>();

  //定义规则
  const formRuleRef = ref<FormInstance>()
  //定义校验规则
  const rules: Ref<Record<string, Array<FormItemRule>>> = ref({
    name: [{ required: true, message: '名称必填！', trigger: 'blur' }],
    code: [{ required: true, message: '编码必填！', trigger: 'blur' }],
  })

  const emit = defineEmits<{
  (e:'update:visible',value:boolean):void;
  }>();

  const handleCancel = () => {
    emit('update:visible',false)
  }
  
  const handleOK = () => {
    ElMessage.info('角色数据提交')
    try {
      formRuleRef.value?.validate((valid: boolean) => {
      if (valid) {
        // 将表单数据转为json,发送到后端
        // let confirmFormData = JSON.parse(JSON.stringify(form.value))
        // console.log(confirmFormData)
        if(openType === "add")
        {
          addRole(data).then(res => {
            if(res.code === "200")
            {
              ElMessage.success(res.message)
              getTableList()
            }
            else
              ElMessage.error(res.message)
          })
        }
          
        if(openType === "edit")
        {
          editRole(data).then(res => {
            if(res.code === "200")
            {
              ElMessage.success(res.message)
              getTableList()
            }
            else
              ElMessage.error(res.message)
          })
        }
        
        emit('update:visible',false)
      } else {
        ElMessage.error('表单验证失败！')
      }
    })
  } catch (error) {
    console.error('验证过程中发生错误', error)
  }
  }
  
  </script>