<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="radius产品名称" prop="radiuscpname">
              <el-input v-model="state.form.radiuscpname" placeholder="radius产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="带宽" prop="radiuscpdkband">
              <el-input v-model="state.form.radiuscpdkband" placeholder="带宽" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下载最大速度" prop="radiuscpxxmax">
              <el-input v-model="state.form.radiuscpxxmax" placeholder="下载最大速度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上传最大速度" prop="radiuscpsxmax">
              <el-input v-model="state.form.radiuscpsxmax" 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 allStoreApi from '@store/modules/allStoreApi'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'
import UploadFile from '@/components/UploadFile/index.vue'
import {generateip} from '@/api/app'

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

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  // 表单
  form: {
    radiuscpname: '',
    radiuscpxxmax: '',
    radiuscpsxmax: '',
    radiuscpdkband: ''
  },
  // 验证表单
  rules: {
    productradiusxxpj: [{required: true, message: '此项不能为空', trigger: 'blur'}],
    productradiusxxmax: [{required: true, message: '此项不能为空', trigger: 'blur'}],
    productradiussxpj: [{required: true, message: '此项不能为空', trigger: 'blur'}],
    productradiussxmax: [{required: true, message: '此项不能为空', trigger: 'blur'}]
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
  textTypeData: []
})

const emits = defineEmits(['refresh'])

// 确定弹窗回调
const submitDialog = () => {
  aimodeltcRef.value.validate(async (valid) => {
    if (valid) {
      let params = JSON.parse(JSON.stringify(state.form))
      // 修改配置
      if (Object.keys(state.editView).length) {
        params['radiuscpid.value'] = state.editView.radiuscpid
        params.pagevalue = 352
        await alleApi.up(params)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        // 单独接口，重新映射
        params.pagevalue = 353
        await alleApi.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 = {}) => {
  // 选择信息
  await getDataType()
  if (Object.keys(data).length) {
    await getDataView(data)
  } else {
    state.form = {
      radiuscpname: '',
      radiuscpxxmax: '',
      radiuscpsxmax: '',
      radiuscpdkband: ''
    }
    state.modalVisible = true
  }
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.radiuscpname}】` : '新增'
}

// 申请详情
const getDataView = (data) => {
  nextTick(async () => {
    let params = {
      'radiuscpid.value': data.radiuscpid,
      pagevalue: 350
    }
    const result = await alleApi.view(params)
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
    state.modalVisible = true
  })
}

// 拉取产品详情
import productStoreApi from '@store/modules/productStoreApi'
const productManageStore = productStoreApi()
const getDataType = () => {
  nextTick(async () => {
    const {Total, Data} = await productManageStore.list({pageindex: 1, rows: 99})
    state.textTypeData = Data
  })
}

defineExpose({
  openDialog
})
</script>
