<template>
  <div class="print-box">
    <el-row class="tem-row">
      <el-col :span="24">
        <div id="printTest" class="tem-img">
          <div class="clearfix" style="font-size: 12px">
            <div class="float_left">
              <span>打印时间：{{ no_time }}</span>
              <span v-if="orderDetail.printNum" style="margin-left: 10px">
                第{{ orderDetail.printNum + 1 }}次打印
              </span>
            </div>
            <div class="float_right">
              <p
                v-if="orderDetail.serialNum"
                style="padding-bottom: 3px; font-weight: bold"
              >
                {{ orderDetail.serialNum }}
              </p>
              <p>{{ enterprise_title }}技术支持</p>
            </div>
          </div>
          <div v-if="orderDetail.printingNum > 0" class="print-tag">
            <img :src="require('@/assets/img/print-tag.png')" alt="" />
          </div>
          <p style="font-size: 20px; text-align: center">
            <span v-if="form_obj.basic.indexOf('enterprise') > -1">
              {{ orderDetail.shopName }}
            </span>
            {{ orderDetail.typeName }}
          </p>
          <div class="infomation">
            <span v-if="form_obj.basic.indexOf('createTime') > -1">
              制单日期:{{ $_common.formatDate(orderDetail.createTime) }}
            </span>
            <span
              v-if="form_obj.basic.indexOf('orderNo') > -1"
              style="padding-left: 20px"
            >
              单据号:{{ orderDetail.no }}
            </span>
            <br />
          </div>
          <div id="second">
            <table
              border="1"
              style="width: 100%; margin-top: 10px"
              class="goods-table"
            >
              <tr>
                <th v-if="form_obj.goods.indexOf('goodsId') > -1">序号</th>
                <th v-if="form_obj.goods.indexOf('goodsName') > -1">
                  商品名称
                </th>
                <th v-if="form_obj.goods.indexOf('unitName') > -1">单位</th>
                <th v-if="form_obj.goods.indexOf('specName') > -1">属性</th>
                <th v-if="form_obj.goods.indexOf('purchaseNum') > -1">
                  采购数量
                </th>
                <th v-if="form_obj.goods.indexOf('WarehousingNum') > -1">
                  入库数量
                </th>
                <th
                  v-if="
                    form_obj.goods.indexOf('unitPrice') > -1 &&
                    $accessCheck($Access.moneyshowstockInMoenyShow)
                  "
                >
                  单价
                </th>
                <th
                  v-if="
                    form_obj.goods.indexOf('totalPrice') > -1 &&
                    $accessCheck($Access.moneyshowstockInMoenyShow)
                  "
                >
                  总价
                </th>
                <th
                  v-if="
                    form_obj.goods.indexOf('reservoirArea') > -1 &&
                    enableLocationManagement === '5'
                  "
                >
                  库区
                </th>
                <th
                  v-if="
                    form_obj.goods.indexOf('location') > -1 &&
                    enableLocationManagement === '5'
                  "
                >
                  库位
                </th>
                <th v-if="form_obj.goods.indexOf('strageCode') > -1">
                  货架编码
                </th>
                <th v-if="form_obj.goods.indexOf('barCode') > -1">商品条码</th>
              </tr>
              <tr v-for="(item, index) in orderDetail.details" :key="index">
                <td v-if="form_obj.goods.indexOf('goodsId') > -1">
                  {{ index + 1 }}
                </td>
                <td v-if="form_obj.goods.indexOf('goodsName') > -1">
                  {{ item.materielName }}
                </td>
                <td v-if="form_obj.goods.indexOf('unitName') > -1">
                  {{ item.unitName }}
                </td>
                <td v-if="form_obj.goods.indexOf('specName') > -1">
                  {{ item.skuName }}
                </td>
                <!-- 采购数量-->
                <td v-if="form_obj.goods.indexOf('purchaseNum') > -1">
                  {{ $_common.formatNub(item.num) }}
                </td>
                <!-- 入库数量-->
                <td v-if="form_obj.goods.indexOf('WarehousingNum') > -1">
                  {{ $_common.formatNub(item.inNum) }}
                  <span v-if="item.isEq === 5">
                    （ 其他单位:{{ $_common.formatNub(item.otherNum) }}）
                  </span>
                </td>
                <td
                  v-if="
                    form_obj.goods.indexOf('unitPrice') > -1 &&
                    $accessCheck($Access.moneyshowstockInMoenyShow)
                  "
                >
                  {{ $_common.formattedNumber(item.unitPrice) }}
                </td>
                <td
                  v-if="
                    form_obj.goods.indexOf('totalPrice') > -1 &&
                    $accessCheck($Access.moneyshowstockInMoenyShow)
                  "
                >
                  {{ $_common.formattedNumber(item.totalPrice) }}
                </td>
                <!--                库区-->
                <td
                  v-if="
                    form_obj.goods.indexOf('reservoirArea') > -1 &&
                    enableLocationManagement === '5'
                  "
                >
                  {{ item.areaName }}
                </td>
                <!--                库位-->
                <td
                  v-if="
                    form_obj.goods.indexOf('location') > -1 &&
                    enableLocationManagement === '5'
                  "
                >
                  {{ item.storageLocationName }}
                </td>
                <td v-if="form_obj.goods.indexOf('strageCode') > -1">
                  {{ item.skuStorage }}
                </td>
                <td v-if="form_obj.goods.indexOf('barCode') > -1">
                  {{ item.skuBarCode }}
                </td>
              </tr>
              <tr>
                <td
                  v-if="form_obj.goods.indexOf('goodsId') > -1"
                  colspan="1"
                  style="text-align: center"
                >
                  合计：
                </td>
                <td v-if="form_obj.goods.indexOf('goodsName') > -1">--</td>
                <td v-if="form_obj.goods.indexOf('unitName') > -1">--</td>
                <td v-if="form_obj.goods.indexOf('specName') > -1">--</td>
                <!-- 采购数量-->
                <td v-if="form_obj.goods.indexOf('purchaseNum') > -1">
                  {{ totalNum }}
                </td>
                <!--                入库数量-->
                <td v-if="form_obj.goods.indexOf('WarehousingNum') > -1">
                  {{ totalWarehousingNum }}
                </td>
                <td v-if="form_obj.goods.indexOf('unitPrice') > -1">--</td>
                <td v-if="form_obj.goods.indexOf('totalPrice') > -1">
                  {{ totalMoney }}
                </td>
                <!--                库区-->
                <td
                  v-if="
                    form_obj.goods.indexOf('reservoirArea') > -1 &&
                    enableLocationManagement === '5'
                  "
                >
                  --
                </td>
                <!--                库位-->
                <td
                  v-if="
                    form_obj.goods.indexOf('location') > -1 &&
                    enableLocationManagement === '5'
                  "
                >
                  --
                </td>
                <td v-if="form_obj.goods.indexOf('strageCode') > -1">--</td>
                <td v-if="form_obj.goods.indexOf('barCode') > -1">--</td>
              </tr>
              <tr
                v-if="
                  form_obj.other.indexOf('remark') > -1 &&
                  $accessCheck($Access.moneyshowstockInMoenyShow)
                "
              >
                <td :colspan="form_obj.goods.length" class="remarks">
                  <pre>备注：{{ orderDetail.remark }}</pre>
                </td>
              </tr>
              <tr v-else>
                <td colspan="10" class="remarks">
                  <pre>备注：{{ orderDetail.remark }}</pre>
                </td>
              </tr>
            </table>
          </div>
          <div class="sign clearfix">
            <span
              v-if="form_obj.other.indexOf('signature') > -1"
              class="float_left"
            >
              签字确认：
            </span>
            <span class="float_right">经手人：{{ userName }}</span>
          </div>
        </div>
      </el-col>
      <div style="text-align: center">
        <el-button
          v-print="'#printTest'"
          type="primary"
          @click="PrintNumPrintIncr"
        >
          确认打印（已打印{{ orderDetail.printNum || 0 }}次）
        </el-button>
      </div>
    </el-row>
  </div>
</template>

<script>
  import { getEnToken } from "@/utils/accessToken";
  import { getPurchaseInInfo } from "@/api/Stock";
  import { getTemplateInfo } from "@/api/System";
  import { PrintNumPrintIncr } from "@/api/common";
  import { mapGetters } from "vuex";

  export default {
    name: "StoragePrinting",
    data() {
      return {
        no_time: "",
        tableData: [],
        add_form: {},
        form_obj: {
          basic: [],
          goods: [],
          other: [],
        },
        meal_id: 3,
        EnToken: getEnToken(),
        info_list: [],
        orderDetail: { details: [] },
      };
    },
    computed: {
      ...mapGetters({
        enableLocationManagement: "MUser/enableLocationManagement",
      }),
      totalNum() {
        if (!this.orderDetail.details.length) {
          return 0;
        } else if (this.orderDetail.details.length === 1) {
          return this.orderDetail.details[0].num - 0;
        } else {
          let sum = 0;
          for (let i in this.orderDetail.details) {
            const item = this.orderDetail.details[i];
            sum = this.$NP.plus(sum, item.num - 0);
          }
          return sum;
        }
      },
      totalWarehousingNum() {
        if (!this.orderDetail.details.length) {
          return 0;
        } else if (this.orderDetail.details.length === 1) {
          return this.orderDetail.details[0].inNum - 0;
        } else {
          let sum = 0;
          for (let i in this.orderDetail.details) {
            const item = this.orderDetail.details[i];
            sum = this.$NP.plus(sum, item.inNum - 0);
          }
          return sum;
        }
      },
      totalMoney() {
        if (!this.orderDetail.details.length) {
          return 0;
        } else if (this.orderDetail.details.length === 1) {
          return this.orderDetail.details[0].totalPrice - 0;
        } else {
          let sum = 0;
          for (let i in this.orderDetail.details) {
            const item = this.orderDetail.details[i];
            sum = this.$NP.plus(sum, item.totalPrice - 0);
          }
          return sum;
        }
      },
    },
    created() {
      this.no_time = this.$_common.formatDate(new Date().getTime());
      this.getTemplateInfo();
      this.getPurchaseInInfo();
    },
    methods: {
      async PrintNumPrintIncr() {
        const data = await PrintNumPrintIncr({
          objectNo: this.orderDetail.no,
          objectType: this.orderDetail.type,
        });
        this.orderDetail.printNum += 1;
        setTimeout(() => {
          window.close();
        }, 1000);
      },
      // 获取自定义模板详情
      async getTemplateInfo() {
        const { data } = await getTemplateInfo(this.meal_id);

        this.info_list = data.fieldsData;
        this.add_form = data.fieldsData.templateInfo;
        // 基础信息
        this.form_obj.basic = this.add_form.basic
          .filter((item) => item.isShow === 5)
          .map((item) => item.alias);
        // 商品清单
        this.form_obj.goods = this.add_form.goods
          .filter((item) => item.isShow === 5)
          .map((item) => item.alias);
        //其他
        this.form_obj.other = this.add_form.other
          .filter((item) => item.isShow === 5)
          .map((item) => item.alias);
      },
      // 获取打印详情
      async getPurchaseInInfo() {
        const data = await getPurchaseInInfo(this.$route.params.id);
        this.orderDetail = data.data;
      },
    },
  };
</script>

<style scoped lang="scss">
  .tem-row {
    padding: 20px 80px;
    position: relative;
  }
  .tem-img {
    width: 100%;
    /*padding: 20px 0;*/
  }
  .infomation {
    margin-bottom: 5px;
    font-size: 14px;
  }
  .sign {
    margin-top: 10px;
    font-size: 14px;
  }
  .add_num {
    margin-right: 10px;
  }
  .goods-table th,
  .goods-table td {
    text-align: center;
    line-height: 28px;
    font-size: 14px;
  }
  .goods-table .remarks {
    line-height: 32px;
    text-align: left;
    padding-left: 5px;
    font-weight: bold;
  }
  .print-tag {
    position: absolute;
    right: 190px;
    top: 60px;
    img {
      width: 120px;
      opacity: 0.7;
    }
  }
</style>
