<template>
  <div class="aimodeltc-edit"></div>
</template>

<script setup>
import paymentStoreApi from '@store/modules/paymentStoreApi'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

// 接口封装文件
const productManageStore = paymentStoreApi()
// 全局变量
const {proxy} = getCurrentInstance()
// 验证form
const aimodeltcRef = ref(null)

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  // 表单
  form: {
    productclassname: '',
    productclasssfky: '',
    productclasssort: ''
  },
  // 验证表单
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  }
})

const emits = defineEmits(['refresh'])

// 确定弹窗回调
const submitDialog = () => {
  aimodeltcRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        let params = JSON.parse(JSON.stringify(state.form))
        params['productclassid.value'] = state.editView.productclassid
        await productManageStore.up(params)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        let params = JSON.parse(JSON.stringify(state.form))
        await productManageStore.add(params)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      aimodeltcRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

// 关闭弹窗
const cancelDialog = () => {
  state.editView = {}
  aimodeltcRef.value.resetFields()
  state.modalVisible = false
}

// 打开弹窗
const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.productname}】` : '新增'
}

// 申请详情
const getDataView = (data) => {
  nextTick(async () => {
    const result = await productManageStore.view({'paysortid.value': data.paysortid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

defineExpose({
  openDialog
})
</script>
