<template>
  <div class="content-detail">
    <div class="content-main">
      <!-- <div id="outInDepotEntrust"></div> -->
      <wtsOne
        v-show="contentPage == 1"
        ref="wtsOne"
        @goToPage="goToPage"
        @handleSave="handleSave"
      ></wtsOne>
      <wtsTwo
        v-show="contentPage == 2"
        ref="wtsTwo"
        :wtfmc="wtfmc"
        :jyrq="jyrq"
        :lsxz="lsxz"
        :jyfx="jyfx"
        :newFlag="newFlag"
        @goToSpage="goToSpage"
        @doAudit="doAudit"
        @handleSave="handleSave"
        @handleSubmit="handleSubmit"
        @goTopage3="goTopage3"
        @doSeal="doSeal"
      ></wtsTwo>
      <wtsThree
        v-show="contentPage == 3"
        :jyfx="jyfx"
        ref="wtsThree"
        :wtfmc="wtfmc"
        :jyrq="jyrq"
        :lsxz="lsxz"
        :zlzbPz="zlzbPz"
        :goodsList="goodsList"
        :newFlag="newFlag"
        @doAudit="doAudit"
        @goToSpage="goToSpage"
        @handleSave="handleSave"
        @handleSubmit="handleSubmit"
        @doSeal="doSeal"
      ></wtsThree>
    </div>
  </div>
</template>

<script>
import {
  insert,
  update,
  detail,
  update2NextNode,
} from "@/api/transaction/tradeEntrust/outInDepotEntrust/index.js";
import { fileInfo } from "@/api/transaction/registrationFee/transactionSignup/index";
import { getNowDateZw, getNowDate, dateTimeFormat } from "@/utils/index";
import { FileApi } from "@/api/index";
import wtsOne from "./wtsOne.vue";
import wtsTwo from "./wtsTwo.vue";
import wtsThree from "./wtsThree.vue";

export default {
  components: {
    wtsOne,
    wtsTwo,
    wtsThree,
  },

  data() {
    return {
      wtfmc: "",
      jyrq: "",
      lsxz: "",
      jyfx: "",
      goodsList: [],
      contentPage: 1,
      jyfxArr: { 采购: "0", 销售: "1", 0: "采购", 1: "销售" },
      zlzbPz: "小麦",
      newFlag: false,
    };
  },
  created() {
    this.getDetail();
  },
  mounted() {},
  methods: {
    getDetail() {
      if (this.$route.query.wtbm) {
        detail(this.$route.query.wtbm).then((res) => {
          if (res.infoCode == 200) {
            let wtsOneData = res.data.entryAndExitOrderVO;
            wtsOneData.bzqDate =
              wtsOneData.bzqFirstDate && wtsOneData.bzqLastDate
                ? [wtsOneData.bzqFirstDate, wtsOneData.bzqLastDate]
                : [];

            wtsOneData.qyzz = wtsOneData.qyzz.split(",");
            wtsOneData.lscd = wtsOneData.lscd.split(",");
            wtsOneData.jlfs = wtsOneData.jlfs.split(",");
            wtsOneData.rkyq = wtsOneData.rkyq.split(",");

            wtsOneData.jyfx =
              wtsOneData.jyfx == "0,1"
                ? ["采购", "销售"]
                : [this.jyfxArr[wtsOneData.jyfx]];
            if (wtsOneData.wtfqz) {
              if (wtsOneData.wtfqz.indexOf("newSeal") == -1) {
                this.$nextTick(() => {
                  this.$ShowSeal("outInDepotEntrust", 1);
                  this.$SetSealData(wtsOneData.wtfqz, 0, "lingdao", 1);
                });
              } else {
                this.$nextTick(() => {
                  this.$signatureOnload(
                    "outInDepotEntrust",
                    wtsOneData.wtfqz.substring(7)
                  );
                });
              }
            }
            if (wtsOneData.wtfqz) {
              if (wtsOneData.wtfqz.indexOf("newSeal") == -1) {
                this.$nextTick(() => {
                  this.$ShowSeal("outInDepotEntrust1", 1);
                  this.$SetSealData(wtsOneData.wtfqz, 0, "lingdao", 1);
                });
              } else {
                this.$nextTick(() => {
                  this.$signatureOnload(
                    "outInDepotEntrust1",
                    wtsOneData.wtfqz.substring(7)
                  );
                });
              }
            }
            if (wtsOneData.wtfqz) {
              if (wtsOneData.wtfqz.indexOf("newSeal") == -1) {
                this.$nextTick(() => {
                  this.$ShowSeal("outInDepotEntrust2", 1);
                  this.$SetSealData(wtsOneData.wtfqz, 0, "lingdao", 1);
                });
              } else {
                this.$nextTick(() => {
                  this.$signatureOnload(
                    "outInDepotEntrust2",
                    wtsOneData.wtfqz.substring(7)
                  );
                });
              }
            }
            if (res.data.hyList) {
              res.data.hyList.map((item) => {
                item.detailList.map((item1) => {
                  // if (Array.isArray(item1.scnd)) {
                  item1.scnd ? (item1.scnd = item1.scnd.split(",")) : "";
                  // }
                });
              });
            }

            if (res.data.apList) {
              res.data.apList.map((item) => {
                item.detailList.map((item1) => {
                  // if (Array.isArray(item1.scnd)) {
                  item1.scnd ? (item1.scnd = item1.scnd.split(",")) : "";
                  // }
                });
              });
            }

            wtsOneData.goodsDOList.map((item) => {
              item.scnd ? (item.scnd = item.scnd.split(",")) : "";
			  item.lscd = item.lscd.split(",");
            });
            this.$refs.wtsOne.fromData = wtsOneData;
            this.$refs.wtsTwo.formData.detailList = res.data.apList;
            this.$refs.wtsTwo.formData.entryAndExitOrderVO =
              res.data.entryAndExitOrderVO;
            this.$refs.wtsThree.formData.detailList = res.data.hyList
              ? res.data.hyList
              : [];
            this.$refs.wtsThree.formData.entryAndExitOrderVO =
              res.data.entryAndExitOrderVO;
            this.getAudit();
          }
        });
      }
    },
    getAudit() {
      if (this.$route.query.state == 5) {
        if (this.$route.query.zt == "91000400") {
          this.$refs.wtsTwo.formData.entryAndExitOrderVO.slrq = getNowDate();
          this.$refs.wtsTwo.formData.entryAndExitOrderVO.slr =
            sessionStorage.getItem("userName");
          this.$refs.wtsTwo.formData.entryAndExitOrderVO.slrshjg = 1;

          this.$refs.wtsThree.formData.entryAndExitOrderVO.slrq = getNowDate();
          this.$refs.wtsThree.formData.entryAndExitOrderVO.slr =
            sessionStorage.getItem("userName");
          this.$refs.wtsThree.formData.entryAndExitOrderVO.slrshjg = 1;
        } else if (this.$route.query.zt == "91000500") {
          this.$refs.wtsTwo.formData.entryAndExitOrderVO.jyzxshrq =
            getNowDate();
          this.$refs.wtsTwo.formData.entryAndExitOrderVO.jyzxshr =
            sessionStorage.getItem("userName");
          this.$refs.wtsTwo.formData.entryAndExitOrderVO.jyzxshrshjg = 1;

          this.$refs.wtsThree.formData.entryAndExitOrderVO.jyzxshrq =
            getNowDate();
          this.$refs.wtsThree.formData.entryAndExitOrderVO.jyzxshr =
            sessionStorage.getItem("userName");
          this.$refs.wtsThree.formData.entryAndExitOrderVO.jyzxshrshjg = 1;
        }
      }
    },
    goToPage() {
      console.log(this.$refs.wtsOne.fromData, "fromData");
      let goodsDOList = this.$refs.wtsOne.fromData.goodsDOList;
      this.wtfmc = this.$refs.wtsOne.fromData.wtfmc;
      this.jyrq = getNowDateZw(this.$refs.wtsOne.fromData.jyrq);
      this.goodsList = this.$refs.wtsOne.fromData.goodsDOList;
      this.zlzbPz = this.$refs.wtsOne.fromData.goodsDOList[0].pzmc;
      this.lsxz = this.$refs.wtsOne.fromData.lsxzbm == 2 ? "区级" : "市级";
      this.jyfx = this.$refs.wtsOne.fromData.jyfx.toString();
      let deWeightThree = () => {
        let map = new Map();
        for (let item of goodsDOList) {
          if (!map.has(item.pzbm)) {
            map.set(item.pzbm, item);
          }
        }
        return [...map.values()];
      };
      let pzList = deWeightThree();
      this.$refs.wtsTwo.pzOptions = pzList;
      this.$refs.wtsThree.pzOptions = pzList;
      console.log(pzList, "pzList");

      if (
        // this.lsxz == "区级" ||
        this.zlzbPz.indexOf("米") > -1 ||
        this.zlzbPz.indexOf("粉") > -1 ||
        this.zlzbPz.indexOf("成品") > -1
      ) {
        this.newFlag = true;
      }

      if (this.$refs.wtsOne.fromData.jyfx.toString() == "销售") {
        this.contentPage = 3;
      } else {
        this.contentPage = 2;
      }
    },
    goToSpage(state) {
      // console.log(this.$refs.wtsOne.fromData, "fromData");
      if (this.$refs.wtsOne.fromData.jyfx == "采购,销售" && state) {
        this.contentPage = 2;
      } else {
        this.contentPage = 1;
      }
    },
    goTopage3() {
      let goodsDOList = this.$refs.wtsOne.fromData.goodsDOList;
      let deWeightThree = () => {
        let map = new Map();
        for (let item of goodsDOList) {
          if (!map.has(item.pzbm)) {
            map.set(item.pzbm, item);
          }
        }
        return [...map.values()];
      };
      let pzList = deWeightThree();
      this.$refs.wtsThree.pzOptions = pzList;
      this.contentPage = 3;
    },
    goToSpage3() {
      this.contentPage = 2;
    },
    handleSave(type) {
      // console.log(this.$refs.wtsOne.fromData, "fromData");
      // console.log(this.$refs.wtsTwo.formData, "wtsTwo");
      let wtsOneData = this.$refs.wtsOne.fromData;
      let wtsTwoData = this.$refs.wtsTwo.formData;
      let wtsThreeData = this.$refs.wtsThree.formData;
      wtsOneData.bzqFirstDate =
        wtsOneData.bzqDate && wtsOneData.bzqDate.length > 1
          ? wtsOneData.bzqDate[0]
          : "";
      wtsOneData.bzqLastDate =
        wtsOneData.bzqDate && wtsOneData.bzqDate.length > 1
          ? wtsOneData.bzqDate[1]
          : "";
      wtsOneData.qyzz = wtsOneData.qyzz ? wtsOneData.qyzz.toString() : "";
      wtsOneData.lscd = wtsOneData.lscd ? wtsOneData.lscd.toString() : "";
      wtsOneData.jlfs = wtsOneData.jlfs ? wtsOneData.jlfs.toString() : "";
      wtsOneData.rkyq = wtsOneData.rkyq ? wtsOneData.rkyq.toString() : "";

      wtsOneData.jyfxmc =
        wtsOneData.jyfx && wtsOneData.jyfx.length > 1
          ? "采购,销售"
          : wtsOneData.jyfx.toString();
      wtsOneData.jyfx =
        wtsOneData.jyfx && wtsOneData.jyfx.length > 1
          ? "0,1"
          : this.jyfxArr[wtsOneData.jyfx.toString()];

      wtsOneData.zt = "91000001";
      wtsOneData.ztmc = "待提交";
      let data = {
        entryAndExitOrderVO: wtsOneData,
      };
      if (type == 2) {
        data.apList = wtsTwoData.detailList;
      }
      if (type == 3) {
        data.hyList = wtsThreeData.detailList;
      }
      if (type == 23) {
        data.apList = wtsTwoData.detailList;
        data.hyList = wtsThreeData.detailList;
      }

      if (data.hyList) {
        data.hyList.map((item) => {
          item.detailList.map((item1) => {
            if (Array.isArray(item1.scnd)) {
              item1.scnd = item1.scnd.toString();
            }
          });
        });
      }
      if (data.apList) {
        data.apList.map((item) => {
          item.detailList.map((item1) => {
            if (Array.isArray(item1.scnd)) {
              item1.scnd = item1.scnd.toString();
            }
			item1.cd=''
			var arr=[]
			var arr2=[]
			data.entryAndExitOrderVO.goodsDOList.map((val) =>{
			  if(item1.pzbm ==val.pzbm){
			    arr2=arr2.concat(val.lscd)
			    arr=Array.from(new Set(arr2))
			    item1.cd=arr.toString()
			  }
			})
          });
        });
      }

      data.entryAndExitOrderVO.goodsDOList.map((item) => {
        item.scnd = item.scnd.toString();
		item.lscd=item.lscd.toString()
      });
      console.log(data, "data");
      if (this.$route.query.state == 0) {
        this.insertTodo(data, "保存成功!");
      } else {
        this.updateTodo(data, "保存成功!");
      }
    },
    handleSubmit(type) {
      // console.log(this.$refs.wtsOne.fromData, "fromData");
      // console.log(this.$refs.wtsTwo.formData, "wtsTwo");
      let wtsOneData = this.$refs.wtsOne.fromData;
      let wtsTwoData = this.$refs.wtsTwo.formData;
      let wtsThreeData = this.$refs.wtsThree.formData;
      wtsOneData.bzqFirstDate =
        wtsOneData.bzqDate && wtsOneData.bzqDate.length > 1
          ? wtsOneData.bzqDate[0]
          : "";
      wtsOneData.bzqLastDate =
        wtsOneData.bzqDate && wtsOneData.bzqDate.length > 1
          ? wtsOneData.bzqDate[1]
          : "";
      wtsOneData.qyzz = wtsOneData.qyzz ? wtsOneData.qyzz.toString() : "";
      wtsOneData.lscd = wtsOneData.lscd ? wtsOneData.lscd.toString() : "";
      wtsOneData.jlfs = wtsOneData.jlfs ? wtsOneData.jlfs.toString() : "";
      wtsOneData.rkyq = wtsOneData.rkyq ? wtsOneData.rkyq.toString() : "";

      wtsOneData.jyfxmc =
        wtsOneData.jyfx.length > 1 ? "采购,销售" : wtsOneData.jyfx.toString();
      wtsOneData.jyfx =
        wtsOneData.jyfx.length > 1
          ? "0,1"
          : this.jyfxArr[wtsOneData.jyfx.toString()];

      wtsOneData.zt = "91000300";
      wtsOneData.ztmc = "待盖章";
      let data = {
        entryAndExitOrderVO: wtsOneData,
      };
      if (type == 2) {
        data.apList = wtsTwoData.detailList;
      }
      if (type == 3) {
        data.hyList = wtsThreeData.detailList;
      }
      if (type == 23) {
        data.apList = wtsTwoData.detailList;
        data.hyList = wtsThreeData.detailList;
      }
      if (data.hyList) {
        data.hyList.map((item) => {
          item.detailList.map((item1) => {
            if (Array.isArray(item1.scnd)) {
              item1.scnd = item1.scnd.toString();
            }
          });
        });
      }
      if (data.apList) {
        data.apList.map((item) => {
          item.detailList.map((item1) => {
            if (Array.isArray(item1.scnd)) {
              item1.scnd = item1.scnd.toString();
            }
			item1.cd=''
			var arr=[]
			var arr2=[]
			data.entryAndExitOrderVO.goodsDOList.map((val) =>{
			  if(item1.pzbm ==val.pzbm){
			    arr2=arr2.concat(val.lscd)
			    arr=Array.from(new Set(arr2))
			    item1.cd=arr.toString()
			  }
			})
          });
        });
      }

      data.entryAndExitOrderVO.goodsDOList.map((item) => {
        item.scnd = item.scnd.toString();
		item.lscd=item.lscd.toString()
      });
      console.log(data, "data");
      if (this.$route.query.state == 0) {
        this.insertTodo(data, "提交成功!");
      } else {
        this.updateTodo(data, "提交成功!");
      }
    },
    insertTodo(data, mes) {
      insert(data).then((res) => {
        if (res.infoCode == 200) {
          this.$message({ type: "success", message: mes });
          this.$router.push({ path: "index" });
        }
      });
    },
    updateTodo(data, mes) {
      update(data).then((res) => {
        if (res.infoCode == 200) {
          this.$message({ type: "success", message: mes });
          this.$router.push({ path: "index" });
        }
      });
    },
    doSeal(data, mes) {
      update2NextNode(data)
        .then((res) => {
          if (res.infoCode == "200") {
            this.$message({
              message: mes + "成功！",
              type: "success",
            });
          } else {
            this.$message({
              message: mes + "失败！",
              type: "error",
            });
            this.$refs.wtsTwo.downTzFlag = true;
          }
        })
        .catch((err) => {
          this.$refs.wtsTwo.downTzFlag = true;
        });
    },
    doAudit(data, mes) {
      update2NextNode(data)
        .then((res) => {
          if (res.infoCode == "200") {
            this.$message({
              message: mes + "成功！",
              type: "success",
            });
            this.$router.push({ path: "index" });
          } else {
            this.$message({
              message: mes + "失败！",
              type: "error",
            });
          }
        })
        .catch((err) => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.content-detail {
  // width: 99%;

  border: 1px solid rgb(244, 245, 248);
}
.content-main {
  margin: 10px;
  padding: 20px;
  background-color: white;
}
.btn-footer {
  text-align: center;
  margin: 20px;
}
</style>
<style>
/* #outInDepotEntrust {
  position: relative;
}

#outInDepotEntrust > div {
  top: -17px !important;
  left: 295px !important;
} */
</style>