<template>
  <div class="bgColor">
    <div slot="content" class="scroll-wrapper" style="height:100%;">
      <div class="demo" style="height:100%;">
        <div class="header">耗材列表</div>
        <div class="seatchHeader">
          <div class="search fl">
            <input
              class="searchInp"
              type="text"
              v-model="title"
              placeholder="可通过名称、型号品牌进行检索支持拼音"
              @input="searchData()"
            />
          </div>
        </div>
        <div class="scroll-list-wrap">
          <cube-scroll
            ref="scroll"
            :data="items"
            :options="options"
            @pulling-down="onPullingDown"
            @pulling-up="onPullingUp" style="height:100%;"
          >
            <div class="conentBox">
              <div
                class="conent"
                v-for="item in items"
                @click="showModal(item)"
                :key="item.id"
              >
                <div class="head">
                  <p>
                    <span>{{ item.name }}</span>
                    <span>{{ item.brandModel }}</span>
                  </p>
                </div>
                <div class="center">
                  <p>
                    <span class="fl"></span>
                    <span class="grayFont overflowEllipsis2">{{item.endPrice}}元</span>
                  </p>
                </div>
              </div>
            </div>
            <template v-if="customPullDown" slot="pulldown" slot-scope="props">
              <div
                v-if="props.pullDownRefresh"
                class="cube-pulldown-wrapper"
                :style="props.pullDownStyle"
              >
                <div
                  v-show="props.beforePullDown"
                  class="before-trigger"
                  :style="{ paddingTop: props.bubbleY + 'px' }"
                >
                  <span
                    :class="{
                      rotate: props.bubbleY > pullDownRefreshThreshold - 40
                    }"
                    >↓</span
                  >
                </div>
                <div class="after-trigger" v-show="!props.beforePullDown">
                  <div v-show="props.isPullingDown" class="loading">
                    <cube-loading></cube-loading>
                  </div>
                  <div v-show="!props.isPullingDown" class="text">
                    <span class="refresh-text">更新成功</span>
                  </div>
                </div>
              </div>
            </template>
            <div class="wushuju" v-show="!loadShow && !items.length">
              <img src="./../../static/images/wushuju.svg" alt />
              <p>暂无数据</p>
            </div>
          </cube-scroll>
        </div>
      </div>
    </div>
    <load-ing v-show="loadShow"></load-ing>
    <!-- 弹窗 -->
    <showModel
      v-if="models.disjunctor"
      :title="models.title"
      :icon="models.icon"
      :disjunctor="models.disjunctor"
      @ok="ok"
      @cancel="cancel"
      :operate="models.operate"
      selectName="hc_add"
      :data="hcData"
    ></showModel>
  </div>
</template>
<script>
import { debounce } from 'lodash';
import showModel from "../components/showModel/showModel.vue";
import CubePage from "../components/cube-page.vue";
import SwitchOption from "../components/switch-option";
import InputOption from "../components/input-option";
import SelectOption from "../components/select-option";
import LoadIng from "./loading.vue";
export default {
  data() {
    return {
      hcData: {},
      modelsData: {}, //点击弹窗的那一项数据
      // 弹窗model
      models: {
        disjunctor: false
      },
      valConfig: JSON.parse(localStorage.getItem("valConfig")) - 0, //报修主体
      items: [],
      pullDownRefresh: true,
      pullDownRefreshThreshold: 60,
      pullDownRefreshStop: 40,
      pullDownRefreshTxt: "Refresh success",
      pullUpLoad: true,
      pullUpLoadThreshold: 0,
      pullUpLoadMoreTxt: "加载更多",
      pullUpLoadNoMoreTxt: "没有更多数据",
      customPullDown: true,
      title: "", //搜索内容
      // stateData: [
      //   {
      //     text: "全部",
      //     value: "all"
      //   },
      //   {
      //     text: "待处理",
      //     value: "todo"
      //   },
      //   {
      //     text: "处理过",
      //     value: "done"
      //   }
      // ],
      // stateValue: "todo",
      sum: 10,
      idx: 0,
      stateClass: "",
      loadShow: true,
    };
  },
  components: {
    CubePage,
    SwitchOption,
    InputOption,
    SelectOption,
    LoadIng,
    showModel,
  },
  computed: {
    options() {
      return {
        pullDownRefresh: this.pullDownRefreshObj,
        pullUpLoad: this.pullUpLoadObj,
        scrollbar: true
      };
    },
    pullDownRefreshObj: function() {
      return this.pullDownRefresh
        ? {
            threshold: parseInt(this.pullDownRefreshThreshold),
            txt: this.pullDownRefreshTxt
          }
        : false;
    },
    pullUpLoadObj: function() {
      return this.pullUpLoad
        ? {
            threshold: parseInt(this.pullUpLoadThreshold),
            txt: {
              more: this.pullUpLoadMoreTxt,
              noMore: this.pullUpLoadNoMoreTxt
            }
          }
        : false;
    }
  },
  methods: {
    //确定
    ok(data) {
      console.log(data);
      const reg = /(^[1-9][0-9]*$)/;
      if(!reg.test(data.consumablesNum)){
          this.$createDialog({
            type: "alert",
            title: "请输入大于0的正整数!",
            icon: "cubeic-warn"
          }).show();
          return;
      }
      const toast = this.$createToast({
        txt: "Loading...",
        mask: true
      });
      toast.show();
      this.$http
        .post("service/bpm/data/addSummaryDoc", {
            "consumableList": [
                {
                    "consumablesId": data.hc.id,
                    "consumablesNum": data.consumablesNum
                }
            ],
            "summaryId": this.$route.params.summaryId
        })
        .then(res => {
          toast.hide();
          this.models.disjunctor = false;
          if (res.data.status == 200) {
            this.$createDialog({
              type: "alert",
              title: "操作成功",
              icon: "cubeic-right",
              onConfirm: (e, promptValue) => {
                this.loadShow = false;
                this.items = [];
                this.idx = 0;
                this.$router.push({
                  path: `/summaryOrder/${this.$route.params.incidentId}/${this.$route.params.processInstanceId}/${this.$route.params.dutyId}`
                })
              }
            }).show();
          } else {
            this.$createDialog({
              type: "alert",
              title: "系统错误,请稍后再试!",
              icon: "cubeic-wrong",
              onConfirm: (e, promptValue) => {
                this.loadShow = false;
                this.items = [];
                this.idx = 0;
                this.$router.push({
                  path: `/summaryOrder/${this.$route.params.incidentId}/${this.$route.params.processInstanceId}/${this.$route.params.dutyId}`
                })
              }
            }).show();
          }
        });
    },
    //取消
    cancel() {
      this.models.disjunctor = false;
    },
    // 弹窗
    showModal(item) {
      this.modelsData = item;
      this.hcData = {
        hc: item,
        incidentId: this.$route.params.incidentId,
        summaryId: this.$route.params.summaryId,
      }
      this.models = {
        disjunctor: true,
        title: "提示",
        icon: "warn",
        operate: {
          ok: "确定",
          cancel: "取消"
        }
      };
    },
    getParamsState() {
      console.log(this.$route.params.state)
      if (this.$route.params.state) {
        console.log(this.$route.params.state);
        this.stateValue = this.$route.params.state;
      }
    },
    showPicker() {
      if (!this.picker) {
        this.picker = this.$createPicker({
          title: "",
          data: [this.stateData],
          selectedIndex: [1],
          onSelect: this.selectHandle,
          onCancel: this.cancelHandle
        });
      }
      this.picker.show();
    },
    selectHandle(selectedVal, selectedIndex, selectedText) {
      console.log(selectedVal, selectedIndex, selectedText);

      this.stateValue = selectedVal[0];
      this.items = [];
      this.getData();
    },
    cancelHandle() {},
    searchData:debounce(function () {
      // 处理输入变化事件的逻辑
      this.loadShow = true;
      this.items=[]
      this.getData()
    }, 300),
    // 获取列表
    getData() {
      console.log(this.$route, 777)
      console.log(this.stateValue);
      let title = this.title;
      let postData = {
        idx: this.idx,
        sum: this.sum,
        consumable: {
          keyWord: this.title || '',
          dutyDTO: {id: this.$route.params.dutyId},
          showZero: true,
        }
      };
      this.$http
        .post(
          "service/bpm/data/fetchDataList/consumable",
          postData
        )
        .then((res) => {
          if(res.status == 200){
            if(title == this.title){
              if (res.data.list.length > 0) {
                if (this.idx) {
                  this.items = this.items.concat(res.data.list);
                } else {
                  this.items = res.data.list;
                }
              } else {
                this.$refs.scroll.forceUpdate();
              }
              this.loadShow = false;
            }
          }else{
            this.$createDialog({
              type: "alert",
              title: "操作失败",
              content: "请求失败!",
              icon: "cubeic-wrong"
            }).show();
          }
        });
    },
    onPullingDown() {
      // 模拟更新数据
      this.idx = 0;
      this.sum = 10;
      this.getData();
    },
    onPullingUp() {
      this.idx = this.idx + 1;
      this.getData();
    },
  },
  mounted() {
    // this.$route.params.data
    // this.getParamsState();
    setTimeout(()=>{
      this.getData();
    },0)
  },
};
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.scroll-list-wrap {
  height: 90vh;
  border-radius: 5px;
  transform: rotate(0deg); // fix 子元素超出边框圆角部分不隐藏的问题
  overflow: hidden;
}

.foods-wrapper {
  .food-item {
    display: flex;

    /* padding: 18px */
    /* border-bottom: 1px solid rgba(7, 17, 27, 0.1) */
    /* border-top: 1px solid rgba(7, 17, 27, 0.1)!important */
    &:last-child {
      border-none();
      margin-bottom: 0;
    }

    .icon {
      flex: 0 0 57px;
      margin-right: 10px;
    }

    .food-content {
      flex: 1;

      .name {
        margin: 2px 0 8px 0;
        height: 14px;
        line-height: 14px;
        font-size: 14px;
        color: rgb(7, 17, 27);
      }

      .description, .extra {
        line-height: 10px;
        font-size: 10px;
        color: rgb(147, 153, 159);
      }

      .description {
        line-height: 12px;
        margin-bottom: 8px;
      }

      .extra {
        .count {
          margin-right: 12px;
        }
      }

      .price {
        font-weight: 700;
        line-height: 24px;

        .now {
          margin-right: 8px;
          font-size: 14px;
          color: rgb(240, 20, 20);
        }

        .old {
          text-decoration: line-through;
          font-size: 10px;
          color: rgb(147, 153, 159);
        }
      }

      .cartcontrol-wrapper {
        position: absolute;
        right: 0;
        bottom: 12px;

        .scroll-wrapper {
          .cube-pulldown-wrapper {
            .before-trigger {
              font-size: 30px;
              line-height: 30px;
              align-self: flex-end;

              span {
                display: inline-block;
                transition: all 0.3s;
                color: #666;

                &.rotate {
                  transform: rotate(180deg);
                }
              }
            }

            .after-trigger {
              .refresh-text {
                color: grey;
              }
            }
          }
        }
      }
    }
  }
}
</style>
<style lang="less" scoped>
.bgColor {
  background-color: white;
}
.demo{
  display:flex;
  flex-direction:column;
}
.food-item {
  border-top: 0.16rem rgb(238, 238, 238) solid;
}
.food-content {
  border-top: 0.01rem rgb(223, 222, 222) solid;
  border-bottom: 0.01rem rgb(223, 222, 222) solid;
}
.title {
  display: flex;
  justify-content: space-between;
  height: 0.7rem;
  line-height: 0.7rem;
  border-top: 0.01rem rgb(223, 222, 222) solid;
  padding: 0 0.24rem;
  border-bottom: 0.01rem rgb(223, 222, 222) solid;
}
.title i {
  font-size: 0.32rem;
  color: #005395;
}
.title div:nth-child(1) {
  font-size: 0.32rem;
}
.title div:nth-child(1) span {
  display: inline-block;
  margin-left: 0.08rem;
}
.title div:nth-child(2) {
  padding: 0.05rem 0.3rem;
  /* border: 0.01rem rgb(0,0,255) solid; */
  border-radius: 16px;
  /* color: rgb(0,0,255); */
  height: 0.25rem;
  line-height: 0.25rem;
  margin-top: 0.15rem;
  font-size: 0.24rem;
}
.weishouli {
  color: #991f00;
  border: 0.01rem #991f00 solid;
  background-color: #f4e8e5;
}
.bushouli {
  color: #666666;
  background-color: #eeeeee;
  border: 0.01rem #666666 solid;
}
.chulizhong {
  color: #003057;
  background-color: #e5eaee;
  border: 0.01rem #003057 solid;
}
.daipingjia {
  color: #825b00;
  background-color: #f2eee5;
  border: 0.01rem #825b00 solid;
}
.yijiejue {
  color: #40a19c;
  background-color: #ebf5f5;
  border: 0.01rem #40a19c solid;
}
.yichehui {
  color: #48a843;
  background-color: #ecf6ec;
  border: 0.01rem #48a843 solid;
}
.timeBox {
  display: flex;
  justify-content: space-between;
  font-size: 0.24rem;
  color: #999999;
  border-top: 0.01rem rgb(223, 222, 222) solid;
  height: 0.7rem;
  line-height: 0.7rem;
  padding: 0 0.24rem 0 0.64rem;
}
.wushuju {
  margin-top: 2.4rem;
  text-align: center;
  color: #999;
}
.wushuju img {
  width: 5.12rem;
  height: 2.84rem;
}
.demo {
  .header {
    width: 100%;
    height: 0.88rem;
    line-height: 0.88rem;
    text-align: center;
    color: #fff;
    font-size: 0.37rem;
    background: linear-gradient(#2e2f32, #414246);
    // position: fixed;
    // top: 0;
    // z-index: 6;
  }
  .seatchHeader {
    width: 100%;
    background-color: #eee;
    padding: 0.16rem;
    // overflow: hidden;
    // position: fixed;
    // top: 0.88rem;
    // z-index: 6;
    box-sizing: border-box;
    .search {
      width: 100%;
      height: 0.56rem;
      font-size: 0.26rem;
      color: #b2b2b2;
      box-sizing: border-box;
      .searchInp {
        text-align: center;
        background-color: #fff;
        width: 100%;
        height: 100%;
        border-radius: 0.09rem;
        border: none;
        &:focus {
          outline: none;
        }
      }
    }
    .selectBtn {
      width: 1.2rem;
      height: 0.56rem;
      font-size: 0.32rem;
      color: #005395;
      border-color: #005395;
      background-color: #fff;
      border-radius: 0.09rem;
      &:focus {
        outline: none;
      }
    }
  }
}
.conentBox {
  height: 100%;
  width: 100%;
  .conent {
    font-size: 0.32rem;
    font-weight: 400;
    line-height: 0.45rem;
    border-bottom: 0.16rem solid #e5e5e5;
    .head {
      border-bottom: 0.01rem solid #e6e6e6;
      p {
        padding: 0.24rem 0.48rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        i {
          color: #00559d;
        }
        .btn {
          float: right;
        }
      }
    }
    .center {
      color: #333333;
      border-bottom: 0.01rem solid #e6e6e6;
      padding: 0.24rem 0.48rem;
      p {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .grayFont {
          color: #666;
        }
      }
    }
    .bottom {
      overflow: hidden;
      line-height: 0.86rem;
      border-bottom: 0.01rem solid #e6e6e6;
      font-size: 0.24rem;
      color: #999;
      padding: 0 0.24rem 0 0.48rem;
    }
  }
}
</style>
