<!-- 采购入库单 -->
<template>
  <modal
    v-model="modal"
    width="700px"
    class-name="vertical-center-modal"
    :footer-hide="true"
  >
    <!--  表单主要内容  -->
    <div class="content" ref="print">
      <div
        class="title"
        style="text-align: center; font-size: 20px; font-weight: bold"
      >
        采购入库单
      </div>
      <div
        class="sub-title border"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin: 10px;
          font-size: 14px;
          border-bottom: 1px solid #000000;
          font-weight: bold;
        "
      >
        <span>申购信息</span>
      </div>
      <div
        class="row"
        style="
          padding: 0 20px;
          margin-bottom: 5px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <div class="col" style="flex: 1; display: flex; align-items: center">
          <div class="label">申购单号：</div>
          <div class="value">{{ data.applyBillNo }}</div>
        </div>
        <div class="col" style="flex: 1; display: flex; align-items: center">
          <div class="label">申请日期：</div>
          <div class="value">{{ data.applyDate }}</div>
        </div>
      </div>
      <div
        class="sub-title border"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin: 10px;
          font-size: 14px;
          border-bottom: 1px solid #000000;
          font-weight: bold;
        "
      >
        <span>入库信息</span>
      </div>
      <div
        class="row"
        style="
          padding: 0 20px;
          margin-bottom: 5px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <div class="col" style="flex: 1; display: flex; align-items: center">
          <div class="label">物料编码：</div>
          <div class="value">{{ data.materialCode }}</div>
        </div>
        <div class="col" style="flex: 1; display: flex; align-items: center">
          <div class="label">物料名称：</div>
          <div class="value">{{ data.materialName }}</div>
        </div>
      </div>
      <div
        class="row"
        style="
          padding: 0 20px;
          margin-bottom: 5px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <div class="col" style="flex: 1; display: flex; align-items: center">
          <div class="label">物料规格：</div>
          <div class="value">{{ data.spec }}</div>
        </div>
      </div>
      <div
        class="row"
        style="
          padding: 0 20px;
          margin-bottom: 5px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
      >
        <div class="col" style="flex: 1; display: flex; align-items: center">
          <div class="label">请购数量：</div>
          <div class="value">{{ data.quantity }}</div>
        </div>
      </div>
      <div
        class="sub-title border"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin: 10px;
          font-size: 14px;
          border-bottom: 1px solid #000000;
          font-weight: bold;
        "
      >
        <span>入库明细</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="table">
            <table style="border-collapse: collapse; border: none; width: 100%">
              <tr>
                <th
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  序号
                </th>
                <th
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  入库日期
                </th>
                <th
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  入库数量
                </th>
                <th
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  操作员
                </th>
              </tr>
              <tr v-for="(item, index) in data.inStockDetailList" :key="index">
                <td
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  {{ index + 1 }}
                </td>
                <td
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  {{ item.date }}
                </td>
                <td
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  <Tooltip
                    placement="bottom"
                    @on-popper-show="getData(item, data)"
                    max-width="1000"
                    :transfer="true"
                  >
                    <div style="text-decoration:underline">{{ item.quantity }}</div>
                    <div slot="content">
                      <div
                        v-for="(receiptItem, index) in receiptList"
                        :key="index"
                      >
                        <span style="text-align: left; white-space: normal">
                          数量: {{ receiptItem.quantity }}
                        </span>
                        <span
                          style="
                            padding-left: 16px;
                            text-align: left;
                            white-space: normal;
                          "
                        >
                          扣款合同: {{ receiptItem.purchasebillno }}
                        </span>
                        <span
                          style="
                            padding-left: 16px;
                            text-align: left;
                            white-space: normal;
                          "
                        >
                          {{ receiptItem.balanceState }}
                        </span>
                        <span
                          style="
                            padding-left: 16px;
                            text-align: left;
                            white-space: normal;
                          "
                        >
                          原因: {{ receiptItem.applyreason }}
                        </span>
                      </div>
                    </div>
                  </Tooltip>
                </td>
                <td
                  style="
                    border: solid #dcdee2 1px;
                    padding: 5px;
                    text-align: center;
                    position: relative;
                    font-size: 12px;
                  "
                >
                  {{ item.operateUser }}
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
      <div
        class="sub-title border"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin: 10px;
          font-size: 14px;
          border-bottom: 1px solid #000000;
          font-weight: bold;
        "
      >
        <span>入库图片</span>
      </div>
      <div class="row">
        <div class="col" style="align-items: center">
          <div class="value" style="flex: 1">
            <Upload action="" ref="uploadFileFlag" :before-upload="beforeFile">
              <Button type="primary" size="small">上传</Button>
            </Upload>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div
            class="value"
            style="display: flex; flex-direction: column; color: blue"
          >
            <div v-for="(item, index) in picList" :key="index">
              <span
                @click="openPic(item.filePath)"
                style="cursor: pointer; text-decoration: underline"
                >{{ item.fileName }}</span
              >
              <Icon
                @click="removePic(index)"
                style="padding: 0 10px"
                size="20"
                color="#333"
                type="md-close"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <vue-easy-print ref="print">
      <template>
        <!--未使用打印功能-->
      </template>
    </vue-easy-print>
    <!--    <div class="footer" slot="footer">-->
    <!--      <Button @click="cfm" class="btn" type="primary">打印</Button>-->
    <!--      <Button @click="cancel" class="btn">关闭</Button>-->
    <!--    </div>-->
  </modal>
</template>

<script>
import VueEasyPrint from "vue-easy-print";
import { UploadBase64 } from "@/api/upload";
import { GetContractBills, UploadContractBills } from "@/api/applyPurchase";
import { filePreview } from "@/libs/util";
import axios from "axios";

export default {
  name: "store-in-form",
  data() {
    return {
      modal: false,
      picList: [],
      receiptList: [],
    };
  },
  components: {
    VueEasyPrint,
  },
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
    value: {
      type: Boolean,
      default: false,
      require: true,
    },
  },
  watch: {
    value: {
      handler(n) {
        this.modal = n;
      },
      immediate: true,
    },
    modal(n) {
      this.$emit("input", n);
      if (n) {
        this.getContractBills();
      }
    },
  },
  methods: {
    /* 上传/删除附件 */
    uploadContractBills(fileObj) {
      UploadContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        contractsList: [fileObj],
      }).then((res) => {
        if (res.code === 0) {
          this.getContractBills();
        }
      });
    },
    /* 移出附近 */
    removePic(index) {
      if (this.picList[index].id) {
        UploadContractBills({
          purchaseBillNo: this.data.purchaseBillNo,
          contractsList: [],
          delItems: [this.picList[index].id],
        }).then((res) => {
          if (res.code === 0) {
            this.getContractBills();
          }
        });
      }
    },
    /* 获取附件 */
    getContractBills() {
      GetContractBills({
        purchaseBillNo: this.data.purchaseBillNo,
        fileType: 4,
      }).then((res) => {
        if (res.code === 0) {
          this.picList = res.result;
        }
      });
    },
    beforeFile(file) {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = (event) => {
        UploadBase64({
          fileBase64: event.srcElement.result,
          fileName: file.name,
          floder: "order",
        }).then((res) => {
          if (res.code === 0) {
            let fileObj = {
              size: file.size,
              fileName: file.name,
              filePath: res.result,
              fileType: 4,
            };
            this.uploadContractBills(fileObj);
          }
        });
      };
      return false;
    },
    openPic(url) {
      filePreview(url);
    },
    cfm() {
      // 未使用打印功能，但已改成vue-easy-print，上面代码未放置打印内容
      this.$refs.print.print();
    },
    cancel() {
      this.modal = false;
    },
    getData(item, data) {
      this.receiptList = [];
      if (Number(item.quantity) < 0) {
        axios
          .post("/cloudApi/stockWater/receiptList", {
            purchaseBillNo: data.purchaseBillNo,
          })
          .then((res) => {
            this.receiptList = res.data.data;
          });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  .title {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
  }
  .sub-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px;
    font-size: 14px;
    font-weight: bold;
    &.border {
      border-bottom: 1px solid #000000;
    }
    .go {
      font-size: 10px;
      font-weight: normal;
      color: #0000ff;
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: center;
      .table {
        width: 100%;
        table {
          border-collapse: collapse;
          border: none;
          width: 100%;
          tr {
            th {
              background-color: #eeeeee;
            }
            td,
            th {
              padding: 5px;
              text-align: center;
              border: solid #dcdee2 1px;
              position: relative;
              font-size: 12px;
            }
          }
        }
      }
    }
  }
  .card {
    margin-bottom: 5px;
    background-color: #ffffff;
    border: 1px solid rgba(215, 215, 215, 1);
    border-radius: 5px;
    .border {
      border-bottom: 1px solid #5d6c7b;
    }
    .line {
      width: 100%;
      border-bottom: 1px solid #5d6c7b;
    }
  }
}
</style>
<style>
.vertical-center-modal .ivu-tooltip-popper{
  width: 800px!important;
}
</style>
