<template>
  <div>
    <van-popup
      v-model="postShow"
      class="more-search bg-gray"
      position="right"
      get-container="body"
    >
      <div class="title-layout font-bold font-16">订单来源</div>
      <search-result :result-num="searchNum"></search-result>
      <div class="w100 listHeight" ref="scrollRef">
        <van-pull-refresh
          v-model="isRefreshing"
          @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            :error.sync="error"
            :offset="10"
            @load="onLoad">
            <van-radio-group v-model="checked">
              <van-cell-group>
                <van-cell :title="titleInitial(item)" clickable @click="itemClick(item,index)" v-for="(item,index) in postDataList"
                          :key="index">
                  <template #right-icon>
                    <van-radio :name="item.ordtypeid" />
                  </template>
                </van-cell>
              </van-cell-group>
            </van-radio-group>
          </van-list>
        </van-pull-refresh>
      </div>
      <div class="action-bottom">
        <van-button type="default" @click="onCancelClick">取消</van-button>
        <van-button type="info" class="submit" @click="onConfirmClick">确定</van-button>
      </div>
    </van-popup>
  </div>
</template>

<script>
import {getOrderOrigin} from '../api/common'
import SearchResult from './SearchResult'

export default {
  name: 'SearchOrderOrigin',
  components: {SearchResult},
  data() {
    return {
      postShow: false,
      isRefreshing: false,
      loading: false,
      finished: false,
      error: false,
      searchNum: '0',
      currentPage: 1,
      pageSize: 99,
      postDataList: [],
      checked: '',
      choosedPost: {}
    }
  },
  computed: {
    linkInfo () {
      return this.$store.state.linkInfo
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    customer: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    value(val) {
      this.postShow = val
      if (val) {
        this.postDataList = []
        this.choosedPost = JSON.parse(JSON.stringify(this.customer))
        this.checked = !this.customer.ordtypeid ? '' : this.customer.ordtypeid
        console.log(this.checked)
        this.currentPage = 1
        this.onLoad()
      }
    },
    postShow (val) {
      if (!val) {
        this.$refs.scrollRef.scrollTop = 0
      }
      this.$emit('input', val)
    }
  },
  methods: {
    /// 标题拼接 <!-- 药厂码/（SAP码）名称 -->
    titleInitial(model) {
      let titleStr = model.ordtypename
      return titleStr
    },
    onCancelClick() {
      this.postShow = false
      this.$emit('cancleclick')
    },
    onConfirmClick() {
      this.postShow = false
      this.$emit('confirmclick', {
        page: 'orderOrigin',
        value: this.choosedPost
      })
    },
    itemClick(item, index) {
      this.checked = item.ordtypeid
      this.choosedPost = item
    },
    onRefresh() {
      // 刷新接口
      // this.postDataList = []
      this.currentPage = 1
      this.isRefreshing = true
      this.finished = false
      this.getPostData()
    },
    onLoad() {
      this.loading = true
      this.getPostData()
    },
    getPostData() {
      let _this = this
      let data = {
        // 'dbName': 'oms_real_shaphar',
        'dbName': _this.linkInfo.omsRealDb + '-' + _this.linkInfo.sysDbSource,
        'start': _this.currentPage,
        'length': _this.pageSize
      }
      getOrderOrigin(data).then(rt => {
        if (_this.currentPage === 1) {
          _this.searchNum = rt.data.length + ''
          this.postDataList = []
        }
        if (rt.code === 1 || rt.code === '1') {
          if (this.isRefreshing) {
            this.postDataList = rt.data
          }
          else if (_this.loading && (!!rt.data || rt.data.length > 0)) {
            this.postDataList = this.postDataList.concat(rt.data)
          }

          // 重置刷新提示
          _this.isRefreshing = false
          // 重置加载更多提示
          _this.loading = false
          // 若当前加载的页数没有满足每页的数量代表完全加载
          if (rt.data < 10) {
            _this.finished = true
          } else {
            // 加载成功后下一页自增1
            _this.currentPage++
          }
        } else {
          _this.$toast(rt.msg)
          // _this.$toast('列表获取失败')
          _this.isRefreshing = false
          _this.error = true
          _this.finished = true
        }
        // 加载状态结束
        _this.loading = false
      }).catch(e => {
        _this.$toast('列表获取失败')
        _this.isRefreshing = false
        _this.loading = false
        _this.error = true
        _this.finished = true
      })
    },
    /// 判断数值是否为空
    isJudgeStrNull(val) {
      if (val !== null && val !== undefined && val !== '') {
        return true
      }
      return false
    }
  }
}
</script>

<style lang="scss" scoped>
  .more-search {
    width: 75%;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  .title-layout {
    height: 45px;
    line-height: 60px;
    background: white;
    padding-left: 10px;
    font-weight: bold;
  }

  .listHeight {
    flex: 1;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .num-tip {
    padding: 10px;
  }

  .item-layout {
    height: 40px;
    line-height: 40px;
    background: white;
    border-bottom: #dedede 1px solid;
    padding-left: 10px;
  }

  .action-bottom {
    width: 100%;
    display: flex;
    button {
      flex: 1;
    }
  }

  .red-tips {
    color: #fb1c1c;
    font-weight: bold;
  }
</style>
