<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="iplibraryname">
              <el-input v-model="state.form.iplibraryname" placeholder="地址池名称" />
            </el-form-item>
          </el-col>
          <el-col v-if="!Object.keys(state.editView).length" :span="12">
            <el-form-item label="IP类型" prop="iplibrarylx">
              <el-radio-group v-model="state.form.iplibrarylx" :disabled="state.dialogType === 'up'" @change="onformType">
                <el-radio :value="'1'" :label="'1'">路由器</el-radio>
                <el-radio :value="'2'" :label="'2'">个人</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col v-if="state.form.iplibrarylx === '2'" :span="24">
            <el-form-item label="宽带iplibrarydk" prop="iplibrarydk">
              <el-input v-model="state.form.iplibrarydk" placeholder="宽带iplibrarydk" />
            </el-form-item>
          </el-col>
          <el-col v-if="Object.keys(state.editView).length" :span="12">
            <el-form-item label="ip地址" prop="iplibraryaddress">
              <el-input v-model="state.form.iplibraryaddress" placeholder="ip地址" />
            </el-form-item>
          </el-col>
          <el-col v-if="!Object.keys(state.editView).length" :span="12">
            <el-form-item label="开始ip" prop="iplibrarystartaddress">
              <el-input v-model="state.form.iplibrarystartaddress" placeholder="开始ip" />
            </el-form-item>
          </el-col>
          <el-col v-if="!Object.keys(state.editView).length" :span="12">
            <el-form-item label="结束ip" prop="iplibraryendaddress">
              <el-input v-model="state.form.iplibraryendaddress" placeholder="结束ip" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="iplibrarybz">
              <el-input v-model="state.form.iplibrarybz" placeholder="备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="iplibrarylabel">
              <el-input v-model="state.form.iplibrarylabel" 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: {
    iplibraryname: '',
    iplibrarybz: '',
    iplibrarylabel: '',
    iplibraryaddress: '',
    iplibrarystartaddress: '',
    iplibraryendaddress: '',
    iplibrarylx: '',
    iplibrarydk: ''
  },
  // 验证表单
  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['iplibraryid.value'] = state.editView.iplibraryid
        params['iplibraryid'] = state.editView.iplibraryid
        params.iplibraryendaddress = state.editView.iplibraryaddress
        params.iplibrarystartaddress = state.editView.iplibraryaddress
        params.pagevalue = 344
        await alleApi.up(params)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        // 单独接口，重新映射
        let paramsAdd = {
          name: params.iplibraryname,
          bz: params.iplibrarybz,
          label: params.iplibrarylabel,
          ipstart: params.iplibrarystartaddress,
          iplibrarylx: params.iplibrarylx,
          iplibrarydk: params.iplibrarydk,
          ipend: params.iplibraryendaddress
        }
        await generateip(paramsAdd)
        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 = {
      iplibraryname: '',
      iplibrarybz: '',
      iplibrarylabel: '',
      iplibrarystartaddress: '',
      iplibraryendaddress: '',
      iplibrarydk: ''
    }
    state.modalVisible = true
  }
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.iplibraryname}】` : '新增'
}

// 申请详情
const getDataView = (data) => {
  nextTick(async () => {
    let params = {
      'iplibraryid.value': data.iplibraryid,
      pagevalue: 341
    }
    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>
