<template>
  <el-dialog :title="title" v-model="open" append-to-body width="400px">
    <el-form ref="createFormRef" :model="createForm" label-width="100px">
      <el-form-item :label="t('sip.sipidGen998538-0')">
        <el-cascader 
          v-model="createForm.city" 
          :options="cityOptions" 
          :props="{ checkStrictly: true }"
          @change="changeProvince" />
       
      </el-form-item>
      <el-form-item :label="t('sip.index998533-9')" prop="deviceType">
        <el-select v-model="createForm.deviceType" :placeholder="t('sip.index998533-14')">
          <el-option 
            v-for="dict in video_type" 
            :key="dict.value" 
            :label="dict.label"
            :value="dict.value" />
          
        </el-select>
      </el-form-item>
      <el-form-item :label="t('sip.index998533-15')" prop="channelType">
        <el-select v-model="createForm.channelType" :placeholder="t('sip.index998533-16')">
          <el-option 
            v-for="dict in channel_type" 
            :key="dict.value" 
            :label="dict.label"
            :value="dict.value" />
      
        </el-select>
      </el-form-item>
      <el-form-item :label="t('sip.index998533-20')" prop="createNum">
        <el-input-number 
          v-model="createForm.createNum" 
          :placeholder="t('sip.index998533-19')"
          controls-position="right" 
          style="width:220px;" 
          type="number"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ t('sip.index998533-21') }}</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {regionData, codeToText} from 'element-china-area-data'
import { addChannel } from "@/api/pms/video/channel"
import { ElMessage } from 'element-plus'
import { getDicts } from '@/api/pms/video/dicts'


// i18n
const { t } = useI18n()

// 定义属性
const props = defineProps({
  product: {
    type: Object,
    default: null
  }
})

// 定义事件
const emit = defineEmits(['addGenEvent'])

// 定义组件名称和字典
defineOptions({
  name: "SipidDialog",
})

const video_type = ref([])
const channel_type = ref([])

// Refs
const createFormRef = ref()

// Reactive data
const loading = ref(true)
const title = t('sip.sipidGen998538-1')
const total = ref(0)
const open = defineModel('open')
const devsipid = ref("")
const cityOptions = regionData
const city = ref('')
const cityCode = ref('')

const createForm = reactive({
  city: '',
  deviceType: '',
  channelType: '',
  createNum: 1,
  citycode: '',
  productId: '',
  productName: '',
  tenantId: '',
  tenantName: '',
  deviceSipId: '',
  channelSipId: ''
})

// Methods
/** 行政区划改变 **/
const changeProvince = (data) => {
  if (data && data[0] != null && data[1] != null && data[2] != null) {
    const str = codeToText[data[0]] + '/' + codeToText[data[1]] + '/' + codeToText[data[2]]
    createForm.citycode = str
  }
}

/** 提交按钮 */
const submitForm = () => {
  if (createForm.createNum < 1) {
    ElMessage.error(t('sip.index998533-42'))
    return
  }
  
  createForm.productId = props.product.productId
  createForm.productName = props.product.productName
  createForm.tenantId = props.product.tenantId
  createForm.tenantName = props.product.tenantName
  
  if (createForm.city && createForm.city.length >= 3) {
    createForm.deviceSipId = createForm.city[2] + "0000" + createForm.deviceType + "0"
    createForm.channelSipId = createForm.city[2] + "0000" + createForm.channelType + "0"
    
    if (createForm.deviceType !== "" && createForm.channelType !== "" && createForm.city.length === 3) {
      addChannel(createForm.createNum, createForm).then(response => {
        ElMessage.success(t('sip.sipidGen998538-2'))
        devsipid.value = response
        confirmSelectProduct()
      })
    } else {
      ElMessage.error(t('sip.sipidGen998538-3'))
    }
  } else {
    ElMessage.error(t('sip.sipidGen998538-3'))
  }
}

const confirmSelectProduct = () => {
  open.value = false
  emit('addGenEvent', devsipid.value)
}

/**关闭对话框 */
const closeDialog = () => {
  open.value = false
}

defineExpose({
  open
})

onMounted(() => {
  getDicts('video_type').then((response) => {
    video_type.value = response
  })

    getDicts('channel_type').then((response) => {
    channel_type.value = response
  })
})
</script>

<style scoped></style>