<template>
  <a-drawer
    v-model:visible="vdata.drawerVisible"
    :mask-closable="false"
    :title="'修改设备'"
    :body-style="{ paddingBottom: '80px' }"
    width="40%"
    class="drawer-width"
    @close="onClose"
  >
    <a-form v-if="vdata.drawerVisible" ref="infoFormModel" :model="vdata.saveObject" layout="horizontal" :rules="vdata.rules">
      <a-row justify="space-between" type="flex">
        <a-col :span="24">
          <a-form-item label="选择厂商" name="provider">
            <a-select v-model:value="vdata.saveObject.provider" :disabled="true" placeholder="请选择厂商">
              <a-select-option v-for="item in providerList" :key="item.value" :value="item.value">{{ item.text }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="选择厂商配置" name="configId">
            <a-select v-model:value="vdata.saveObject.configId" placeholder="请选择厂商">
              <a-select-option v-for="d in vdata.providerConfigList" :key="d.configId" v-model:value="d.configId">
                {{ d.configDesc }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="设备名称" name="deviceName">
            <a-input v-model:value="vdata.saveObject.deviceName" placeholder="请输入设备名称" />
          </a-form-item>
        </a-col>
      </a-row>

      <!-- 动态组件 -->
      <component :is="vdata.configComponent" ref="configComponentRef" />
    </a-form>
    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" style="margin-right:8px" @click="onClose"><close-outlined />取消</a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="handleOkFunc"><check-outlined />保存</a-button>
    </div>
  </a-drawer>
</template>

<script lang="ts" setup>
import { defineProps, getCurrentInstance, nextTick, reactive, ref } from 'vue'
import { API_URL_DEVICE_PROVIDER, API_URL_STORE_DEVICE, req } from '@/api/manage'
import CommonConfig from '@/views/device/mchstoredevice/speaker/CommonConfig.vue'
import providerJSON from '../../provider.json'

const providerList = providerJSON.speaker

  const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties
 
  const props = defineProps({
    callbackFunc: { type: Function, default:null }
  })

  const configComponentRef = ref()
  const infoFormModel = ref()

  /** 定义全部组件 **/
  const allConfigPage = {
    'bsj': CommonConfig,
    'ps': CommonConfig,
    'zgwl': CommonConfig,
  }

  const  vdata = reactive({
    configComponent: null as any, // 动态组件 当前配置组件
    providerConfigList: [] as any, // 厂商列表
    storeList: [] as any, // 门店列表
    btnLoading: false,
    deviceType: 1, // 1-云喇叭 2-云打印 3-扫码POS
    saveObject: {} as any, // 数据对象
    recordId: null, // 更新对象ID
    drawerVisible: false, // 是否显示弹层/抽屉
    rules: {
      deviceName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }]
    }
  }) as any

  function show (record: any) { // 弹层打开事件
  
    vdata.deviceType = record.deviceType

    vdata.saveObject = { 'state': 1, 'deviceType': vdata.deviceType }
    vdata.configComponent = null

    if (infoFormModel.value != undefined) {
      infoFormModel.value.resetFields()
    }

    req.list(API_URL_DEVICE_PROVIDER, { 'pageSize': -1, 'state': 1, 'deviceType': vdata.deviceType, 'provider': record.provider }).then(res => { // 厂商配置下拉选择列表
      vdata.providerConfigList =  res.records
    })
    
    vdata.recordId = record.deviceId
    vdata.configComponent = allConfigPage[record.provider] || CommonConfig

    req.getById(API_URL_STORE_DEVICE, vdata.recordId).then((res: any) => {

      vdata.drawerVisible = true

      if(res){
        vdata.saveObject = res

        // 设置当前存储的配置项
        let currentConfig = JSON.parse(vdata.saveObject.deviceParams || '{}' )
        currentConfig.bindQrcId = vdata.saveObject.bindQrcId

        // 调用组件的渲染函数
        nextTick(() => {
          configComponentRef.value.pageRender(currentConfig, vdata.saveObject.deviceType, false)
        })

      }
    })
  }

  function  handleOkFunc () { // 点击【确认】按钮事件
    infoFormModel.value.validate().then((valid: any) =>{
      configComponentRef.value.getConfigParams().then((deviceParams: any) => {
      
        vdata.btnLoading = true

        // 赋值
        vdata.saveObject.deviceNoParams = deviceParams.deviceNo || ''
        vdata.saveObject.deviceNo = deviceParams.deviceNo || ''
        vdata.saveObject.bindQrcId = deviceParams.bindQrcId || ''

        // 请求接口
        req.updateById(API_URL_STORE_DEVICE, vdata.recordId, vdata.saveObject).then((res: any) => {
          successFunc('修改成功')
        }).catch((err: any) => {
          vdata.btnLoading = false
        })
      })
    })
  }

  function successFunc(e: string) { // 新增/更新成功
    vdata.btnLoading = false
    $infoBox.message.success(e)
    props.callbackFunc()
    vdata.drawerVisible = false
  }

  function  onClose () { // 关闭抽屉
    vdata.drawerVisible = false
  }
defineExpose({
  show
})
</script>
