<script>
import Print_tag_60_40 from '@/components/print/tag_60_40.vue';
import Print_express_75 from '@/components/print/express_75.vue';
import {updatePrintStatus} from '@/api/prescription/prescriptionCore';

export default {
  name: 'printContainer',
  props: {
    id: {type: [String, Number], required: true},
    loadExpress: {type: Boolean, default: false},
  },
  components: {Print_express_75, Print_tag_60_40},
  data() {
    return {};
  },
  computed: {
    height() { return `${window.innerHeight * 0.8}`; },
    style() { return {height: `${this.height}px`}; },
  },
  watch: {},
  mounted() {
    this.select(this.loadExpress ? 0 : 1);
  },
  methods: {
    async print() {
      const keys = Object.keys(this.$refs).filter(key => key.startsWith('print'));
      try {
        for (const key of keys) {
          const el = this.$refs[key];
          await el['print']();
        }
        this.$message.success(`组合打印成功`);
      } catch (e) {
        this.$message.error(`出错了，请重试`);
      }
    },
    update(type) {
      if (type === 0 || type === 1) {
        updatePrintStatus({
          isPrint: '1',
          id: this.id,
        });
      }
    },
    select(index) {
      try {
        const el = this.$refs[`print${index}`];
        el['print'](true);
      } catch (e) {}
    }
  },
};
</script>

<template>
  <div>
    <el-button class="pin" type="primary" @click="print">组合打印</el-button>
    <el-tabs tab-position="left" :style="style" @tab-click="select($event.index)">
      <el-tab-pane v-if="loadExpress" label="顺丰面单">
        <print_express_75 ref="print0" :id="id" :style="style" @click="$event && update()"></print_express_75>
      </el-tab-pane>
      <el-tab-pane label="标签">
        <print_tag_60_40 ref="print1" :id="id" @click="$event && update(0)"></print_tag_60_40>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style scoped lang="scss">
.pin {
  position: absolute;
  top: 12px;
  right: 60px;
}
</style>
