<template>
  <van-popup
    v-show="show"
    v-model="show"
    closeable
    round
    position="bottom"
    :style="{ height: '560px',overflow:hidden }"
    @close="close"
  >
    <div class="select-hospital">
      <div class="select-hospital-title">
        选择检测机构
      </div>
      <div class="select-hospital-input">
        <van-search
          v-model="searchValue"
          shape="round"
          show-action
          background="#fff"
          placeholder="输入检测地点"
          @blur="getHospitalList(false)"
        >
          <template #action>
            <div @click="cancel">
              取消
            </div>
          </template>
        </van-search>
      </div>
      <div class="select-hospital-list">
        <van-list
          v-if="list && list.length >0"
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
        >
          <van-cell
            v-for="(item,index) in list"
            :key="index"
            class="list-item"
            @click="selectItem(item)"
          >
            <div
              class="list-item-name"
              v-html="highLight(item.hospitalName, searchValue)"
            />
            <!-- <div class="list-item-name">
              {{ item.hospitalName }}
            </div> -->
            <div class="list-item-address">
              {{ item.provinceName }}{{ item.cityName }}{{ item.countyName }}{{ item.townName }}
            </div>
            <div
              v-if="hospitalId == item.hospitalId"
              class="list-item-icon"
            />
          </van-cell>
        </van-list>
        <div
          v-else
          class="select-hospital-nodata"
        >
          <img
            class="img"
            src="@/assets/images/nodata.png"
            alt=""
          >
          <div class="text">
            暂未搜索到相关检测点
          </div>
        </div>
      </div>
    </div>
  </van-popup>
</template>

<script>
import {highLight} from '@/utils/common';
import {getHospitalList} from './api';
export default {
  props: {
    hospitalId: {
      type: Number,
      default:() => null
    },
    show: {
      type: Boolean,
      default:() => false
    },
    projectEquityNo: {
      type: String,
      default:() => ''
    },
    externalData: {
      type: Object,
      default:() => {}
    }
  },
  data() {
    return {
      hospitalName: '',
      list: [],
      loading: false,
      finished: true,
      pageNum: 0,
      pageSize: 50,
      searchValue: '',
    };
  },
  watch: {
    externalData: {
      handler(nv) {
        console.log('externalData', nv);
        this.getHospitalList(false);
      },
      deep: true,
      immediate:true
    },
    projectEquityNo: {
      handler(nv) {
        console.log('projectEquityNo', nv);
        this.getHospitalList(false);
      },
      deep: true,
      immediate:true
    }
  },
  mounted() {

  },
  methods: {
    highLight(name, value) {
      return highLight(name, value);
    },
    // 获取机构列表
    getHospitalList(isLoad) {

      this.finished = false;
      this.pageNum = isLoad ? this.pageNum + 1 : 1;

      const data = {
        cityId: this.externalData.cityId,
        countyId: this.externalData.countyId,
        provinceId:this.externalData.provinceId,
        projectEquityNo:this.projectEquityNo,
        // pageNum: this.pageNum,
        // pageSize: this.pageSize,
        keyword: this.searchValue,
      };
      console.log(data);
      getHospitalList(data).then(res => {
        console.log('getHospitalList', res);

        this.list = res.data.data || [];
        this.finished = true;
        this.loading = false;
        const hospitalIdIndex = (this.list.length > 0) && this.list.findIndex(ele => ele.hospitalId === this.hospitalId);
        console.log('hospitalIdIndex', hospitalIdIndex);
        if (this.externalData.cityId && this.externalData.countyId && this.externalData.provinceId && hospitalIdIndex == -1) {
          this.$emit('clean');
        }
      });
    },
    selectItem(item) {
      console.log(item);
      this.$emit('change', item);
    },
    cancel() {
      this.finished = false;
      this.searchValue = '';
      this.pageNum = 0;
      this.onLoad();

    },
    onLoad() {
      this.loading = true;
      this.finished = false;
      this.getHospitalList(true);
    },
    close() {
      this.$emit('close', 'showselectHospital');
    }
  }
};
</script>

<style lang="scss" scoped>
.select-hospital{
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;

}
.select-hospital-title{
  position: absolute;
  width: 100%;
  top: 0;
  padding-top: 18px;
  padding-bottom: 21px;
  height: 18px;
  font-size: 18px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #02120F;
  line-height: 18px;
  text-align: center;
}
.select-hospital-input{
  position: absolute;
  width: calc(100% - 15px);
  top: 58px;
  padding-right: 15px;
  border-bottom: 1px solid #ebedf0;
  /deep/ .van-search__action{
    padding-left:  13px;
    padding-right: 0;
  }
}
.select-hospital-list{
  padding-top: 120px;
  height: calc(100% - 120px);
  overflow: hidden;
  // position: absolute;
  // top: 100px;
  // left: 0;
  // right: 0;
  // bottom: 0;
  // min-height: 450px;
  // height: 450px;
  .van-list{
    height: 100%;
    overflow: auto;
    .list-item{
      position: relative;
      padding-top: 14px;
      padding-bottom: 12px;
      &-name{
        height: 15px;
        font-size: 15px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #212121;
        line-height: 15px;
        margin-bottom: 12px;
      }
      &-address{
        height: 12px;
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999999;
        line-height: 12px
      }
      &-icon{
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 19px;
        height: 19px;
        background: url('~@/assets/images/icon-4.png') 0 0 / 100%
        100% no-repeat;
      }
    }
  }
  .select-hospital-nodata{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);

    .img{
      width: 155px;
      height: 85px;
      margin: 21px auto;
    }
    .text{
      height: 25px;
      font-size: 18px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #02120F;
      line-height: 25px;
    }
  }
}

</style>
