import { doctorInfo } from '../doctor-page/service';
Component({
  props: {
    doctorId: '',
    show: true,
    onClose: () => null
  },
  data: {
    contentShow: false,
    doctor: {
      doctorId: '',
      name: '',
      picUrl: '',
      level: '',
      description: '',
      specialty: '',
      departments: []
    }
  },

  didMount() {
    const {
      show
    } = this.props;
    if (show) this.init();
  },

  didUpdate({
    show: preShow
  }) {
    const {
      show
    } = this.props;

    if (preShow !== show) {
      if (show) {
        this.init();
      } else {
        this.setData({
          contentShow: false
        });
      }
    }
  },

  methods: {
    init() {
      my.showLoading({
        content: '加载中...'
      });
      doctorInfo({
        doctorId: this.props.doctorId
      }).then(res => {
        this.setData({
          doctor: res,
          contentShow: true
        });
      }).catch(() => {
        this.onClose();
      }).finally(() => {
        my.hideLoading();
      });
    },

    onClose() {
      this.props.onClose();
    }

  }
});