<template>
  <van-dialog v-model:show="printTrueFalseBy"
              title="打印机列表"
              :show-confirm-button="false"
              close-on-click-overlay
  >
    <div style="width:100%;max-height:65vh;overflow:auto">
      <van-radio-group v-model="print">
        <van-cell-group inset>
          <van-cell v-for="(item,i) in printList" :title="item.printer" clickable @click="onPrinter(item)"  >
            <template #right-icon>
              <van-radio :name="item.server" />
            </template>
          </van-cell>
        </van-cell-group>
      </van-radio-group>
    </div>
  </van-dialog>
</template>
<script setup>
import { ref } from 'vue'
import { getPrinter } from '@/api/basic/index'
import { closeLoading, showLoading } from '@/utils/loading'

const printTrueFalseBy=ref(false)
const print=ref('')
const printList=ref([])
const show=(code)=>{
  print.value=''
  if(localStorage.getItem('PRINTER:KEY')){
    const list=JSON.parse(localStorage.getItem('PRINTER:KEY'))
    printList.value=list.filter(item=>item.warehouseCode===code && item.enable===true)
    printTrueFalseBy.value=true
    return
  }
  showLoading()
  getPrinter().then(res => {
    printList.value=res.data.filter(item=>item.warehouseCode===code)
    localStorage.setItem('PRINTER:KEY',JSON.stringify(res.data))
    printTrueFalseBy.value=true
  }).finally(() => {
    closeLoading()
  })

}
const emit = defineEmits()
const onPrinter=(item)=>{
  print.value = item.server
  printTrueFalseBy.value=false
  emit('onPrint', item)
}
defineExpose({show})
</script>
<style scoped lang="sass">
</style>
