import history from '../../utils/history';
import { getHospitalDistrictList } from '../select-hospital-area/service';
import { getPatientList } from '../edit-patient/service';
Component({
  data: {
    hospitalList: [],
    showPerson: false,
    personList: [],
    personItem: {},
    hospitalName: '',
    componentExtInfo: {}
  },
  props: {
    componentData: {}
  },

  didMount() {
    this.setData({
      componentExtInfo: this.props.componentData.componentExtInfo
    });
    this.getHospitalList();
    this.getPatientLists();
  },

  methods: {
    async getPatientLists() {
      const personList = await getPatientList();
      this.setData({
        personList
      });
    },

    async getHospitalList() {
      const hospitalList = await getHospitalDistrictList();
      this.setData({
        hospitalList
      });
    },

    async handleToBook(e) {
      const {
        hospitalDistrictId,
        name: hospitalName
      } = e.target.dataset.item;
      const {
        personList,
        componentExtInfo
      } = this.data;
      this.setData({
        hospitalDistrictId,
        hospitalName
      }); // 判就诊人列表三种情况

      if (personList.length === 0) {
        // 没有就诊人信息
        my.confirm({
          content: '您还未添加就诊人，无法进行核酸预约',
          confirmButtonText: '去新增',
          cancelButtonText: '取消',
          success: ({
            confirm
          }) => {
            if (confirm) {
              history.push({
                title: '添加就诊人',
                pageType: 'edit-patient'
              });
            }
          }
        });
      } else if (personList.length === 1) {
        this.setData({
          personItem: personList[0]
        }); // 有唯一就诊人（区分本人和其他人）

        history.push({
          query: {
            hospitalName,
            hospitalDistrictId,
            personItem: JSON.stringify(personList[0]),
            componentExtInfo: JSON.stringify(componentExtInfo)
          },
          title: '选择项目',
          pageType: 'hospital-project'
        });
      } else {
        // 选择就诊人
        this.setData({
          showPerson: true
        });
      }
    },

    onClosePerson() {
      this.setData({
        showPerson: false
      });
    },

    onPersonChange(item, index) {
      this.setData({
        personItem: item
      });
      history.push({
        query: {
          hospitalName: this.data.hospitalName,
          hospitalDistrictId: this.data.hospitalDistrictId,
          personIndex: index,
          personItem: JSON.stringify(item),
          componentExtInfo: JSON.stringify(this.data.componentExtInfo)
        },
        title: '选择项目',
        pageType: 'hospital-project'
      });
    }

  }
});