<template>
  <div class="bgColor">
    <div slot="content" class="scroll-wrapper">
      <div class="demo">
        <div class="header">事件列表</div>
        <cube-select
          class="stateSearch"
          v-model="searchType"
          :options="searchsType"
          @change="stateChange()"
        ></cube-select>
        <div class="scroll-list-wrap">
          <cube-scroll
            ref="scroll"
            :data="items"
            :options="options"
            @pulling-down="onPullingDown"
            @pulling-up="onPullingUp"
          >
            <div class="conentBox" v-if="!loadShow && items.length">
              <div
                class="conent"
                v-for="item in items"
                :key="item.id"
                @click="toIncidentDetails(item)"
              >
                <div class="head">
                  <p>
                    <i class="iconfont icon-zuixinbaoxiu newPapir"></i>
                    事件编号：{{ item.incidentsign }}
                    <span
                      :class="{
                        btn: true,
                        daipingjia: item.state.name == '待评价',
                        daijiedan:
                          item.state.name == '待接单' &&
                          item.handlerUser &&
                          !item.candidateGroups,
                        daiqiangdan:
                          item.state.name == '待接单' &&
                          !item.handlerUser &&
                          item.candidateGroups,
                        chulizhong: item.state.name == '处理中',
                        yijiejue: item.state.name == '已解决',
                        yiguanbi: item.state.name == '已关闭',
                        chongxinzhipai: item.state.name == '重新指派'
                      }"
                      >{{
                        item.state.name == "待接单"
                          ? item.handlerUser && !item.candidateGroups
                            ? "待接单"
                            : "待抢单"
                          : item.state.name
                      }}</span
                    >
                  </p>
                </div>
                <div class="center">
                  <p class="desc">
                    <span class="fl">事件描述：</span>
                    <span
                      class="grayFont overflowEllipsis2"
                      v-html="item.description"
                    ></span>
                  </p>
                  <p>
                    报修<span>{{ valConfig == 1 ? "人" : "科室" }}</span> ：
                    <span class="grayFont">{{ item.contacts }}</span>
                    <span @click.stop v-if="item.contactsInformation"
                      ><a :href="'tel:' + item.contactsInformation"
                        >(<i class="iconfont icon-shouji"></i
                        >{{ item.contactsInformation }})</a
                      ></span
                    >
                  </p>
                </div>
                <div class="bottom">
                  <span v-if="!item.place"></span>
                  <span
                    v-if="item.place"
                    style="width:70%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"
                    >{{ item.place.area.area }} {{ item.place.place }}
                    {{ item.houseNumber }}</span
                  >
                  <span>{{ item.acceptDate | timeFormat("MM-dd HH:mm") }}</span>
                </div>
                <div class="btns">
                  <cube-form-group
                    class="sub-btn"
                    v-show="
                      (item.state.id === 1544 || item.state.id === 1543) &&
                        isAssign &&
                        item.assignee
                    "
                  >
                    <cube-button @click.stop="assign(item)">指派</cube-button>
                  </cube-form-group>
                  <cube-form-group
                    class="sub-btn"
                    v-show="item.state.id === 1544"
                  >
                    <cube-button @click.stop="addHandlerLog(item)"
                      >新增处理日志</cube-button
                    >
                  </cube-form-group>
                </div>
              </div>
            </div>
            <template slot="pulldown" slot-scope="props">
              <div
                v-if="props.pullDownRefreshObj"
                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 - 80
                    }"
                    >↓</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-if="!loadShow && items.length === 0">
              <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="user"
    ></showModel>
    <!-- 新增处理日志弹窗 -->
    <showModel
      :title="models1.title"
      :icon="models1.icon"
      :disjunctor="models1.disjunctor"
      @ok="ok1"
      @cancel="cancel1"
      :operate="models1.operate"
      textName="textarea"
    ></showModel>
  </div>
</template>
<script>
import Vue from "vue";
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 { formatDate } from "./../components/js/date.js";
import LoadIng from "./../views/loading.vue";
export default {
  data() {
    return {
      modelsData: null, //点击弹窗的那一项数据
      modelsData1: null, //点击弹窗的那一项数据
      // 弹窗model
      models: {
        disjunctor: false
      },
      models1: {
        disjunctor: false
      },
      isAssign: false, //是否有权限指派
      loginUser: JSON.parse(localStorage.getItem("loginUser")),
      valConfig: JSON.parse(localStorage.getItem("valConfig")) - 0, //报修主体
      items: [],
      pullDownRefresh: true,
      pullDownRefreshThreshold: 60,
      pullDownRefreshStop: 40,
      pullDownRefreshTxt: "更新成功",
      pullUpLoad: true,
      pullUpLoadThreshold: 0,
      pullUpLoadMoreTxt: "加载更多",
      pullUpLoadNoMoreTxt: "没有更多数据",
      customPullDown: true,
      candidateGroups: "",
      searchsType: [
        {
          text: "全部",
          value: "userAll"
        },
        {
          text: "待处理",
          value: "todo"
        },
        {
          text: "处理过",
          value: "done"
        }
        // {
        //   text: "我创建",
        //   value: "create"
        // }
      ],
      searchType: "userAll",
      type: "all",
      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(selectUserId) {
      console.log(selectUserId);
      if (!selectUserId) {
        this.$createDialog({
          type: "alert",
          title: "请选择指派对象",
          icon: "cubeic-warn"
        }).show();
        return;
      }
      const toast = this.$createToast({
        txt: "Loading...",
        mask: true
      });
      toast.show();
      this.$http
        .post("service/bpm/bpm/delegateTask/" + this.modelsData.taskId, {
          assginee: selectUserId
        })
        .then(res => {
          toast.hide();
          this.models.disjunctor = false;
          if (res.status == 200) {
            this.$createDialog({
              type: "alert",
              title: "操作成功",
              icon: "cubeic-right",
              onConfirm: (e, promptValue) => {
                this.loadShow = true;
                this.items = [];
                this.idx = 0;
                this.getData();
              }
            }).show();
          } else {
            this.$createDialog({
              type: "alert",
              title: "系统错误,请稍后再试!",
              icon: "cubeic-wrong",
              onConfirm: (e, promptValue) => {
                this.loadShow = true;
                this.items = [];
                this.idx = 0;
                this.getData();
              }
            }).show();
          }
        });
    },
    //取消
    cancel() {
      this.models.disjunctor = false;
    },
    // 指派
    assign(item) {
      this.modelsData = item;
      this.models = {
        disjunctor: true,
        title: "指派对象",
        icon: "warn",
        operate: {
          ok: "确定",
          cancel: "取消"
        }
      };
    },
    //确定
    ok1(text) {
      console.log(text);
      if (!text.trim()) {
        this.$createDialog({
          type: "alert",
          title: "请填写处理日志",
          icon: "cubeic-warn"
        }).show();
        return;
      }
      const toast = this.$createToast({
        txt: "Loading...",
        mask: true
      });
      toast.show();
      this.$http
        .post("service/bpm/data/addData/operationLog", {
          operationLog: {
            opType: "handlerLog",
            opValue: text,
            extra1: this.modelsData1.id
          }
        })
        .then(res => {
          toast.hide();
          this.models1.disjunctor = false;
          if (res.status == 200) {
            this.$createDialog({
              type: "alert",
              title: "操作成功",
              icon: "cubeic-right",
              onConfirm: (e, promptValue) => {
                this.loadShow = true;
                this.items = [];
                this.idx = 0;
                this.getData();
              }
            }).show();
          } else {
            this.$createDialog({
              type: "alert",
              title: "系统错误,请稍后再试!",
              icon: "cubeic-wrong",
              onConfirm: (e, promptValue) => {
                this.loadShow = true;
                this.items = [];
                this.idx = 0;
                this.getData();
              }
            }).show();
          }
        });
    },
    //取消
    cancel1() {
      this.models1.disjunctor = false;
    },
    // 新增处理日志
    addHandlerLog(item) {
      this.modelsData1 = item;
      this.models1 = {
        disjunctor: true,
        title: "新增处理日志",
        icon: "warn",
        operate: {
          ok: "确定",
          cancel: "取消"
        }
      };
    },
    getParamsState() {
      if (this.$route.params.type) {
        this.type = this.$route.params.type;
        if (this.type === "todo") {
          this.searchType = "todo";
        } else if (this.type === "done") {
          this.searchType = "done";
        } else {
          this.searchType = "userAll";
        }
      }
    },
    stateChange() {
      var that = this;
      this.loadShow = true;
      that.items = [];
      that.idx = 0;
      that.sum = 10;
      that.getData();
    },
    toIncidentDetails(data) {
      console.log(1111111);
      console.log(data);
      var name = "";
      if (data.state.value == "pending") {
        if (data.handlerUser && !data.candidateGroups) {
          // 指派给人 接单
          name = "Order";
        } else if (data.candidateGroups && !data.handlerUser) {
          // 指派给组 抢单
          name = "GrabSheet";
        }
      } else if (data.state.value == "reassign") {
        name = "AgainAssign";
      } else if (data.state.value == "handler") {
        name = "Processing";
      } else if (data.state.value == "resolved") {
        name = "Solved";
      } else if (data.state.value == "close") {
        name = "Closed";
      }
      this.$router.push({
        name: name,
        params: {
          data: data
        }
      });
    },

    getData() {
      var that = this;
      let postData = {
        assignee: that.loginUser.id,
        candidateGroups: that.candidateGroups,
        searchType: that.searchType,
        idx: that.idx,
        sum: that.sum,
        incident: { temporary: true }
      };
      if (this.type === "today") {
        postData.incident.acceptDate =
          formatDate(new Date(), "yyyy-MM-dd") + " 00:00:00";
        postData.incident.acceptDateEnd =
          formatDate(new Date(), "yyyy-MM-dd") + " 23:59:59";
      }

      that.$http
        .post("service/bpm/bpm/fetchTask/bpm_incident", postData)
        .then(function(res) {
          if (res.data.data.length > 0) {
            for (var i = 0; i < res.data.data.length; i++) {
              res.data.data[i].createTime = formatDate(
                new Date(res.data.data[i].createTime),
                "yyyy-MM-dd hh:mm"
              );
            }
            if (that.idx) {
              that.items = that.items.concat(res.data.data);
            } else {
              that.items = res.data.data;
            }
          } else {
            that.pullUpLoad = false;
          }
          that.loadShow = false;
        });
    },
    onPullingDown() {
      var that = this;
      that.idx = 0;
      that.sum = 10;
      setTimeout(() => {
        that.getData();
      }, 1000);
    },
    onPullingUp() {
      var that = this;
      that.idx = that.idx + 1;
      that.getData();
    },
    updatePullDownRefresh(val) {
      this.pullDownRefresh = val;
    },
    updatePullDownRefreshThreshold(val) {
      this.pullDownRefreshThreshold = val;
    },
    updatePullDownRefreshTxt(val) {
      this.pullDownRefreshTxt = val;
    },
    updatePullUpLoad(val) {
      this.pullUpLoad = val;
    },
    updatePullUpLoadThreshold(val) {
      this.pullUpLoadThreshold = val;
    },
    updatePullUpLoadMoreTxt(val) {
      this.pullUpLoadMoreTxt = val;
    },
    updatePullUpLoadNoMoreTxt(val) {
      this.pullUpLoadNoMoreTxt = val;
    },
    updateCustomPullDown(val) {
      this.customPullDown = val;
    },
    rebuildScroll() {
      Vue.nextTick(() => {
        this.$refs.scroll.destroy();
        this.$refs.scroll.initScroll();
      });
    }
  },
  created() {
    var that = this;
    let menu = JSON.parse(localStorage.getItem("menu"));
    if (menu) {
      this.isAssign = menu.some(v => v.link == "shijianliebiao_assign");
    }
    that.loginUser.group.forEach(element => {
      that.candidateGroups += element.id + ",";
    });
    that.candidateGroups = that.candidateGroups.substring(
      0,
      that.candidateGroups.length - 1
    );
    this.getParamsState();
    this.loadShow = true;
    this.getData();

    localStorage.removeItem("modelData");
    // localStorage.removeItem("category");
    // localStorage.removeItem("categoryVal");
    localStorage.removeItem("referenceInfo");
    localStorage.removeItem("order");
    localStorage.removeItem("model");
    // localStorage.removeItem("selectedCategoryTxt");
  }
};
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.btns{
  display: flex;
  justify-content: space-between
  .sub-btn {
    margin:0 2px;
    flex:1;
    background: #ececec;
    .cube-btn {
      background-color: #005395 !important;
      border-radius: 8px
    }
  }
}
.scroll-list-wrap {
  height: calc(100vh - 2.72rem);
  border-radius: 5px;
  transform: rotate(0deg); // fix 子元素超出边框圆角部分不隐藏的问题
  overflow: hidden;
  padding-top: 1.76rem;
}

.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>
.cont {
  margin-top: 1.86rem;
}
.scroll-list-wrap {
  padding-top: 1.76rem;
}
.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;
}
.stateSearch {
  width: 100%;
  position: fixed;
  top: 0.88rem;
  z-index: 6;
}
.bgColor {
  background-color: white;
}
.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;
}
.cube-select {
  height: 0.88rem;
  line-height: 0.88rem;
  padding: 0 0.24rem;
  font-size: 0.36rem;
  color: #333333;
  border-bottom: 0.08rem solid #e5e5e5;
}
.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;
}
// .conent {
//   font-size: 0.28rem;
//   line-height: 0.39rem;
//   overflow: hidden;
//   display: -webkit-box;
//   -webkit-line-clamp: 2;
//   -webkit-box-orient: vertical;
//   word-break: break-all;
//   padding: 0 0.64rem;
//   margin: 0.24rem 0;
// }
.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;
}
.conentBox {
  width: 100%;
  height: 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;
        i {
          color: #00559d;
        }
        .btn {
          float: right;
        }
      }
    }
    .center {
      color: #333333;
      border-bottom: 0.01rem solid #e6e6e6;
      padding: 0.24rem 0.48rem;
      p {
        &.desc {
          overflow: hidden;
          max-height: 0.88rem;
        }
        .grayFont {
          overflow-x: scroll;
          color: #666;
        }
      }
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      overflow: hidden;
      height: 0.86rem;
      border-bottom: 0.01rem solid #e6e6e6;
      font-size: 0.24rem;
      color: #999;
      padding: 0 0.24rem 0 0.48rem;
    }
  }
}
</style>
