<template>
  <div id="app">
    <router-view></router-view>

    <div class="hide">
      <webview
        id="printWebview"
        :src="publicPath"
        nodeintegration
      ></webview>

      <webview
        id="printSmallTicketWebview"
        :src="publicSmallTicketPath"
        nodeintegration
      ></webview>

      <!-- 打印模板 -->
      <PrintTemplate :current-ticket="currentTicket"></PrintTemplate>

      <!-- 小票打印模板 -->
      <SmallTicketPrintTemplate :current-order="printOrder"></SmallTicketPrintTemplate>
    </div>

    <UpdateFile></UpdateFile>
  </div>
</template>

<script>
import PrintTemplate from '@/components/PrintTemplate.vue'
import SmallTicketPrintTemplate from '@/components/SmallTicketPrintTemplate'
import UpdateFile from './UpdateFile'

const { ipcRenderer } = require('electron')
const path = require('path')

export default {
  name: 'AdminClient',
  components: {
    PrintTemplate,
    UpdateFile,
    SmallTicketPrintTemplate
  },
  computed: {
    faceModelIsReady () {
      return this.$store.state.app.faceModelIsReady
    },
    printList () {
      return this.$store.state.app.printList
    },
    printOrder () {
      return this.$store.state.app.printOrder
    }
  },
  data () {
    return {
      publicPath: path.join(__static, 'print.html'),
      publicSmallTicketPath: path.join(__static, 'smallTicket.html'),
      isPrinting: false, // 正在打印
      printers: [],
      currentTicket: null,
      currentOrder: null
    }
  },
  watch: {
    $route: {
      handler (val) {
        console.log(val.fullPath)
      },
      immediate: true
    },
    // 待打印列表
    printList (val) {
      // 打印列表不为空，且当前未在打印时，设置当前打印对象
      if (val.length > 0 && !this.isPrinting) {
        this.isPrinting = true
        this.currentTicket = val[0]
      }
    }
  },
  methods: {
    checkConfig () {
      const serverUrl = this.$localStore.get('serverUrl')
      if (!serverUrl) {
        this.$router.push('/config')
        return false
      } else {
        // 检查更新
        ipcRenderer.send('updateReady', this.$localStore.get('updateUrl'))

        if (this.$route.name === 'config') {
          this.$router.push('/')
        }
      }
    },
    setPrint () {
      ipcRenderer.send('getPrinters')
      ipcRenderer.on('sendPrinters', (event, printers) => {
        console.log(printers)
        this.printers = printers
        this.defaultPrinter = printers.find(i => i.isDefault) && printers.find(i => i.isDefault).name
      })

      this.webview = document.querySelector('#printWebview')
      this.webview.removeEventListener('ipc-message', this.handlePrint)
      this.webview.addEventListener('ipc-message', this.handlePrint)

      this.smallTicketWebview = document.querySelector('#printSmallTicketWebview')
      this.smallTicketWebview.removeEventListener('ipc-message', this.handlePrintSmallTicket)
      this.smallTicketWebview.addEventListener('ipc-message', this.handlePrintSmallTicket)
    },
    handlePrint (event) {
      if (event.channel === 'webview-print-do') {
        // if (this.isPrinting) return
        try {
          let printer
          if (this.currentTicket && this.currentTicket.printer_name && this.printers.find(v => v.name === this.currentTicket.printer_name)) {
            printer = this.currentTicket.printer_name || this.$localStore.get('defaultPrinter') || this.defaultPrinter
          } else {
            printer = this.$localStore.get('defaultPrinter') || this.defaultPrinter
          }
          console.error('开始打印！', printer)
          this.webview.print({
            silent: true,
            printBackground: true,
            deviceName: printer
          }, (data) => {
          // console.log(data)
          // this.$message.success('打印成功')
            console.log('打印成功')
            // 打印成功后，更新打印列表
            this.isPrinting = false
            this.$store.commit('PRINT_NEW')
          })
        } catch (e) {
          console.log(e)
        }
      }
    },
    handlePrintSmallTicket (event) {
      if (event.channel === 'webview-print-do') {
        // if (this.isPrinting) return
        try {
          let printer = this.$localStore.get('defaultSmallTicketPrinter') || this.defaultPrinter
          console.error('开始打印！', printer)
          this.smallTicketWebview.print({
            silent: true,
            printBackground: true,
            deviceName: printer
          }, (data) => {
          // console.log(data)
          // this.$message.success('打印成功')
            console.log('打印成功')
            // 打印成功后，更新打印列表
            this.isPrinting = false
            this.$store.commit('SET_PRINT_ORDER', null)
          })
        } catch (e) {
          console.log(e)
        }
      }
    },
    setConfigEvent () {
      ipcRenderer.on('goConfig', () => {
        this.$router.push('/config')
      })
      ipcRenderer.on('goPos', () => {
        this.$router.push('/posTool')
      })
    }
  },
  created () {
    this.$log.info('程序初始化完成')
  },
  mounted () {
    console.log('app mounted')
    // 检查本地配置
    this.checkConfig()
    // 设置打印事件
    this.setPrint()
    // 设置配置事件
    this.setConfigEvent()

    this.$store.dispatch('initDonsee100R')
  }
}
</script>

<style lang="scss">
.success {
  color: #67c23a;
}

.danger {
  color: #f56c6c;
}

i {
  font-style: normal;
}

.hidden {
  width: 0;
  height: 0;
  overflow: hidden;
}

.el-card__header {
  padding: 10px 20px;
  font-size: 16px;
  line-height: 22px;
}

.form-wrap {

  .el-input,
  .el-select,
  .el-date-editor,
  .el-date-editor.el-input,
  .el-input-number {
    width: 194px;
  }
}

.el-pagination {
  padding: 30px 0 20px;
  text-align: center;

  .el-select {
    width: auto;

    input {
      height: 28px;
    }
  }

  .el-input__icon {
    line-height: 28px;
  }

  &__jump {
    .el-input {
      width: 50px !important;
    }
  }
}

.el-table {
  .el-table__row.expanded {
    background: lighten(#b5bec7, 18%);
  }

  .el-table__expanded-cell {
    background: lighten(#e9eaee, 5%) !important;

    &:hover {
      background: lighten(#e9eaee, 5%) !important;
    }
  }

  .el-tag {
    margin: 3px 5px 3px 0;
  }
}

// 表格的详情展开面板
.table-expand {
  .el-form-item {
    label {
      color: #99a9bf;
    }
  }
}

// 对话框padding太大了，30 20改成20
.el-dialog__body {
  padding: 20px;
}

// 表单信息p标签不换行
.p-nowrap {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// 信息对话框的样式
.info-form {
  display: flex;
  flex-wrap: wrap;

  .info-form-line {
    display: flex;
    width: 50%;
    height: 40px;
    line-height: 40px;

    .info-form-label {
      width: 80px;
      padding-right: 12px;
      color: #64667b;
      text-align: right;
      flex-shrink: 0;
      flex-grow: 0;
    }
  }
}

.overflow-txt {
  display: block;
  width: 180px;
  overflow: hidden;

  text-overflow: ellipsis;
  white-space: nowrap;
}

// 图片上传样式
.avatar-uploader {
  width: 100px;
  height: 100px;
  text-align: center;
  background: #eee;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;

  .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
  }

  .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    width: 100px;
    height: 100px;
    font-size: 28px;
    line-height: 100px;
    color: #8c939d;
    text-align: center;
  }

  .avatar {
    display: block;
    width: 100px;
    height: 100px;
    background: center center no-repeat;
    background-size: contain;
  }
}

.no-wrap {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.offSale-btn {
  color: #f56c6c;
}

.onSale-btn {
  color: #67c23a;
}

#printWebview {
  width: 500px;
  height: 500px;
}

.hide {
  width: 0;
  height: 0;
  overflow: hidden;
}

.nohide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9999;
}

.card-title {
  display: flex;
  color: #333;
  align-items: center;
  justify-content: space-between;

  .price {
    b {
      font-size: 1.6em;
      color: $blue;
    }
  }
}

.price {

  b {
    color: $blue;
    font-size: 1.6em;
  }
}

.buyer-info {
  .el-form-item {
    display: inline-block;
    margin-right: 20px;
  }

  .el-form-item__content {
    font-weight: bold;
  }
}

.readCard {
  position: absolute;
  right: 0;
  z-index: 0;
  display: flex;
  width: 80px;
  height: 100%;
  font-size: 12px;
  color: #fff;
  cursor: pointer;
  align-items: center;

  .inner {
    display: flex;
    height: 80%;
    padding: 0 5px;
    padding-right: 30px;
    background: $blue;
    border-radius: 5px;
    align-items: center;
  }

  .wave {
    position: absolute;
    top: 50%;
    left: 50%;
    display: none;
    width: 20px;
    height: 20px;
    box-sizing: border-box;
    margin-top: -10px;
    margin-left: -10px;
    background: rgba(255, 255, 255, .384);
    border: 1px solid red;
    border-radius: 50%;
    opacity: 0;
    animation: wave infinite linear 1s forwards;
  }

  .wave2 {
    opacity: 0;
    animation: wave infinite 2s 1s forwards;
  }

  .svg-icon {
    position: relative;
    width: 16px;
    height: 16px;
    margin-right: 5px;
    font-size: 16px;
  }

  &.active {
    .wave {
      display: block;
    }

    .svg-icon {
      color: #fff;
    }
  }
}

@keyframes wave {
  0% {
    opacity: 0;
    transform: scale(3);
  }

  50% {
    opacity: .5;
    transform: scale(1);
  }

  100% {
    opacity: 0;
    transform: scale(0);
  }
}

.dark-btn {
  display: flex;
  height: 26px;
  padding: 0 5px;
  margin-left: 10px;
  font-size: 12px;
  color: #fff;
  cursor: pointer;
  background: $blue;
  border-radius: 5px;
  align-items: center;

  .svg-icon {
    width: 16px;
    height: 16px;
    margin-right: 5px;
    color: #fff;
  }
}

.pay-channel-wrap {
  display: flex;
  margin-left: 20px;
  margin-bottom: 10px;
  align-items: center;

  .text {
    display: inline-block;
    width: 73px;
    padding-bottom: 5px;
  }
}

.tags-btn {
  flex: 1;

  .el-radio.is-bordered+.el-radio.is-bordered {
    margin-left: 0;
  }

  .el-radio--small.is-bordered .el-radio__label {
    font-size: 14px;
    line-height: 20px;
  }

  .el-radio.is-bordered.is-checked {
    background: #0075ff;
  }

  .el-radio__input.is-checked+.el-radio__label {
    color: #fff;
  }

  .el-radio.is-bordered {
    padding: 5px 10px;
    margin: 0px 10px 5px 0;
  }

  .el-radio__input {
    display: none;
  }

  .el-radio__label {
    padding: 0;
  }
}

.el-submenu .el-menu-item {
  padding: 0 30px;
}
</style>
