<!--
 * @Description: file content
 * @Version: 2.0
 * @Author: ljl
 * @Date: 2022-06-16 09:49:18
 * @LastEditors: ljl
 * @LastEditTime: 2022-12-12 13:43:36
 * @FilePath: \zyypt-ssb-vue_alipay\src\views\prescriptionDetails.vue
-->
<template>
  <div>
    <header>
      <div class="center">
        <div class="avatarImg">
          <img src="../assets/images/man.png" alt="" v-if="sex == 1" />
          <img src="../assets/images/woman.png" alt="" v-else-if="sex == 2" />
          <img src="../assets/images/avatar.png" alt="" v-else />
        </div>
        <div class="avatarInfo">
          <div class="name">
            <span>{{ info.name }}</span>
            <span @click="eye()" v-if="eyeLock"
              ><img src="../assets/images/openEye.png" alt=""
            /></span>
            <span @click="eye()" v-else
              ><img src="../assets/images/closeEye.png" alt=""
            /></span>
          </div>
        </div>
      </div>
      <div class="bottom">
        <div class="bottomInfo">
          <div class="info">
            <span><img src="../assets/images/hospital.png" alt="" /></span>
            <span>{{ $route.query.YLJGMC }}</span>
          </div>
          <div class="info">
            <span><img src="../assets/images/date.png" alt="" /></span>
            <span class="date">{{ getDate($route.query.KFRQ) }}</span>
          </div>
        </div>
      </div>
    </header>
    <div id="main">
      <van-tabs
        v-model="tabId"
        title-active-color="#8b614d"
        color="#8b614d"
        title-inactive-color="#c4c4c4"
        background="#fff"
        line-width="70px"
        line-height="3px"
        duration="0.2"
        sticky
      >
        <van-tab title="处方状态">
          <div class="ge"></div>
          <van-loading type="spinner" class="load" vertical v-show="presShow"
            >加载中...</van-loading
          >
          <div
            v-if="prescriptionStatus.JLS == 0"
            class="nodata"
            v-show="!presShow"
          >
            暂无数据
          </div>
          <div class="content" v-else v-show="!presShow">
            <div v-for="(item1, index1) in MXXQ" :key="index1">
              <div
                class="outDiv1"
                v-for="(item2, index2) in item1.children"
                :key="index2"
              >
                <div class="datetime">
                  <p
                    class="date"
                    :style="index1 == 0 && index2 == 0 ? 'color: #000' : ''"
                    v-if="index2 == 0 && item1.date"
                  >
                    {{ item1.date ? getDate(item1.date) : "" }}
                  </p>
                  <p
                    class="time"
                    :style="index1 == 0 && index2 == 0 ? 'color: #7a7a7a' : ''"
                  >
                    {{ item2.FSSJ ? getTime(item2.FSSJ) : "-" }}
                  </p>
                </div>
                <div class="lian">
                  <img
                    src="../assets/images/quan.png"
                    alt=""
                    style="width: 18px"
                    v-if="index1 == 0 && index2 == 0"
                  />
                  <img src="../assets/images/quan2.png" alt="" v-else />
                  <div
                    v-if="
                      !(
                        index1 == MXXQ.length - 1 &&
                        index2 == item1.children.length - 1
                      )
                    "
                    class="line"
                    :style="index1 == 0 && index2 == 0 ? 'top: 18px' : ''"
                  ></div>
                </div>
                <div
                  class="status"
                  :style="
                    index1 == 0 && index2 == 0
                      ? 'background: #faf8f3; color: #8b614d'
                      : ''
                  "
                >
                  <span v-if="item2.CLZT == 0">待处理(已收费)</span>
                  <span v-else-if="item2.CLZT == 1">已审方</span>
                  <span v-else-if="item2.CLZT == 2">代煎机构已确认</span>
                  <span v-else-if="item2.CLZT == 5">已配方</span>
                  <span v-else-if="item2.CLZT == 6">已浸泡</span>
                  <span v-else-if="item2.CLZT == 7">已煎煮</span>
                  <span v-else-if="item2.CLZT == 8">已包装</span>
                  <span v-else-if="item2.CLZT == 9">已快递</span>
                  <span v-else-if="item2.CLZT == 10">已发车</span>
                  <span v-else-if="item2.CLZT == 11">已签收</span>
                  <span v-else-if="item2.CLZT == 12">代煎企业退回</span>
                  <span v-else-if="item2.CLZT == 13">医疗机构退回</span>
                  <span v-else-if="item2.CLZT == 99">处方信息处理出现异常</span>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
        <van-tab title="物流信息">
          <div class="ge"></div>
          <van-loading type="spinner" class="load" vertical v-show="logShow"
            >加载中...</van-loading
          >
          <div v-if="logisticsInfo.JLS == 0" class="nodata" v-show="!logShow">
            暂无数据
          </div>
          <div class="content" v-else v-show="!logShow">
            <div
              class="outDiv2"
              v-for="(item, index) in logisticsInfo.MXXQ"
              :key="index"
            >
              <div class="lian">
                <img
                  src="../assets/images/quan.png"
                  alt=""
                  style="width: 18px"
                  v-if="index == 0"
                />
                <img src="../assets/images/quan2.png" alt="" v-else />
                <div
                  class="line"
                  :style="index == 0 ? 'top: 18px' : ''"
                  v-if="index != logisticsInfo.JLS - 1"
                ></div>
              </div>
              <div class="info" :style="index == 0 ? 'color: #000' : ''">
                <div class="datetime">
                  <span>{{ item.FSSJ ? getDateTime(item.FSSJ) : "-" }}</span>
                </div>
                <div class="status">
                  <span>{{ item.JDMS }}</span>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
        <van-tab title="处方内容">
          <div class="ge"></div>
          <van-loading type="spinner" class="load" vertical v-show="precShow"
            >加载中...</van-loading
          >
          <div
            v-if="prescriptionContent.JLS == 0"
            class="nodata"
            v-show="!precShow"
          >
            暂无数据
          </div>
          <div class="outDiv3 content" v-else v-show="!precShow">
            <p class="title">
              处方总帖数： <span>{{ CFTS }}</span
              >，单帖处方内容如下
            </p>
            <div class="drugList">
              <div class="listHead">
                <div style="width: 100%">药品名称</div>
                <!-- <div style="width: 50%">药品名称</div>
                <div style="width: 50%">产地</div> -->
              </div>
              <van-list
                v-model="loading"
                :finished="finished"
                @load="onLoad"
                finished-text="没有更多了"
              >
                <div
                  class="listBody"
                  v-for="(item, index) in prescriptionContentMXXQ"
                  :key="index"
                >
                  <!-- <div class="name">{{ item.YPMC }}</div>
                  <div class="address">{{ item.CD ? item.CD : "-" }}</div> -->
                  <!-- <div class="jiantou">
                    <img src="../assets/images/open.png" alt="" />
                  </div> -->
                  <div class="name" @click="goDrugTrace1(item)">
                    <span>{{ item.YPMC1 }}</span>
                    <img src="../assets/images/syIcon.png" class="drugIcon" alt="" v-if="item.YPSYBM1 && item.YPSYBM1 != -1"/>
                  </div>
                  <div class="name" @click="goDrugTrace2(item)">
                    <span>{{ item.YPMC2 }}</span>
                    <img src="../assets/images/syIcon.png" class="drugIcon" alt="" v-if="item.YPSYBM2 && item.YPSYBM2 != -1"/>
                  </div>
                </div>
              </van-list>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import { Tab, Tabs, List, Loading } from "vant";
Vue.use(Tab).use(Tabs).use(List).use(Loading);
import { getDate, getTime, getDateTime } from "@/mixins/format";
import { eye } from "@/mixins/eye";
import { getCookies } from "@/utils/auth";
import { setParamLog,getAlipayService } from "@/api/index";
export default {
  name: "PrescriptionDetails",
  mixins: [getDate(), getTime(), getDateTime(), eye()],
  data() {
    return {
      sex: "",
      tabId: 0,
      MXXQ: [],
      prescriptionContentMXXQ: [],
      loading: false,
      finished: false,
      presShow: false,
      logShow: false,
      precShow: false,
      CFTS: 0,
      prescriptionStatus: {
        JLS: 0,
        MXXQ: [],
      },
      logisticsInfo: {
        JLS: 0,
        MXXQ: [],
      },
      prescriptionContent: {
        JLS: 0,
        MXXQ: [],
      },
      strArray:[],
      MXXQArray:[]
    };
  },
  created() {
    let query = {
      YNCFBH: this.$route.query.YNCFBH,
      YLJGDM: this.$route.query.YLJGDM,
      KFRQ: this.$route.query.KFRQ,
    };
    this.CFTS = this.$route.query.CFTS
    this.presShow = true;
    this.logShow = true;
    this.precShow = true;
    setParamLog({type:'alipay',service:'WX002',jgdm:query.YLJGDM,zjhm:this.$route.query.zjhm})
    getAlipayService({ Service: "WX002", ...query }).then((res) => {
      let prescriptionStatus = res.data.MXXQ;
      this.MXXQ = this.toTree(prescriptionStatus);
      // this.MXXQ = this.toTree(this.prescriptionStatus.MXXQ);
      this.prescriptionStatus = res.data;
      this.presShow = false;
    });
    setParamLog({type:'alipay',service:'WX003'})
    getAlipayService({ Service: "WX003", ...query }).then((res) => {
      this.logisticsInfo = res.data;
      this.logShow = false;
    });
    setParamLog({type:'alipay',service:'WX004'})
    getAlipayService({ Service: "WX004", ...query }).then((res) => {
      res.data.MXXQ.forEach(item1=>{
        this.strArray.forEach(item2=>{
          if(item2.YPSYBM && item1.YPYBDM == item2.YPYBDM){
            item1.YPSYBM = item2.YPSYBM
          }
        })
      })
      // this.prescriptionContent = res.data;
      // this.prescriptionContentMXXQ = res.data.MXXQ.slice(0, 5);
      let prescriptionContentMXXQ = this.toArr(res.data.MXXQ);
      this.prescriptionContent = {
        // JLS: Math.ceil(res.data.JLS / 2),
        JLS: res.data.JLS,
        MXXQ: prescriptionContentMXXQ,
      };
      this.prescriptionContentMXXQ = prescriptionContentMXXQ.slice(0, 5);
      this.precShow = false;
    });
    setParamLog({type:'alipay',service:'WX006'})
    getAlipayService({ Service: "WX006", ...query }).then((res) => {
      this.MXXQArray = res.data.MXXQ
      var str1 = JSON.parse(res.data.MXXQ[0].mxxq)
      var str2 = JSON.parse(res.data.MXXQ[1].mxxq)
      this.strArray = str1.concat(str2)
    });

    if (this.$route.query.sex) {
      this.sex = this.$route.query.sex.trim();
    } else {
      this.sex = "-";
    }
  },
  mounted() {
    if (getCookies("relativeInfo")) {
      this.info = JSON.parse(getCookies("relativeInfo"));
    } else {
      this.info = JSON.parse(getCookies("myInfo"));
    }
    this.eye("refresh");
    let tabId = getCookies("tabId");
    if (!tabId) {
      this.tabId = 0;
    } else {
      this.tabId = Number(tabId);
    }
  },
  methods: {
    toArr(oldArr) {
      let newArr = [];
      if (oldArr.length > 0) {
        oldArr.forEach((item, index, arr) => {
          if ((index + 1) % 2 == 1) {
            let obj = {
              YPMC1: item.YPMC,
              YPMC2: arr[index + 1] ? arr[index + 1].YPMC : "",
              YPSYBM1: item.YPSYBM,
              YPSYBM2: arr[index + 1] ? arr[index + 1].YPSYBM : "",
            };
            newArr.push(obj);
          }
        });
      }
      return newArr;
    },
    toTree(list) {
      var newobj = {},
        newArr = [];
      for (var i = 0; i < list.length; i++) {
        var item = list[i];
        if (!newobj[item["FSSJ"].substring(0, 8)]) {
          newobj[item["FSSJ"].substring(0, 8)] = newArr.push(
            item["FSSJ"].substring(0, 8)
          );
        }
      }
      let outArr = [];
      newArr.forEach((ele) => {
        let insideArr = [];
        list.forEach((item) => {
          if (item.FSSJ) {
            if (ele.indexOf(item.FSSJ.substring(0, 8)) != -1) {
              item.FSSJ = item.FSSJ.substring(8, 14);
              insideArr.push(item);
            }
          }
          if (!ele) {
            if (!item.FSSJ) {
              insideArr.push(item);
            }
          }
        });
        if (ele != -1) {
          outArr.push({
            date: ele,
            children: insideArr,
          });
        }
      });
      return outArr;
    },
    getYPCDM(YPSYBM){
      if(this.MXXQArray[0].mxxq.indexOf(YPSYBM) != '-1'){
        return this.MXXQArray[0].ypcdm
      }
      if(this.MXXQArray[1].mxxq.indexOf(YPSYBM) != '-1'){
        return this.MXXQArray[1].ypcdm
      }
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        for (let i = 0; i < 5; i++) {
          if (
            this.prescriptionContentMXXQ.length >=
            this.prescriptionContent.MXXQ.length
          ) {
            this.finished = true;
          } else {
            this.prescriptionContentMXXQ.push(
              this.prescriptionContent.MXXQ[this.prescriptionContentMXXQ.length]
            );
          }
          // console.log(
          //   this.prescriptionContent.MXXQ[this.prescriptionContentMXXQ.length]
          // );
        }

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
      }, 1000);
    },

    goDrugTrace1(item) {
      let ypcdm = this.getYPCDM(item.YPSYBM1)
      if(item.YPSYBM1 && item.YPSYBM1 != -1){
        this.$router.push({
          name: "DrugTrace",
          query: {
            YPSYBM: item.YPSYBM1,
            YPCDM:ypcdm,
          },
        });
      }
    },
    goDrugTrace2(item) {
      let ypcdm = this.getYPCDM(item.YPSYBM2)
      if(item.YPSYBM2 && item.YPSYBM2 != -1){
        this.$router.push({
          name: "DrugTrace",
          query: {
            YPSYBM: item.YPSYBM2,
            YPCDM:ypcdm,
          },
        });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
header {
  width: 100%;
  // height: 270px;
  background-color: #cbb486;
  border-bottom-left-radius: 15px;
  border-bottom-right-radius: 15px;
  padding-bottom: 10px;
}
.center {
  // height: 80px;
  display: flex;
  padding: 10px 15px;
  position: relative;
}
.avatarImg {
  height: 100%;
  img {
    width: 70px;
    height: 70px;
    border-radius: 50%;
    // margin-top: 15px;
  }
}
.avatarInfo {
  .name {
    color: #fff;
    font-size: 24px;
    height: 50px;
    display: flex;
    align-items: center;
    span {
      margin-left: 12px;
      font-weight: bolder;
    }
    img {
      width: 20px;
    }
  }
}
.bottom {
  padding: 0 15px;
  height: 100px;
  font-size: 14px;
  text-align: left;
  .bottomInfo {
    background: rgba(111, 82, 38, 0.2);
    border-radius: 8px;
    width: 100%;
    height: 100%;
    .info {
      color: #fff;
      font-family: "PingFang SC-Regular";
      height: 50%;
      display: flex;
      align-items: center;
      padding-left: 12px;
      img {
        width: 20px;
        margin-right: 8px;
      }
      .date {
        // font-family: "Avenir-Roman";
        font-family: "Avenir-Medium";
      }
    }
  }
}
::v-deep #main {
  font-family: "PingFang SC-Regular";
  .van-tab {
    font-size: 18px;
  }
  .van-tabs--line .van-tabs__wrap {
    border-bottom: 1px solid #dcdee2;
  }
  .load {
    margin-top: 50px;
  }
  .content {
    padding: 0 15px;
    padding-top: 20px;
  }
  .nodata {
    color: #7a7a7a;
    margin-top: 30px;
  }
  .ge {
    background: #f8f8f8;
    height: 10px;
  }
  .outDiv1 {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 70px;
    .datetime {
      width: 90px;
      text-align: right;
      color: #c4c4c4;
      .date {
        font-family: "Avenir-Medium";
        margin-bottom: 10px;
      }
      .time {
        // font-family: "Avenir-Roman";
        font-family: "Avenir-Medium";
        font-size: 14px;
      }
    }
    img {
      width: 16px;
    }
    .status {
      width: 180px;
      background: #f6f6f6;
      color: #c4c4c4;
      border-radius: 4px;
      padding: 10px 0;
    }
    .lian {
      width: 30px;
      position: relative;
    }
    .line {
      width: 2px;
      height: 55px;
      background: #e7e7e7;
      position: absolute;
      top: 16px;
      left: 50%;
      transform: translateX(-50%);
    }
  }
  .outDiv2 {
    display: flex;
    // height: 70px;
    padding-bottom: 20px;
    position: relative;
    img {
      width: 16px;
    }
    .lian {
      width: 30px;
      // position: relative;
      margin-left: 10px;
    }
    .line {
      width: 1px;
      // height: 75px;
      height: calc(100% - 16px);
      background: #e7e7e7;
      position: absolute;
      top: 16px;
      left: 24px;
      // left: 50%;
      // transform: translateX(-50%);
    }
    .info {
      text-align: left;
      margin-left: 20px;
      color: #999999;
      width: calc(100% - 50px);
    }
    .datetime {
      margin-bottom: 10px;
      height: 18px;
      line-height: 18px;
      font-family: "Avenir-Medium";
    }
    .status {
      font-size: 14px;
      line-height: 20px;
      font-family: "PingFang SC-Regular";
    }
  }

  .outDiv3 {
    text-align: left;
    color: #999999;
    font-size: 14px;
    font-family: "PingFang SC-Regular";
    .title {
      span {
        color: #fc7a73;
      }
    }
    .drugList {
      background: #ffffff;
      box-shadow: 0px 0px 10px 1px rgba(184, 160, 115, 0.3);
      border-radius: 4px;
      margin-top: 20px;
      font-size: 14px;
      .listHead {
        display: flex;
        height: 60px;
        border-bottom: 1px solid #e7e7e7;
        align-items: center;
        text-align: center;
        font-size: 16px;
      }
      .listBody {
        display: flex;
        height: 60px;
        border-bottom: 1px solid #e7e7e7;
        align-items: center;
        text-align: center;
        // .name {
        //   width: 50%;
        //   color: #fc7a73;
        // }
        .name {
          text-align: center;
          width: 50%;
          color: #000000;
          display: flex;
          justify-content: center;
          height: 100%;
          flex-direction: column;
          height: 100%;
          line-height: 22px;
          padding: 0 10px;
          position: relative;
          // span:nth-child(1) {
          //   color: #fc7a73;
          // }
        }
        .name:nth-child(1) {
          border-right: 1px solid #e7e7e7;
        }
        .drugIcon{
          width: 20px;
          position: absolute;
          right: 3px;
          top: 3px;
        }
        .address {
          width: 50%;
          color: #000000;
        }
        .jantou {
          width: 10%;
        }
        img {
          width: 18px;
        }
      }
    }
  }
}
</style>