<template>
    <!-- 基础信息弹出框 -->
  <el-dialog :model-value="dialogVisible" :title="title" v-bind="dialogSmall" @close="handleCancel" width="500" draggable>
    <div>
        <el-form :model="data">
        <el-form-item label="名称" :label-width="100">
          <el-input v-model="data.name" placeholder="请输入名称" autocomplete="off" />
        </el-form-item>
        <el-form-item label="编码" :label-width="100">
          <el-input v-model="data.code" placeholder="请输入编码" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" :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.id"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" :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 { 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,updataList}  = defineProps<{
    dialogVisible:boolean;
    title:string;
    openType:string;
    data:{
      id?: string;     //角色类型ID
      name: string;   //角色类型名称
      code: string;   //角色代码
      type: number;
      remark:string;   //角色描述
    }
    updataList:Function
  }>();

  // const value = computed({
  //   // get: () => { 
  //   //   if(title === '新增角色')
  //   //     return true
  //   //   data.status === 1
  //   // },
  //   // set: (value: boolean) => data.status = value ? 1 : 0
  // })
  
  const emit = defineEmits<{
  (e:'update:visible',value:boolean):void;
  }>();

  const handleCancel = () => {
    emit('update:visible',false)
  }
  
  const handleOK = () => {
    
  // const addData:Role.RoleBO = {
  //   name: '', //角色类型名称
  //   code: '',   //角色代码
  //   type: 0,
  //   remark:'',   //角色描述
  // }

  // addData.name = data.name
  // addData.code = data.code
    console.log(data);
    if(openType === "add")
  {
    addRole(data).then(res => {
      if(res.code === "200")
      {
        ElMessage.success(res.message)
        updataList()
      }
      else
        ElMessage.error(res.message)
    })
  }
    
  if(openType === "edit")
  {
    editRole(data).then(res => {
      if(res.code === "200")
      {
        ElMessage.success(res.message)
        updataList()
      }
      else
        ElMessage.error(res.message)
    })
  }
    emit('update:visible',false)
  }
  
  </script>