/**
 * EPC 打印界面
 */

<template>
  <div>
    <!-- 打印结果模态框 -->
    <Modal
      ref="printResultModal"
      title="发卡机发卡"
    >
      <div
        v-if="printResult != null && printResult.command == 'read'"
        class="alert alert-info"
        role="alert"
      >
        {{ printResult.success ? '操作成功' : '操作失败' }},读取到{{ printResult.tagCount }}个标签,{{ printResult.epcs }}
      </div>

      <div
        v-if="printResult != null && printResult.command == 'write'"
        class="alert alert-danger"
        role="alert"
      >
        {{ printResult.success ? '操作成功' : '操作失败' }},{{ printResult.message }}待写入EPC：{{ lastWriteEpc }}
      </div>

      <div
        v-if="printResult != null && printResult.command == 'restore'"
        class="alert alert-danger"
        role="alert"
      >
        {{ printResult.success ? '操作成功' : '操作失败' }},{{ printResult.message }}
      </div>

      <button
        type="button"
        class="btn btn-info"
        @click="printSingleEpc(lastWriteEpc)"
      >
        再次打印
      </button>

      <button
        type="button"
        class="btn btn-info"
        @click="restoreEpc"
      >
        恢复标签
      </button>

      <button
        type="button"
        class="btn btn-info"
        @click="readEpc"
      >
        读取
      </button>

      <button
        type="button"
        class="btn btn-danger"
        @click="$refs.printResultModal.show = false"
      >
        关闭
      </button>
    </Modal>
    <Loading ref="loading" />
  </div>
</template>

<script>
var PrintEpcUtil = require('./PrintEpcUtil.js').default;
var Modal = require('../../modal/src/Modal.vue').default;
var Loading = require('../../loading/src/Loading.vue').default;

export default {
  name: 'PrintEpc',

  components: {
    Modal, Loading,
  },

  props: {
    'printerName':
    {
      type: String,
      default: '',
    },
  },

  data: function () {
    return {
      lastWriteEpc: '',   // 最后写入的EPC
      // 打印结果
      printResult: {
        command: '',    // 指令
        success: true,  // 是否操作成功
        message: '',    // 操作不成功的消息
        tagCount: 0,    // 标签读取的数量
        epcs: '',       // 读取的标签信息
        epc: '',        // 打印的EPC
      },
    };
  },


  methods: {
    /**
         * 打印PrintPages
         */
    printPrintPages: function (printPages) {
      let _self = this;

      if (printPages == null || printPages.length == 0) {
        this.printResult.command = 'write';
        this.printResult.success = false;
        this.printResult.message = '无可打印的数据。';

        this.$refs.printResultModal.show = true;
        return;
      }

      if (printPages.length > 1) {
        this.printResult.command = 'write';
        this.printResult.success = false;
        this.printResult.message = '使用发卡机发卡，每次只能发一个标签，打印程序收到了' + printPages.length + '个标签数据。';

        this.$refs.printResultModal.show = true;
        return;
      }

      let printPage = printPages[0];
      let epc = null;
      for (let index = 0; index < printPage.printItems.length; index++) {
        if (printPage.printItems[index].displayType == 'EPC') {
          epc = printPage.printItems[index].content;
        }
      }

      _self.lastWriteEpc = epc;

      this.printSingleEpc(epc);
    },

    /**
         * 打印单张EPC
         * @param epc 待打印的EPC的内容
         */
    printSingleEpc: function (epc) {
      let _self = this;

      _self.$refs.printResultModal.show = false;

      let promise = PrintEpcUtil.printSingleEpc(this.printerName, epc);

      _self.$refs.loading.show();
      promise.then(successData => {
        _self.$refs.loading.hide();
        if (successData.success == false) {
          successData.command = 'write';
          _self.printResult = successData;
          _self.$refs.printResultModal.show = true;
        }
      }, errorData => {
        _self.$refs.loading.hide();
        if (errorData.success == false) {
          errorData.command = 'write';
          _self.printResult = errorData;
          _self.$refs.printResultModal.show = true;
        }
      });
    },

    /**
         * 恢复EPC
         */
    restoreEpc: function () {
      let _self = this;

      _self.$refs.printResultModal.show = true;

      let promise = PrintEpcUtil.resetTag(this.printerName);

      promise.then(successData => {
        successData.command = 'restore';
        _self.printResult = successData;
        _self.$refs.printResultModal.show = true;
      }, errorData => {
        errorData.command = 'restore';
        _self.printResult = errorData;
        _self.$refs.printResultModal.show = true;
      });
    },

    /**
         * 读取EPC
         */
    readEpc: function () {
      let _self = this;

      _self.$refs.printResultModal.show = true;

      let promise = PrintEpcUtil.readEpc(this.printerName);

      promise.then(successData => {
        successData.command = 'read';
        _self.printResult = successData;
      }, errorData => {
        errorData.command = 'read';
        _self.printResult = errorData;
      });
    },

    /**
         * 显示模态框
         */
    show: function () {
      this.$refs.printResultModal.show = true;
    },
  },
};
</script>
