<template>
  <div class="bg">
    <el-dialog
      width="900px"
      height="12cm"
      :modal-append-to-body="false"
      title="采购入库单"
      :visible.sync="scanVisible"
    >
      <!-- 表单是否显示 技术列表页面打印-->
      <div style="height: 500px; width: 86%; margin: 0 auto;overflow: hidden;" v-if="scanVisible">
        <div ref="qrcode" id="box">
          <vue-easy-print tableShow ref="easyPrint" :onePageRow="onePageRow">
            <div
              v-for="(item1, index1) in list"
              :key="index1"
              style="margin-bottom: 30px;width: 88%;margin: 0px 0px 0px 2px;"
            >
              <div style="page-break-after: always;">
                <div style="margin: 0px 0px 0px 2px;">
                  <table :border="1">
                    <thead>
                      <tr class="title">
                        <th colspan="15" style="line-height: 12px !important;">
                          <h1 style="display:block;margin:0px;line-height: 16px;font-size:16px;padding: 0px 0px 5px;">采购入库单</h1>
                        </th>
                      </tr>
                      <tr class="title-sub" style="line-height: 12px;">
                        <th colspan="15" style="padding-top: 2px;padding-bottom: 4px;">
                          <span style="float: left;">供应商: {{item1[0].gong_ying_shang_| gongyingshang(gongyingshangList)}}</span>  
                          <span style="margin-left: 6px;float: left;">申购部门: {{item1[0].shen_gou_bu_men_ | bumenfilters(bumenList)}}</span>
                          <span style="margin-left: 6px;float: right;">日期:{{item1[0].dao_ku_ri_qi_}}</span>  
                          <span style="margin-left: 6px;float: right;">采购单号:{{item1[0].cai_gou_ding_dan_}} </span>             
                        </th>
                      </tr>
                      <tr style="border:1px solid #000;font-size: 12px;">
                        <th style="width: 15px">序号</th>
                        <th style="font-weight: normal;width: 25px">物料编码</th>
                        <th style="font-weight: normal;width: 70px">物料名称</th>
                        <th style="width: 60px">货号</th>
                        <th style="width: 60px">批号</th>
                        <th style="width: 50px">规格/特征</th>
                        <th style="width: 60px">生产商/品牌</th>
                        <th style="width: 70px">数量</th>
                        <th style="width: 30px">单位</th>
                        <th style="width: 50px">单价(含税)</th>
                        <th style="width: 50px">税率</th>
                        <th style="width: 40px">单价</th>
                        <th style="width: 40px">税额</th>
                        <th style="width: 40px">金额</th>
                        <th style="width: 50px">含税合计</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr v-for="(item, index) in item1" :key="index" style="border:1px solid #000;">
                        <td style="width: 25px">{{ index+1 }}</td>
                        <td style="width: 70px">{{ item.wu_liao_dai_ma_ }}</td>
                        <td style="width: 100px">{{ item.wu_liao_ming_chen }}</td>
                        <td style="width: 50px">{{ item.huo_hao_ }}</td>
                        <td style="width: 50px">{{ item.pi_hao_ }}</td>
                        <td style="width: 60px">{{ item.gui_ge_ }}</td>
                        <td style="width: 70px;padding: 1px;">{{ item.chang_jia_| gongyingshang(gongyingshangList) }}</td>
                        <td style="width: 30px">{{ item.dao_huo_shu_liang }}</td>
                        <td style="width: 50px;padding: 1px;">{{ item.dan_wei_ }}</td>
                        <td style="width: 50px;padding: 1px;">{{ item.dan_jia_han_shui_|numToFixed}}</td>
                        <td style="width: 40px">{{ item.shui_lv_ }}</td>
                        <td style="width: 40px;padding: 1px;">{{ item.dan_jia_|numToFixed(item.dan_jia_)}}</td>
                        <td style="width: 40px;padding: 1px;">{{ item.shui_e_ |numToFixed}}</td>
                        <td style="width: 40px;padding: 1px;">{{ item.jin_e_ |numToFixed(item.jin_e_)}}</td>
                        <td style="width: 50px;padding: 1px;">
                          {{ item.han_shui_he_ji_|numToFixed(item.han_shui_he_ji_) }}
                        </td>
                      </tr>
                      <tr class="count" style="border:1px solid #000;">
                        <td colspan="15">
                          合计&nbsp;&nbsp;&nbsp;&nbsp;
                          税额:&nbsp;{{ item1.addshuie}}&nbsp;&nbsp;
                          金额:&nbsp;{{ item1.addjine}}&nbsp;&nbsp;
                          含税合计:&nbsp;{{ item1.addhanshuiheji }}
                        </td>
                      </tr>
                    </tbody>
                    <tfoot class="bottom">
                      <tr>
                        <td colspan="5" style="text-align:left">
                          <div style="float: left;margin-top:5px">仓管员:{{ item1.name2 }}</div>
                        </td>
                        <td colspan="10" style="text-align:right">
                          <div style="margin-top:5px;">审核人:{{ item1.name3 }} </div>
                        </td>
                      </tr>
                    </tfoot>
                  </table>
                </div>
              </div>
            </div>
          </vue-easy-print>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="printDemo">打印</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import VueBarcode from "vue-barcode";
import vueEasyPrint from "vue-easy-print";
import repostCurd from "@/business/platform/form/utils/custom/joinCURD.js";

export default {
  components: {
    VueBarcode,
    vueEasyPrint,
  },
  props: {
    obj: {
      default: [],
      type: Array,
    },
    onePageRow: {
      type: Number,
      default: 3,
    },
    blankLines: {
      type: Boolean,
      default: true,
    },
    scanVisible: {
      type: Boolean,
      default: false,
    },
    currentScan: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      onePageRow: 1,
      value: "",
      id: "",
      list: [],
      visible: true,
      gongyingshangList: [],
      bumenList: [],
    };
  },
  created() {
    this.getInit();
    let sql = `select id_,gong_ying_shang_m FROM  t_gysml`;
    repostCurd("sql", sql).then((res) => {
      return (this.gongyingshangList = res.variables.data);
    });
    let sql1 = `select ID_,NAME_ FROM  ibps_party_org `; //部门列表
    repostCurd("sql", sql1).then((res) => {
      return (this.bumenList = res.variables.data);
    });
  },
  filters: {
    gongyingshang: function (value, list) {
      for (let i = 0; i < list.length; i++) {
        if (value == list[i].id_) {
          return list[i].gong_ying_shang_m;
        } else if (i == list.length - 1) {
          return value;
        }
      }
    },
    numToFixed: function (value) {
      return (value*1).toFixed(2)
    },
    bumenfilters: function (value, list) {
      for (let i = 0; i < list.length; i++) {
        if (value == list[i].ID_) {
          return list[i].NAME_;
        } else if (i == list.length - 1) {
          return value;
        }
      }
    },
  },
  watch: {
    obj() {
      this.getInit();
    },
  },
  methods: {
    printDemo() {
      this.$refs.easyPrint.print();
      // this.$refs.easyPrint.window.print()
    },
    addNumber(num1, num2) {
      let a = parseFloat(num1);
      let b = parseFloat(num2);
      return a.toFixed("2") * 1 + b.toFixed("2") * 1;
    },
    riqi(value) {
      let date = new Date(value);
      let y = date.getFullYear(); //获取年份
      let m = date.getMonth() + 1; //获取月份
      m = m < 10 ? "0" + m : m; //月份不满10天显示前加0
      let d = date.getDate(); //获取日期
      d = d < 10 ? "0" + d : d; //日期不满10天显示前加0
      return y + "-" + m + "-" + d;
    },
    addNumber(num1, num2) {
      let a = parseFloat(num1);
      let b = parseFloat(num2);
      return a.toFixed("2") * 1 + b.toFixed("2") * 1;
    },
    toDecimal2(x) {
      var f = parseFloat(x);
      if (isNaN(f)) {
        return false;
      }
      var f = Math.round(x * 100) / 100;
      var s = f.toString();
      var rs = s.indexOf(".");
      if (rs < 0) {
        rs = s.length;
        s += ".";
      }
      while (s.length <= rs + 2) {
        s += "0";
      }
      return s;
    },
    getInit() {
      // var idStr = "";
      // this.obj.forEach((item) => {
      //   idStr += item + ",";
      // });
      // idStr = idStr.substring(0, idStr.length - 1);
      this.getLook(this.obj);
    },
    getLook(id) {
      console.log(id)
      this.list = [];
      let this_ = this;
      let name1, name2, name3;
      let time1, time2, time3;
      console.log(id,"22")
      id.forEach((item) => {
        let sql = `select * FROM t_wljsysjlb WHERE id_='${item}'`;
        repostCurd("sql", sql).then((res) => {
          
          let data = res.variables.data;
          let s2 = `select a.NAME_,DATE_FORMAT(b.CREATE_TIME_,'%Y-%m-%d') AS CREATE_TIME_ FROM ibps_party_employee  as a join ibps_bpm_oper_log as b on a.ID_ = b.CREATE_BY_ where b.PROC_INST_ID_ = ( SELECT  PROC_INST_ID_  FROM ibps_bpm_bus_rel WHERE BUSINESSKEY_ = '${item}') AND b.NODE_ID_ = 'Activity_1pha4py' ORDER BY b.CREATE_TIME_ DESC LIMIT 1`;
          repostCurd("sql", s2).then((res) => {
            
            name2 = res.variables.data[0].NAME_ || 0;
            time2 = res.variables.data[0].CREATE_TIME_ || 0;
            data["name2"] = name2;
            data["time2"] = time2;
            let addhanshuiheji = 0;
            let addjine = 0;
            let addshuie = 0;
            let s3 = `select a.NAME_,DATE_FORMAT(b.CREATE_TIME_,'%Y-%m-%d') AS CREATE_TIME_ FROM ibps_party_employee  as a join ibps_bpm_oper_log as b on a.ID_ = b.CREATE_BY_ where b.PROC_INST_ID_ = ( SELECT  PROC_INST_ID_  FROM ibps_bpm_bus_rel WHERE BUSINESSKEY_ = '${item}' LIMIT 1) AND b.NODE_ID_ = 'Activity_04ff3k5' ORDER BY b.CREATE_TIME_ DESC LIMIT 1`;
            repostCurd("sql", s3).then((res) => {
              
              if(res.variables.data.length >0){
                name3 = res.variables.data[0].NAME_ || 0;
                time3 = res.variables.data[0].CREATE_TIME_ || 0;
              }
              data["name3"] = name3;
              data["time3"] = time3;
              data.forEach((it) => {
                data["addhanshuiheji"] = this.addNumber(
                  addhanshuiheji,
                  it.han_shui_he_ji_
                );
                data["addjine"] = this.addNumber(addjine, it.jin_e_);
                data["addshuie"] = this.addNumber(addshuie, it.shui_e_);
                addhanshuiheji = data["addhanshuiheji"];
                addjine = data["addjine"];
                addshuie = data["addshuie"];
              });
              data["addhanshuiheji"] = this_.toDecimal2(data["addhanshuiheji"]);
              data["addjine"] = this_.toDecimal2(data["addjine"]);
              data["addshuie"] = this_.toDecimal2(data["addshuie"]);
              this_.list.push(data);
              addhanshuiheji = 0;
              addjine = 0;
              addshuie = 0;
              console.log(this_.list)
              console.log(this_.list);
            });
          });
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
table {
  border-collapse: collapse;
  font-family: "微软雅黑";
  background: #fff;
  font-size: 12px;
}
.title{
  border: 1px solid #fff;
}
.title-sub{
  font-size: 12px;
  border-left: 1px solid #fff;
  border-right: 1px solid #fff;
}
span{
  font-weight: 500;
}
td {
  text-align: center;
  padding: 2px;
}
thead > tr > th {
  text-align: center;
  font-weight: 500;

}
.el-dialog__body {
  word-break: none;
}
.count td {
  text-align: right;
  padding: 4px 0px;
  padding-right: 15px;
}
.bottom {
  margin-top: 15px;
}
.bottom {
  margin-top: 15px;
  border-bottom: 1px solid #fff;
  border-left: 1px solid #fff;
  border-right: 1px solid #fff;
}
</style>
