<template>
  <div class="h100">
    <van-popup
      id="bmpPopup"
      v-model="show"
      show-action
      closeable
      @close="bmpClose"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <van-nav-bar title="审批人列表"/>
      <van-search
        v-model="bmpSearch"
        placeholder="请输入工号姓名"
        show-action
        @search="onSearchBmp"
        class="hightSearch"
      >
        <i slot="left-icon" class="fa fa-search"></i>
        <template slot="action">
          <div class="font14 float-l common" @click="onSearchBmp">搜索</div>
        </template>
      </van-search>
      <search-result :result-num="bmpNum"></search-result>
      <van-pull-refresh class="list" v-model="isRefreshing" @refresh="onRefresh" v-if="newBmp">
        <van-list
          v-model="isLoading"
          :finished="finished"
          :finished-text="finishedText"
          :error.sync="error"
          error-text="请求失败，点击重新加载"
          @load="onLoad"
        >
          <van-radio-group v-model="bmpCkecked">
            <van-cell-group>
              <van-cell
                clickable
                @click="itemClick(item)"
                v-for="(item,index) in BmpList"
                :key="index"
              >
                <template #title>
                  <span class="empuid">{{item.empid}}</span>
                  <span>{{item.cnname}}</span>
                </template>
                <template #right-icon>
                  <van-radio :name="item"/>
                </template>
              </van-cell>
            </van-cell-group>
          </van-radio-group>
        </van-list>
      </van-pull-refresh>

      <van-radio-group v-model="bmpCkecked" v-if="!newBmp">
        <van-cell-group class="list" v-show="init">
          <van-cell clickable @click="itemClick(item)" v-for="(item,index) in BmpList" :key="index">
            <template #title>
              <span class="empuid">{{item.EMPUID}}</span>
              <span>{{item.CNNAME}}</span>
            </template>
            <template #right-icon>
              <van-radio :name="item"/>
            </template>
          </van-cell>
        </van-cell-group>
        <div class="no-more" v-show="!init">未查询到数据</div>
      </van-radio-group>
    </van-popup>
  </div>
</template>

<script>
import SearchResult from "@/components/SearchResult";
import { searchBpmUser, newSearchBpmUser } from "@/api/clientprofile";
export default {
  name: "BmpUser",
  components: { SearchResult },
  data() {
    return {
      newBmp: false,
      show: false,
      bmpSearch: "",
      bmpNum: "0",
      bmpCkecked: "",
      BmpList: [],
      pagerNum: 1,
      pagerSize: 10,
      isLoading: false,
      isRefreshing: false,
      loading: false,
      finished: false,
      finishedText: "没有更多了",
      error: false,
      init: false
    };
  },
  computed: {
    linkInfo() {
      return this.$store.state.linkInfo;
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    value(val) {
      this.show = val;
    }
  },
  created() {
    if (
      this.linkInfo.sysCompanyName == "上药控股有限公司" ||
      this.linkInfo.sysCompanyName == "上药康德乐罗达（上海）医药有限公司" ||
      this.linkInfo.sysCompanyName == "上药科泽（上海）医药有限公司" ||
      this.linkInfo.sysCompanyName == "上药闵行（上海）医药有限公司(合并)"
    ) {
      this.newBmp = false;
    } else {
      this.newBmp = true;
    }
  },
  methods: {
    onSearchBmp() {
      this.onRefresh();
    },
    getMainData() {
      if (this.newBmp) {
        let param = {
          attr: {
            username: this.bmpSearch,
            pageNo: this.pagerNum,
            pageSize: this.pagerSize
          }
        };
        newSearchBpmUser(param)
          .then(rt => {
            this.finished = false;
            if (rt.status == 200) {
              if (rt.data.total == 0) {
                if (this.pagerNum == 1) {
                  this.finishedText = "未查询到数据";
                  this.bmpNum = "0";
                  this.BmpList = [];
                }
                this.finished = true;
              } else {
                this.finishedText = "没有更多了";
                this.bmpNum = rt.data.total + "";
                if (this.pagerNum == 1) {
                  this.BmpList = rt.data.taskInfos;
                } else {
                  this.BmpList = this.BmpList.concat(rt.data.taskInfos);
                }
                if (rt.data.length < this.pagerSize || this.bmpNum == this.BmpList.length) {
                  this.finished = true;
                } else {
                  this.pagerNum++;
                }
              }
            } else {
              this.error = true;
            }
            this.isLoading = false;
            this.isRefreshing = false;
          })
          .catch(e => {
            console.log(e);
            this.isLoading = false;
            this.isRefreshing = false;
            this.error = true;
          });

      } else {
        if (!this.bmpSearch) {
          this.$toast("请输入工号姓名！");
        } else {
          let param = {
            attr: {
              searchText: this.bmpSearch
            }
          };
          searchBpmUser(param).then(rt => {
            if (rt.code == 1) {
              if(rt.userList == ''){
                this.init = false
              } else {
                this.BmpList = rt.userList;
                this.bmpNum = rt.userList.length + "";
                this.init = true
              }
            } else {
              this.$toast("查询失败");
            }
          });
        }
      }
    },
    onRefresh() {
      this.pagerNum = 1;
      this.getMainData();
    },
    onLoad() {
      this.getMainData();
    },
    itemClick(item) {
      this.show = false;
      this.bmpCkecked = item;
      this.bmpUser = item.CNNAME;
    },
    bmpClose(data) {
      this.$emit("bmpUserClose", false);
      this.$emit("getBmpUser", this.bmpCkecked);
    }
  }
};
</script>

<style lang="scss" scoped>
#bmpPopup {
  .empuid {
    margin-right: 20px;
  }
  background-color: $bc2;
  .van-nav-bar__title {
    color: $txtBlack;
    font-weight: 600;
    font-size: 15px;
  }
  .content {
    padding: 10px 14px;
  }
  .van-cell-group {
    .van-cell {
      padding: 18px;
    }
  }
  .list {
    height: calc(100vh - 147px);
    overflow: scroll;
  }
}
</style>
