<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="24">
            <el-form-item label="商品图片" prop="productpic">
              <UploadFile v-model:fileUrl="state.form.productpic" multiple accept="image/*" :oldUrl="state.form.productpic" :limit="1" attmodel="productpic" ref="sfzUploadRef" />
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <!-- 代理商归属 -->
            <all-Select :multiple="false" @refreshButton="refreshData" :activeId="state.form.routerboxqyid" />
          </el-col>
          <el-col :span="12">
            <el-form-item label="路由器mac码" prop="routerboxmac">
              <el-input v-model="state.form.routerboxmac" placeholder="路由器mac码" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="路由器sn码" prop="routerboxsn">
              <el-input v-model="state.form.routerboxsn" placeholder="路由器sn码" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="路由器关联产品" prop="routerboxproductid">
              <el-select v-model="state.form.routerboxproductid" clearable>
                <el-option v-for="(item, index) of state.textTypeData" :key="index" :label="item.productname" :value="item.productid">{{ item.productname }}</el-option>
              </el-select>
            </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 lyqStoreApi from '@store/modules/lyqStoreApi'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'
import UploadFile from '@/components/UploadFile/index.vue'

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

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  luyData: {},
  // 表单
  form: {
    routerboxsn: '',
    routerboxproductid: '',
    routerboxmac: '',
    routerboxqym: '',
    routerboxqyid: ''
  },
  // 验证表单
  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 = () => {
  // console.log(state.form, ';state.form')
  aimodeltcRef.value.validate(async (valid) => {
    if (valid) {
      // 首选获取套餐信息
      const newLuyouqi = state.textTypeData.find((textType) => textType.productid == state.form.routerboxproductid)
      let params = JSON.parse(JSON.stringify(state.form))
      params.routerboxproductname = newLuyouqi?.productname
      params.routerboxproductid = newLuyouqi?.productid
      params.routerboxproductlimittype = newLuyouqi?.productlimittype
      params.routerboxproductlimit = newLuyouqi?.productlimit
      params.routerboxproductzslimit = newLuyouqi?.productlimit
      params.routerboxproductprice = newLuyouqi?.productprice
      params.routerboxproductprice1 = newLuyouqi?.productprice1
      params.routerboxradiusvalue = newLuyouqi?.productradiusvalue
      params.routerboxradiusxxpj = newLuyouqi?.productradiusxxpj
      // 下
      params.routerboxradiusxxmax = newLuyouqi?.radiuscpxxmax
      // 上
      params.routerboxradiussxmax = newLuyouqi?.radiuscpsxmax

      params.routerboxradiussxpj = newLuyouqi?.productradiussxpj

      // radiuscpxxmax
      // radiuscpsxmax
      // 修改配置
      if (state.editView.routerboxid) {
        params['routerboxid.value'] = state.editView.routerboxid
        params['routerboxid'] = state.editView.routerboxid
        await lyqStore.up(params)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await lyqStore.add(params)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      aimodeltcRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}
const refreshData = async (data) => {
  state.form.routerboxqyid = data.map((value) => value.agentsid).join(',')
  state.form.routerboxqym = data.map((value) => value.agentsqym).join(',')
  state.editView.routerboxqyid = state.form.routerboxqyid
  state.editView.routerboxqym = state.form.routerboxqym
  // 选择信息
  await getDataType()
}
// 关闭弹窗
const cancelDialog = () => {
  state.editView = {}
  aimodeltcRef.value.resetFields()
  state.modalVisible = false
}

// 打开弹窗
const openDialog = async (data = {}) => {
  if (Object.keys(data).length) {
    await getDataView(data)
    // console.log(state.form.productpic, 'state.form.productpic')
  } else {
    state.form = {
      routerboxsn: '',
      routerboxproductid: '',
      routerboxmac: '',
      routerboxqym: '',
      routerboxqyid: ''
    }
    state.modalVisible = true
  }
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.routerboxmac}】` : '新增'
}

// 申请详情
const getDataView = (data) => {
  nextTick(async () => {
    const result = await lyqStore.view({'routerboxid.value': data.routerboxid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
    state.modalVisible = true
    // 选择信息
    await getDataType()
  })
}

// 拉取产品详情
import allStoreApi from '@store/modules/allStoreApi'
// 接口封装文件
const alleApi = allStoreApi()
const getDataType = () => {
  nextTick(async () => {
    const Data2 = await alleApi.list({
      pagevalue: 354,
      pageindex: 1,
      rows: 99,
      'productxkky.value': 1,
      'productsbky.value': 1,
      'productsfky.value': 1,
      'productagentsid.value': state.editView.routerboxqyid || '',
      'productagentsqym.value': state.editView.routerboxqym || ''
    })
    state.textTypeData = Data2.Data
    console.log(state.textTypeData, 'taocanTypeData')
  })
}

// import productStoreApi from '@store/modules/productStoreApi'
// const productManageStore = productStoreApi()
// const getDataType = () => {
//   nextTick(async () => {
//     const {Total, Data} = await productManageStore.list({
//       pageindex: 1,
//       rows: 99,
//       'productxkky.value': 1,
//       'productsbky.value': 1,
//       'productsfky.value': 1,
//       'productagentsid.value': state.editView.routerboxqyid || '',
//       'productagentsqym.value': state.editView.routerboxqym || ''
//     })
//     state.textTypeData = Data
//   })
// }

defineExpose({
  openDialog
})
</script>
