<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-alert
          type="warning"
          title="温馨提示：小票打印机目前仅支持芯烨云打印机"
          :closable="false"
          show-icon
      />
    </el-card>
    <el-card class="!border-none mt-4" shadow="never" v-loading="pager.loading">
      <div>
        <el-button v-perms="['article:cate:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus"/>
          </template>
          新增打印机
        </el-button>
      </div>
      <el-table class="mt-4" size="large" :data="pager.lists">
        <el-table-column type="index" width="55"/>
        <el-table-column label="打印机名称" prop="name" min-width="80"/>
        <el-table-column
            label="设备类型"
            prop="type"
            min-width="120"
            :formatter="(item) => (item.type == '1' ? '芯烨云' : '未知')"
        />
        <el-table-column label="终端号" prop="finalNumber" min-width="120"/>
        <!--                <el-table-column-->
        <!--                    label="小票模板"-->
        <!--                    prop="mode"-->
        <!--                    min-width="120"-->
        <!--                    :formatter="(item) => (item.mode == '1' ? '模板01' : '未知')"-->
        <!--                />-->
        <el-table-column label="来单语音" prop="timbre" min-width="80" :formatter="timbreFormatter"/>
        <el-table-column label="打印机音量" prop="volume" min-width="80" :formatter="volumeFormatter"/>
        <el-table-column label="状态" min-width="80">
          <template #default="{ row }">
            <el-switch
                v-perms="['article:cate:change']"
                v-model="row.status"
                :active-value="1"
                :inactive-value="0"
                @change="changeStatus(row.id,row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['article:cate:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
                v-perms="['article:cate:edit']"
                type="primary"
                link
                @click="handlePrint(row)"
            >
              测试打印机
            </el-button>
            <el-button
                v-perms="['article:cate:del']"
                type="danger"
                link
                @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false"/>
  </div>
</template>
<script lang="ts" setup>
// import {articleCateDelete, articleCateStatus} from '@/api/article'
import {delPrinter,editPrinter} from '@/api/shop'
import {usePaging} from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {getPrinterList} from '@/api/shop'
import {timeFormat} from '@/utils/util'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const {pager, getLists} = usePaging({
  fetchFun: getPrinterList
})
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const timbreFormatter = (row: any) => {
  if (row.timbre == 0) {
    return '真人语音'
  } else if (row.timbre == 3) {
    return '嘀嘀声'
  } else if (row.timbre == 4){
    return '静音'
  } else {
    return '真人语音'
  }
}
const volumeFormatter = (row: any) => {
  if (row.volume == 0) {
    return '大'
  } else if (row.volume == 1) {
    return '中'
  } else if (row.volume == 2) {
    return '小'
  } else if (row.volume == 3) {
    return '关闭'
  } else {
    return '大'
  }
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  // await articleCateDelete({id})
  await delPrinter({id})
  feedback.msgSuccess('删除成功')
  getLists()
}

const changeStatus = async (id: number,status: any) => {
  try {
    await editPrinter({id,status})
    feedback.msgSuccess('修改成功')
    getLists()
  } catch (error) {
    getLists()
  }
}

const handlePrint = async (row: any) => {
  const res = await feedback.request({
    url: '/api/printer/print',
    method: 'post',
    data: {
      id: row.id,
      brandId: row.brandId,
      houseId: row.houseId,
      orderNo: '123456789987654321',
      deskNo: '大厅',
      orderSource: '扫码支付',
      orderTime: timeFormat(Date.now(), 'yyyy年mm月dd日 hh时MM分'),
      remark: '测试打印机',
      userNum: '4',
      amount: '30',
      discount: '10',
      orderDishes: [
        {'title': '测试菜品1', 'num': '1', 'summary': '10'},
        {'title': '测试菜品2', 'num': '1', 'summary': '18'},
        {'title': '测试菜品3', 'num': '1', 'summary': '12'}
      ]
    }
  })
  if (res.code == 200) {
    feedback.msgSuccess('测试打印成功')
  } else {
    feedback.msgError('测试打印失败')
  }
}
getLists()
</script>
