<template>
  <div class="aimodeltc-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="aimodel-dialog"
      draggable
    >
      <el-form ref="aimodeltcRef" :model="state.form" :rules="state.rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="aimodeltcname">
              <el-input v-model="state.form.aimodeltcname" placeholder="名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

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

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

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  // 表单
  form: {
    aimodeltcname: '',
    aimodeltcmodelname: '',
    aimodeltcdzprice: '',
    aimodeltcnumprice: '',
    aimodeltcstatus: '',
    aimodeltcmaxtoken: '',
    aimodeltcaikeyid: '',
    aimodeltciscontextnum: '',
    aimodeltciscontext: '',
    aimodeltctemperature: '',
    aimodeltcfirstmfwjsize: '',
    aimodeltcwjmaxsize: '',
    aimodeltcwztwmaxlen: '',
    aimodeltcwztwjl: '',
    aimodeltcwztwprice: '',
    aimodeltcwzccjl: '',
    aimodeltcwzccprice: '',
    aimodeltcwjtwjl: '',
    aimodeltcwjtwprice: '',
    aimodeltcwjccjl: '',
    aimodeltcwjccprice: '',
    aimodeltcisassistantsstatus: '',
    aimodeltcisassistantsid: ''
  },
  // 验证表单
  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) {
        state.form.aimodeltcid = state.editView.aimodeltcid
        await productManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await productManageStore.add(state.form)
        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.aimodeltcid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.aimodeltcname}】` : '新增'
}

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

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
