<template>
  <div>
    <el-dialog title="材料预览" :visible.sync="Visible" width="50%" top="2vh">
      <div class="main flex flexc aic">
        <div class="header tac mb20">
          <div class="materals-name mb10">{{ materialsInfo.materialName }}</div>
          <p class="abbreviation">
            材料全称：{{ materialsInfo.materiaFullName }}
          </p>
        </div>
        <div class="preview-box">
          <img class="sample-sheet-img" :src="api + materialsPreview" />
        </div>
      </div>
      <div slot="footer" class="tac">
        <el-button
          type="primary"
          :loading="loading"
          @click="handlePrint(materialsInfo)"
          >打 印</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    materialsInfo: {
      type: Object,
      required: true,
      default: () => {
        return {};
      },
    },
    previewVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  computed: {
    Visible: {
      get() {
        return this.previewVisible;
      },
      set(val) {
        this.$emit("update:previewVisible", val);
      },
    },
    materialsPreview() {
      return this.materialsInfo.preViewPath;
    },
  },
  data() {
    return {
      api: process.env.VUE_APP_API_BASE_URL + "/",
      loading: false,
    };
  },
  methods: {
    handlePrint(info) {
      let _this = this;
      _this.loading = true;
      /* eslint-disable */
      device.printer({
        papertype: "A4",
        printername: "", // 打印机名称 为空则为默认打印机
        printertype: "url", // base64 ,normal,url
        url: this.api + info.samplePath, // url
        success: (res) => {
          if (res && res.code == "00000") {
            _this.$message.success("打印成功");
            _this.loading = false;
          }
        },
        fail: (err) => {
          _this.$message.error(err.Message);
          _this.loading = false;
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
:deep(.el-dialog__body) {
  height: 78vh;
  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 6px;
    overflow-y: auto;
  }

  &::-webkit-scrollbar-thumb {
    border-radius: 6px;
    background-color: rgba(144, 147, 153, 0.5);
  }

  &::-webkit-scrollbar-track {
    border-radius: 6px;
    background: #fff;
  }
}
.main {
  width: 100%;
  .materals-name {
    font-size: 18px;
  }
  .abbreviation {
    font-size: 14px;
    color: #999;
  }

  .preview-box {
    width: 80%;
    // box-shadow: 2px 2px 8px 8px #ccc;
    .sample-sheet-img {
      width: 100%;
    }
  }
}
</style>