<template>
  <a-modal 
    v-model:visible="data.isShow"
    :title=" data.isAdd ? '新增菜单' : '修改菜单' "
    :confirm-loading="data.confirmLoading"
    @ok="handleOkFunc"
    @close="handleCloseFunc"
  >
    <a-form ref="infoFormModel" :model="data.saveObject" :label-col="{span: 6}" :wrapper-col="{span: 15}" :rules="data.rules">
      <a-form-item label="资源名称：" name="entName">
        <a-input v-model:value="data.saveObject['entName']" />
      </a-form-item>
      <a-form-item label="路径地址：" name="menuUri">
        <a-input v-model:value="data.saveObject['menuUri']" />
      </a-form-item>
      <a-form-item label="平台分类" name="sys_type">
        <a-select show-search  :value="data.saveObject['sys_type']" placeholder="选择小程序平台"    >
          <a-select-option v-for="(value,index) in data.platformList" :key="index" :value="value.key">
            {{ value.name }}
          </a-select-option>
        </a-select>
      </a-form-item>

      
      <a-form-item label="页面组菜单：" name="componentName">
        <a-input v-model:value="data.saveObject['componentName']" />
      </a-form-item>

      <a-form-item label="排序（正序显示）：" name="entSort">
        <a-input v-model:value="data.saveObject['entSort']" />
      </a-form-item>

      <a-form-item label="快速开始：" name="quickJump">
        <a-radio-group v-model:value="data.saveObject['quickJump']" :disabled="data.saveObject['menuType'] == 'PB' || !data.saveObject['menuUri']">
          <a-radio :value="1">
            是
          </a-radio>
          <a-radio :value="0">
            否
          </a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="状态：" name="state">
        <a-radio-group v-model:value="data.saveObject['state']">
          <a-radio :value="1">
            启用
          </a-radio>
          <a-radio :value="0">
            停用
          </a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { API_URL_ENT_LIST, req, $getEntBySysType } from '@/api/manage'
import {ref, reactive} from 'vue'
import {message} from 'ant-design-vue'
 const props= defineProps({
    callbackFunc: { type: Function,default:null }
  })
    const infoFormModel = ref()
    const data = reactive ({
      platformList:[{name:'商家',key:"MCH"},{name:'服务商',key:"AGENT"},{name:'运营',key:"PLATFORM"}],
      confirmLoading: false, // 显示确定按钮loading图标
      isAdd: true, // 新增 or 修改页面标识
      isShow: false, // 是否显示弹层/抽屉
      saveObject: {}, // 数据对象
      recordId: null, // 更新对象ID
      sysType: 'PLATFORM', // 菜单类型
      rules: {
        entName: [
          { required: true, message: '请输入资源名称', trigger: 'blur' }
        ]
      }
    })

 defineExpose({show})

   function show (recordId, sysType='PLATFORM') { // 弹层打开事件
   
      data.isAdd = !recordId
      data.sysType = sysType
      // data.saveObject = {} // 数据清空
      data.confirmLoading = false // 关闭loading

      if (infoFormModel.value !== undefined) {
        infoFormModel.value.resetFields()
      }

      if (!data.isAdd) { // 修改信息 延迟展示弹层
        data.recordId = recordId
        $getEntBySysType(recordId, sysType).then(res => { data.saveObject = res })
        data.isShow = true

      } else {
        data.isShow = true // 立马展示弹层信息
      }
    }

   function handleOkFunc (){ // 点击【确认】按钮事件
        infoFormModel.value.validate().then(valid =>{
            data.confirmLoading = true // 显示loading
            if (!data.isAdd) {
              req.updateById(API_URL_ENT_LIST, data.recordId, data.saveObject).then(res => {
                message.success('修改成功')
                data.isShow = false
                props.callbackFunc() // 刷新列表
              }).catch(res => { data.confirmLoading = false })
            }
        })
    }

    //关闭
    function handleCloseFunc(){
      data.isShow = false
      props.callbackFunc() // 刷新列表
    }

</script>
