<template>
  <view style="height: 100%">
    <custom-nav-bar position="fixed" color="black" left-text="申请签约家庭医生" left-arrow />

    <!-- 重新定制封装，可抽离成数组的形式以方便复用，组件化，后续思考，此处逻辑可随时抽离成组件形式 -->
    <view class="form-list">
      <view class="form-item">
        <view class="label label-must">姓名</view>
        <input class="label-input" :value="form.name" @input="headleInput" data-name="name" placeholder="请输入姓名" />
      </view>
      <view class="form-item">
        <view class="label label-must">身份证号码</view>
        <input type="number" :value="form.idCardNo" @input="headleInput" data-name="idCardNo" class="label-input"
          placeholder="请输入身份证号码" />
      </view>
      <view class="form-item">
        <view class="label label-must">联系电话</view>
        <input type="number" :value="form.mobile" @input="headleInput" data-name="mobile" class="label-input"
          placeholder="请输入联系电话" />
      </view>

      <view class="form-item form-item-select">
        <view class="select-msg">家庭医生进网格专项服务正在逐步建设中，如暂未覆盖您居住的社区/村,请您耐心等待，感谢您的理解和支持。</view>
        <view class="label label-must">所在村/社区</view>
        <view class="label-select" @tap="onOpenVisible">
          <input :value="choiceObj.currentTile" disabled class="label-input" placeholder="请选择" />
          <image class="ic-xia" src="/static/packages/people/images/ic-xia.png"></image>
        </view>
      </view>

      <view class="form-item">
        <view class="label label-must">现住详址</view>
        <input type="number" :value="form.addressDetail" @input="headleInput" data-name="addressDetail"
          class="label-input" placeholder="请输入现住详址" />
      </view>

      <view class="form-item">
        <view class="label label-must">家属联系电话</view>
        <input type="number" :value="form.familyMobile" @input="headleInput" data-name="familyMobile"
          class="label-input" placeholder="请输入家属联系电话" />
      </view>

      <view class="form-item form-item-checkbox">
        <view class="label label-must2">
          <text class="label-title">人群类别</text>
          <view class="label-msg">（可多选）</view>
        </view>
        <view class="label-checkbox">
          <block v-for="(e, i) in getTypeList" :key="i">
            <text :class="'tag-bth ' + (e.isAction ? 'tag-bth-active' : '')" :data-item="e"
              @tap="handTabs">{{ e.label }}</text>
          </block>
        </view>
      </view>

      <view class="form-item form-item-checkbox">
        <view class="label label-must2">
          <text class="label-title">既往史</text>
          <view class="label-msg">请填写真实情况，没有填“无”</view>
        </view>
        <textarea :value="form.pastHistory" @input="headleInput" data-name="pastHistory" class="label-textarea"
          placeholder="请输入既往史" auto-height></textarea>
      </view>

      <view class="form-item form-item-checkbox">
        <view class="label label-must2">
          <text class="label-title">家族史</text>
          <view class="label-msg">请填写真实情况，没有填“无”</view>
        </view>
        <textarea :value="form.familyHistory" @input="headleInput" data-name="familyHistory" class="label-textarea"
          placeholder="请输入家族史" auto-height></textarea>
      </view>

      <view class="form-item form-item-checkbox">
        <view class="label label-must2">
          <text>上传照片</text>
        </view>
        <view class="label-image">
          <van-uploader @after-read="handAfterRead" :file-list="fileList" :deletable="true" max-count="9"
            @delete="handDeleteImg">
            <image class="label-image-camera" src="/static/images/icon9.png"></image>
          </van-uploader>
        </view>
        <view class="label-image-msg">为了让家庭医生更好的了解您的情况，您可以上传您的病历或其他信息</view>
      </view>

      <van-checkbox class="form-treaty" label-disabled :value="checked" shape="square" use-icon-slot
        @change="onCheckbox">
        <image class="ic-check" slot="icon"
          :src="checked ? '/static/images/ic-radio1.png' : '/static/images/ic-radio.png'" />
        <view class="checbox-text">
          本人承诺以上提交的信息真实无误并接受
          <view class="jump-to">《家庭医生团队签约服务协议书》</view>
          全部条款
        </view>
      </van-checkbox>

      <view class="form-button">
        <van-button v-if="checked" @click="handSubmit" block color="#FE3B53">提交申请</van-button>
        <van-button v-else disabled block color="#C1C1C1">提交申请</van-button>
      </view>
    </view>

    <!-- 社区选择组件 -->
    <wux-cascader :visible="isVisible" title="所在村/社区" :options="optionData" :safe-area-inset-bottom="true"
      :defaultFieldNames="defaultFieldNames" @close="onCloseVisible" @change="onChangeVisible" />

    <!-- 确认弹窗 -->
    <van-dialog use-slot :show="isSubmit" confirm-button-color="#0090FC" show-cancel-button @close="onClose"
      @confirm="patientCreateApi">
      <view class="dialog">
        <view>申请提交后将无法修改</view>
        <view>确认提交吗？</view>
      </view>
    </van-dialog>
  </view>
</template>

<script>
  // import wuxCascader from '../../../../lib/wux-weapp/cascader/index';
  // const app = getApp();
  // const {
  //   publicApi,
  //   peopleApi
  // } = app.globalData.api;
  import * as peopleApi from '@/api/peopleApi';
  import * as publicApi from '@/api/publicApi';
  export default {
    components: {
      // wuxCascader
    },
    data() {
      return {
        options: {},

        form: {
          name: '',
          idCardNo: '',
          mobile: '',
          addressDetail: '',
          familyMobile: '',
          pastHistory: '',
          familyHistory: ''
        },

        fileList: [],

        // 上传的图片
        // 社区选择组件 start
        isVisible: false,

        choiceObj: {
          currentTile: ''
        },

        optionData: [],

        defaultFieldNames: {
          // 选择默认显示字段
          label: 'name',
          value: 'id',
          children: 'children'
        },

        // 社区选择组件 start
        getTypeObj: {},

        // 当前选择
        // 人群类别
        getTypeList: [],

        checked: '',
        isSubmit: false,
        i: '',

        e: {
          isAction: false,
          label: ''
        }
      };
    },
    onLoad(options) {
      this.setData({
        options
      });
      this.getDictDataApi();
      this.deptTreeApi();
    },
    methods: {
      async deptTreeApi() {
        // 获取所在村/社区
        const res = await publicApi.deptTreeApi();
        if (res.data && res.data.length) {
          this.setData({
            optionData: res.data
          });
        }
      },

      async getDictDataApi() {
        const {
          data: {
            list = []
          }
        } = await publicApi.getDictDataApi({
          dictType: 'people_patient_crowd_type'
        });
        if (list.length) {
          list.forEach((e, i) => {
            e.isAction = i === 0;
            if (e.isAction) {
              this.setData({
                getTypeObj: e
              });
            }
          });
          this.setData({
            getTypeList: list
          });
        }
      },

      async patientCreateApi() {
        const res = await peopleApi.patientCreateApi(this.form);
        console.log(res);
      },

      handTabs(event) {
        const {
          item: {
            value
          }
        } = event.currentTarget.dataset;
        console.log(value);
        // this.data.getTypeList.forEach((e, i) => {
        //   this.data.getTypeList[i].isAction = e.value === value;
        // });
      },

      onCheckbox(event) {
        this.setData({
          checked: event.detail
        });
      },

      // 提交 - 开启确认弹窗
      handSubmit() {
        const params = {
          userId: uni.getStorageSync('userInfoData').userId,
          images: this.fileList.map((item) => {
            return item.url;
          }),
          orgId: this.choiceObj.currentValue,
          applyStatus: this.getTypeObj.value
        };
        this.setData({
          form: Object.assign(this.form, params)
        });
        console.log(this.form);
        this.setData({
          isSubmit: true
        });
      },

      onClose() {
        this.setData({
          isSubmit: false
        });
      },

      // 上传图片
      handAfterRead(event) {},

      // 删除图片
      handDeleteImg(event) {},

      // 提交 - 确认提交弹窗
      healedOrder() {},

      onOpenVisible() {
        this.setData({
          isVisible: true
        });
      },

      onCloseVisible() {
        this.setData({
          isVisible: false
        });
      },

      onChangeVisible(e) {
        const {
          options = [], value = []
        } = e.detail;
        this.setData({
          choiceObj: Object.assign({}, {
            currentTile: options[options.length - 1].name,
            currentValue: value[value.length - 1]
          })
        });
      },

      headleInput(event) {
        const {
          value
        } = event.detail;
        const {
          name
        } = event.currentTarget.dataset;
        this.form[name] = value;
      }
    }
  };
</script>
<style lang="less">
  .form-list {
    .form-item {
      padding: 42rpx 40rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      position: relative;

      &::before {
        content: '';
        position: absolute;
        left: 50%;
        bottom: 0;
        transform: translateX(-50%);
        width: 678rpx;
        height: 1px;
        background-color: #e1e1e1;
      }

      .label {
        position: relative;
        font-size: 28rpx;
        font-weight: 400;
        color: #333333;
        line-height: 28rpx;
        position: relative;
      }

      .label-must::before {
        content: '*';
        position: absolute;
        right: -16rpx;
        top: 0;
        font-size: 28rpx;
        font-weight: 400;
        color: #fe3b53;
        line-height: 32rpx;
      }
    }

    .form-item-select {
      position: relative;
      padding-top: 140rpx;

      .select-msg {
        position: absolute;
        top: 24rpx;
        left: 50%;
        width: 670rpx;
        transform: translateX(-50%);
        font-size: 26rpx;
        font-weight: 400;
        color: #ff7f34;
        line-height: 40rpx;
        text-align: justify;
      }
    }

    .form-item-checkbox {
      align-items: flex-start;
      flex-direction: column;

      .label-image-msg {
        font-size: 24rpx;
        font-weight: 400;
        color: #999999;
        line-height: 32rpx;
      }

      .label-must2 {
        width: 100%;
        display: flex;
        align-items: center;

        .label-title {
          position: relative;

          &::before {
            content: '*';
            position: absolute;
            right: -16rpx;
            top: 0;
            font-size: 28rpx;
            font-weight: 400;
            color: #fe3b53;
            line-height: 32rpx;
          }
        }

        .label-msg {
          margin-left: 27rpx;
          font-size: 24rpx;
          font-weight: 400;
          color: #999999;
          line-height: 24rpx;
        }
      }
    }
  }

  .label-input {
    flex: 1;
    text-align: right;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 32rpx;
    padding-left: 30rpx;
  }

  .label-select {
    flex: 1;
    display: flex;
    align-items: center;

    .label-is-select {}

    .ic-xia {
      margin-left: 27rpx;
      width: 32rpx;
      height: 32rpx;
    }
  }

  .label-checkbox {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .tag-bth {
      color: #333333;
      padding: 16rpx 32rpx;
      background: #f7faff;
      border-radius: 40rpx;
      border: 1px solid #c5dff4;
      font-size: 28rpx;
      font-weight: 400;
      line-height: 28rpx;
      margin-left: 16rpx;
      margin-top: 24rpx;

      &:first-child {
        margin-left: 0;
      }
    }

    .tag-bth-active {
      color: #0091ff;
    }
  }

  .label-textarea {
    width: 100%;
    margin-top: 24rpx;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 32rpx;
    text-align: justify;
  }

  .label-image {
    margin: 32rpx 0rpx 28rpx 0;
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .label-image-camera {
      width: 160rpx;
      height: 160rpx;
    }
  }

  .form-treaty {
    padding: 37rpx 40rpx 30rpx;
    display: flex;

    .ic-check {
      width: 32rpx;
      height: 32rpx;
    }

    .checbox-text {
      font-size: 28rpx;
      font-weight: 400;
      color: #666666;
      line-height: 46rpx;

      .jump-to {
        display: inline;
        color: #0091ff;
      }
    }

    .van-checkbox__icon-wrap {
      align-self: flex-start;
      padding-top: 8rpx;
    }
  }

  .form-button {
    padding: 25rpx 36rpx;
  }

  .dialog {
    text-align: center;
    padding: 66rpx 88rpx;
    font-size: 40rpx;
    font-weight: 400;
    line-height: 60rpx;
    color: #333333;
  }
</style>