<template>
  <view>
    <view v-for="(item, index) in infoList" :key="index">
      <template v-for="(colItem, itemIndex) in item">
        <view v-if="colItem.showType === 1" :key="itemIndex" class="flex flex-col mx-4 border-b">
          <view class="my-3 flex justify-between items-center text-sm" @click="changeShow(index, itemIndex)">
            {{ colItem.name }}
            <u-icon name="arrow-up" class="ml-2" color="#909399" v-if="colItem.show"></u-icon>
            <u-icon name="arrow-down" class="ml-2" color="#909399" v-else></u-icon>
          </view>
          <view v-if="colItem.show" class="pb-2">
            <!-- 单选 -->
            <view v-if="colItem.type === 1">
              <u-radio-group v-model="colItem.value">
                <u-radio
                  @change="change($event, index, itemIndex, colItem.type)"
                  v-for="(radioItem, radioIndex) in colItem.radioList"
                  :key="radioIndex"
                  :name="radioItem"
                >
                  <view class="py-1">{{ radioItem }}</view>
                </u-radio>
              </u-radio-group>
              <u-input
                v-if="colItem.showOther"
                :placeholder="`请输入其他${colItem.name}`"
                v-model="colItem.otherValue"
                :clearable="false"
                @blur="change(colItem.otherValue, index, itemIndex, 99)"
              />
            </view>
            <!-- 多选 -->
            <view v-if="colItem.type === 5">
              <u-checkbox-group>
                <u-checkbox
                  @change="change($event, index, itemIndex, colItem.type)"
                  v-model="checkboxItem.checked"
                  v-for="(checkboxItem, checkboxIndex) in colItem.checkList"
                  :key="checkboxIndex"
                  :name="checkboxItem.name"
                >
                  <view class="py-1">{{ checkboxItem.name }}</view>
                </u-checkbox>
              </u-checkbox-group>
              <u-input
                v-if="colItem.showOther"
                :placeholder="`请输入其他${colItem.name}`"
                v-model="colItem.otherValue"
                :clearable="false"
                @blur="change(colItem.otherValue, index, itemIndex, 99)"
              />
            </view>
            <!-- 失眠 -->
            <view v-if="colItem.type === 10">
              <u-radio-group v-model="colItem.value" class="flex flex-col" @change="change($event, index, itemIndex, colItem.type)">
                <u-radio class="flex mb-1" :name="0"><text class="py-1 text-sm">否</text></u-radio>
                <u-radio class="flex mb-1" :name="1">
                  <view class="flex flex-nowrap items-center py-1 text-sm">
                    是，每天能睡
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      v-model="colItem.value1"
                      :clearable="false"
                      type="number"
                      @change="changeInput(colItem.value1, index, itemIndex, 'value1')"
                    />
                    小时
                  </view>
                </u-radio>
              </u-radio-group>
            </view>
            <!-- 吸烟 -->
            <view v-if="colItem.type === 11">
              <u-radio-group v-model="colItem.value" class="flex flex-col" @change="change($event, index, itemIndex, colItem.type)">
                <u-radio class="flex mb-1 w-full" :name="0"><text class="py-1 text-sm">否</text></u-radio>
                <u-radio class="flex mb-1" :name="1">
                  <view class="flex flex-nowrap items-center py-1 text-sm">
                    曾经是，但已经戒烟，已戒
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      v-model="colItem.value1"
                      :clearable="false"
                      type="number"
                      @change="changeInput(colItem.value1, index, itemIndex, 'value1')"
                    />
                    年
                  </view>
                </u-radio>
                <u-radio class="flex mb-1" :name="2">
                  <view class="flex flex-nowrap items-center py-1 text-sm">
                    是，烟龄
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      v-model="colItem.value2"
                      :clearable="false"
                      type="number"
                      @change="changeInput(colItem.value2, index, itemIndex, 'value2')"
                    />
                    年，每天
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      :border="true"
                      v-model="colItem.value3"
                      type="number"
                      @change="changeInput(colItem.value3, index, itemIndex, 'value3')"
                    />只
                  </view>
                </u-radio>
              </u-radio-group>
            </view>
            <!-- 饮酒 -->
            <view v-if="colItem.type === 12">
              <u-radio-group v-model="colItem.value" class="flex flex-col" @change="change($event, index, itemIndex, colItem.type)">
                <u-radio class="flex mb-1" :name="0"><text class="py-1 text-sm">否</text></u-radio>
                <u-radio class="flex mb-1" :name="1">
                  <view class="flex flex-nowrap items-center py-1 text-sm">
                    曾经是，但已经戒酒，已戒
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      placeholder=" "
                      v-model="colItem.value1"
                      :clearable="false"
                      type="number"
                      @change="changeInput(colItem.value1, index, itemIndex, 'value1')"
                    />
                    年
                  </view>
                </u-radio>
                <u-radio class="flex mb-1" :name="2"><text class="py-1 text-sm">是</text></u-radio>
              </u-radio-group>
              <!-- 饮酒种类 -->
              <view class="mt-2 flex flex-col" v-if="colItem.showClasses">
                <view>饮酒种类</view>
                <u-radio-group v-model="colItem.classesValue" class="my-2">
                  <u-radio
                    @change="changeInput($event, index, itemIndex, 'classesValue')"
                    v-for="(radioItem, classRadioIndex) in colItem.radioList"
                    :key="classRadioIndex"
                    :name="radioItem"
                  >
                    <text class="py-1 text-sm">{{ radioItem }}</text>
                  </u-radio>
                </u-radio-group>
                <u-input
                  v-if="colItem.showOther"
                  placeholder="请输入其他种类"
                  v-model="colItem.otherValue"
                  :clearable="false"
                  @blur="changeInput(colItem.otherValue, index, itemIndex, 'otherValue')"
                />
              </view>
            </view>
            <!-- 饮茶 -->
            <view v-if="colItem.type === 13">
              <u-radio-group v-model="colItem.value" class="flex flex-col" @change="change($event, index, itemIndex, colItem.type)">
                <u-radio class="flex mb-1" :name="0"><text class="py-1 text-sm">否</text></u-radio>
                <u-radio class="flex mb-1" :name="1">
                  <view class="flex flex-nowrap items-center py-1 text-sm">
                    曾经是，但已经不喝，已不喝
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      v-model="colItem.value1"
                      :clearable="false"
                      type="number"
                      @change="changeInput(colItem.value1, index, itemIndex, 'value1')"
                    />
                    年
                  </view>
                </u-radio>
                <u-radio class="flex mb-1" :name="2">
                  <view class="flex flex-nowrap items-center py-1 text-sm">
                    是，已持续
                    <input
                      class="mx-1 w-12 h-6 text-center border-b"
                      v-model="colItem.value2"
                      :clearable="false"
                      type="number"
                      @change="changeInput(colItem.value2, index, itemIndex, 'value2')"
                    />
                    年
                  </view>
                </u-radio>
              </u-radio-group>
              <!-- 饮茶种类 -->
              <view class="mt-2 flex flex-col" v-if="colItem.showClasses">
                <view>饮茶种类</view>
                <u-radio-group v-model="colItem.classesValue" class="my-2">
                  <u-radio
                    @change="changeInput($event, index, itemIndex, 'classesValue')"
                    v-for="(radioItem, classIndex) in colItem.classesList"
                    :key="classIndex"
                    :name="radioItem"
                  >
                    <text class="py-1 text-sm">{{ radioItem }}</text>
                  </u-radio>
                </u-radio-group>
                <view>饮茶频率</view>
                <u-radio-group v-model="colItem.frequencyValue" class="my-2">
                  <u-radio
                    @change="changeInput($event, index, itemIndex, 'frequencyValue')"
                    v-for="(radioItem, frequencyIndex) in colItem.frequencyList"
                    :key="frequencyIndex"
                    :name="radioItem"
                  >
                    <text class="py-1 text-sm">{{ radioItem }}</text>
                  </u-radio>
                </u-radio-group>
              </view>
            </view>
            <!-- 单选 有id -->
            <view v-if="colItem.type === 14">
              <u-radio-group v-model="colItem.value">
                <u-radio
                  @change="change($event, index, itemIndex, colItem.type)"
                  v-for="(radioItem, radioIndex) in colItem.radioList"
                  :key="radioIndex"
                  :name="radioItem.id"
                >
                  <view class="py-1 text-sm">{{ radioItem.value }}</view>
                </u-radio>
              </u-radio-group>
            </view>
          </view>
        </view>
        <view v-else :key="itemIndex" class="mx-4 flex justify-between items-center border-b">
          <view class="my-3 text-sm">{{ colItem.name }}</view>
          <!-- 日期: 年月日 -->
          <view v-if="colItem.type === 2">
            <view @click="changeShow(index, itemIndex)" class="text-sm">
              <text :class="colItem.value ? '' : 'text-gray-350'">{{ colItem.value || '请选择' }} </text>
              <u-icon name="arrow-right" class="ml-2" color="#909399"></u-icon>
            </view>
            <u-picker
              @confirm="change($event, index, itemIndex, colItem.type)"
              v-model="colItem.show"
              :default-time="colItem.value"
              mode="time"
              :start-year="startYear"
              :end-year="endYear"
              :params="paramsDay"
              :title="colItem.name"
            ></u-picker>
          </view>
          <!-- 数字输入框 -->
          <view v-if="colItem.type === 3" class="pr-5">
            <u-input
              placeholder="请输入"
              v-model="colItem.value"
              :clearable="false"
              type="number"
              input-align="right"
              @blur="change(colItem.value, index, itemIndex, colItem.type)"
            />
          </view>
          <!-- 单列下拉选框 -->
          <view v-if="colItem.type === 4">
            <view @click="changeShow(index, itemIndex)" class="text-sm">
              <text :class="colItem.value ? '' : 'text-gray-350'">{{ colItem.value || '请选择' }} </text>
              <u-icon name="arrow-right" class="ml-2" color="#909399"></u-icon>
            </view>
            <u-select
              v-model="colItem.show"
              mode="single-column"
              :list="colItem.radioList"
              @confirm="change($event, index, itemIndex, colItem.type)"
              :default-value="[colItem.defaultValue]"
              :title="colItem.name"
            ></u-select>
          </view>
          <!-- 多列下拉选框 -->
          <view v-if="colItem.type === 7">
            <view @click="changeShow(index, itemIndex)" class="text-sm">
              <text :class="colItem.value ? '' : 'text-gray-350'">{{ colItem.value || '请选择' }} </text>
              <u-icon name="arrow-right" class="ml-2" color="#909399"></u-icon>
            </view>
            <u-select
              v-model="colItem.show"
              mode="mutil-column"
              :list="colItem.radioList"
              @confirm="change($event, index, itemIndex, colItem.type)"
              :default-value="colItem.defaultValue"
              title="低血压/高血压"
            ></u-select>
          </view>
          <!-- input 文本输入框 -->
          <view v-if="colItem.type === 8" class="pr-5">
            <u-input
              placeholder="请输入"
              v-model="colItem.value"
              :clearable="false"
              input-align="right"
              @blur="change(colItem.value, index, itemIndex, colItem.type)"
            />
          </view>
          <!-- 地区选择器 -->
          <view v-if="colItem.type === 9">
            <view @click="changeShow(index, itemIndex)" class="text-sm">
              <text :class="colItem.value ? '' : 'text-gray-350'">{{ colItem.value || '请选择' }} </text>
              <u-icon name="arrow-right" class="ml-2" color="#909399"></u-icon>
            </view>
            <u-picker
              @confirm="change($event, index, itemIndex, colItem.type)"
              mode="region"
              class="text-sm"
              v-model="colItem.show"
              :params="paramsArea"
              :title="colItem.name"
              :area-code="colItem.defaultValue"
            ></u-picker>
          </view>
          <!-- 带小数点的数字键盘 -->
          <view v-if="colItem.type === 15" class="pr-3">
            <view @click="change($event, index, itemIndex, colItem.type)" class="w-24 text-right" v-if="colItem.value">
              {{ colItem.value }}
            </view>
            <view @click="change($event, index, itemIndex, colItem.type)" class="w-full text-right text-gray-350" v-else>
              {{ colItem.placeholder }}
            </view>
            <u-keyboard
              mode="number"
              :mask="false"
              :show-tips="false"
              v-model="colItem.show"
              @change="changeVal($event, index, itemIndex)"
              @backspace="backspace($event, index, itemIndex)"
              @confirmText="confirmText(index, itemIndex)"
              @cancelText="confirmText(index, itemIndex)"
            ></u-keyboard>
          </view>
        </view>
      </template>
      <view class="w-full h-2 bg-gray-100" v-if="index !== infoList.length - 1"></view>
    </view>
    <view class="p-4 bg" v-if="!isEdit">
      <u-button type="primary" class="w-full" @click="submit">完成</u-button>
    </view>
  </view>
</template>

<script>
import { mapGetters } from 'vuex';
import { infoList } from '@/config/yyInfo';

export default {
  name: 'Info',
  props: {
    personalInfo: { type: Object, default: () => {} },
    isEdit: { type: Boolean, default: false },
  },

  data() {
    return {
      infoList,
      paramsDay: {
        year: true,
        month: true,
        day: true,
        hour: false,
        minute: false,
        second: false,
      },
      paramsArea: {
        province: true,
        city: true,
        area: false,
      },
    };
  },

  computed: {
    ...mapGetters('project', ['projectId']),
    startYear() {
      var date = new Date();
      return date.getFullYear() - 130;
    },
    endYear() {
      var date = new Date();
      return date.getFullYear();
    },
  },

  watch: {
    personalInfo(val) {
      if (val && val.userName) {
        this.setDate(val);
      }
    },
  },

  mounted() {
    this.$nextTick(() => {
      if (!this.isEdit) {
        this.$emit('setEmptyInfo');
        const info = this.$t.storage.getStorageSync('infoList');
        if (info !== 'null' && !this.isEdit) {
          this.infoList = JSON.parse(info);
        }
      }
    });
  },

  methods: {
    changeVal(e, index, itemIndex) {
      const info = this.infoList[index][itemIndex];

      if (info.value === null) {
        info.value = e.toString();
      } else {
        if (typeof e === 'number') {
          const str = e.toString();
          info.value += str;
        } else {
          info.value += e;
        }
      }
      if (this.isEdit) {
        this.updateTrainee(info);
      }
    },
    // 退格键被点击
    backspace(e, index, itemIndex) {
      const info = this.infoList[index][itemIndex];
      // 删除value的最后一个字符
      if (info.value.length) info.value = info.value.substr(0, info.value.length - 1);
      if (this.isEdit) {
        this.updateTrainee(info);
      }
    },
    confirmText(index, itemIndex) {
      const info = this.infoList[index][itemIndex];
      info.show = false;
    },

    changeShow(index, itemIndex) {
      this.infoList[index][itemIndex].show = !this.infoList[index][itemIndex].show;
    },

    change(e, index, itemIndex, type) {
      const info = this.infoList[index][itemIndex];
      switch (type) {
        case 2: {
          // 日期选择器(年月日)
          const valueDay = e.year + '-' + e.month + '-' + e.day;
          info.value = valueDay;
          break;
        }
        case 4: {
          // 单列下拉选框
          info.value = e[0].value;
          break;
        }
        case 5: {
          // 多选
          let checkerList = info.value || [];
          if (checkerList.length) {
            const i = checkerList.findIndex(item => item === e.name || item.substring(0, 2) === e.name);
            if (i < 0) {
              checkerList.push(e.name);
            } else {
              checkerList.splice(i, 1);
            }
          } else {
            checkerList.push(e.name);
          }
          info.value = checkerList;
          const otherIndex = checkerList.findIndex(checker => checker === '其他');
          if (otherIndex !== -1) {
            info.showOther = true;
          } else {
            info.showOther = false;
            info.otherValue = null;
          }
          break;
        }
        case 7: {
          // 多列下拉选框
          info.value = e[0].value + '/' + e[1].value;
          break;
        }
        case 9: {
          // 地区选择
          info.value = e.province.label + e.city.label;
          info.hospitalCityId = e.city.value;
          info.hospitalProvinceId = e.province.value;
          info.defaultValue = [e.province.value, e.city.value];
          break;
        }
        case 15: {
          // 带小数点的数字键盘
          info.show = !info.show;
          break;
        }
        case 99: {
          // 其他
          info.otherValue = e;
          break;
        }
        default: {
          info.value = e;
          if (type === 1) {
            if (e === '其他') {
              info.showOther = true;
            } else {
              info.showOther = false;
              info.otherValue = null;
            }
          }
          // 吸烟饮酒失眠饮茶 操作
          this.setEmpty(e, info, index, itemIndex);

          if (type === 12 || type === 13) {
            if (e === 2) {
              info.showClasses = true;
            } else {
              info.showClasses = false;
            }
          }
          break;
        }
      }
      if (this.isEdit) {
        this.updateTrainee(info);
        this.$t.storage.setStorageSync('infoList', null);
      } else {
        this.$t.storage.setStorageSync('infoList', this.infoList);
      }
    },

    changeInput(e, index, itemIndex, type) {
      const info = this.infoList[index][itemIndex];
      if (type === 'classesValue') {
        if (e === '其他') {
          info.showOther = true;
        } else {
          info.showOther = false;
        }
      }
      info[type] = e;

      if (this.isEdit) {
        this.updateTrainee(info);
      }
    },

    // 吸烟饮酒失眠饮茶
    setEmpty(e, info, index, itemIndex) {
      if (e === 0) {
        if (info.value1) {
          info.value1 = null;
          this.changeInput('', index, itemIndex, 'value1');
        }
        if (info.value2) {
          info.value2 = null;
          this.changeInput('', index, itemIndex, 'value2');
        }
        if (info.value3) {
          info.value3 = null;
          this.changeInput('', index, itemIndex, 'value3');
        }
      }
      if (e === 1) {
        if (info.value2) {
          info.value2 = null;
          this.changeInput('', index, itemIndex, 'value2');
        }
        if (info.value3) {
          info.value3 = null;
          this.changeInput('', index, itemIndex, 'value3');
        }
      }
      if (e === 2) {
        if (info.value1) {
          info.value1 = null;
          this.changeInput('', index, itemIndex, 'value1');
        }
      }
    },

    // 提交用户信息
    submit() {
      if (!this.AddvalidationRequired()) return;
      const params = this.saveInfo();
      this.$emit('submit', params, this.infoList);
    },

    // 添加用户信息 设置参数
    saveInfo() {
      const params = {};
      for (let i = 0; i < this.infoList.length; i++) {
        const item = this.infoList[i];
        for (let j = 0; j < item.length; j++) {
          const info = item[j];

          // 默认 否 的
          params.apoeGene = '0';
          params.isInsomnic = 0;
          params.isSmoke = 0;
          params.isDrink = 0;
          params.isTea = 0;
          params.isStrongFlavour = 0;
          params.isMoreOil = 0;

          // 赋值
          params[info.label] = info.value;

          // 其他
          if (info.value === '其他' && info.showOther && info.otherValue) {
            params[info.label] = info.otherValue;
          }
          // 疾病史
          if (info.showType === 1 && info.type === 5) {
            let arrValue = '';
            if (info.showOther && info.otherValue) {
              const index = info.value.indexOf('其他');
              info.value.splice(index, 1, '其他:' + info.otherValue);
            }
            arrValue = info.value.toString();
            params[info.label] = arrValue;
          }
          // 血压
          if (info.label === 'BloodPressure') {
            const arr = info.value.split('/');
            params.minBloodPressure = arr[0];
            params.maxBloodPressure = arr[1];
          }
          // 医院地址
          if (info.type === 9) {
            params.hospitalCityId = info.hospitalCityId;
            params.hospitalProvinceId = info.hospitalProvinceId;
          }
          // 失眠
          if (info.type === 10) {
            params.insomnicPeriod = info.value1;
          }
          // 吸烟
          if (info.type === 11) {
            params.quitPeriod = info.value1;
            params.sustainPeriod = info.value2;
            params.averageNum = info.value3;
          }
          // 饮酒
          if (info.type === 12) {
            params.abstinencePeriod = info.value1;
            if (info.classesValue) {
              params.drinkType = info.classesValue;
              if (info.classesValue === '其他' && info.showOther && info.otherValue) {
                params.drinkType = info.otherValue;
              }
            }
          }
          // 饮茶
          if (info.type === 13) {
            params.quitTea = info.value1;
            params.continuePeriod = info.value2;

            params.teaType = info.classesValue;
            params.teaPeriod = info.frequencyValue;
          }
        }
      }
      return params;
    },

    // 验证必填项
    AddvalidationRequired() {
      let isComplete = true;
      for (let i = 0; i < this.infoList.length; i++) {
        const info = this.infoList[i];
        for (let j = 0; j < info.length; j++) {
          const item = info[j];
          if (item.type !== 5) {
            if (item.value === null) {
              this.$emit('showToast', 'default', `请填写${item.name}`, false);
              isComplete = false;
              break;
            }
            if (item.showOther && !item.otherValue) {
              this.$emit('showToast', 'default', `请填写${item.name}`, false);
              isComplete = false;
              break;
            }
            if (item.type === 10 || item.type === 11 || item.type === 12 || item.type === 13) {
              if (item.value === 1 && !item.value1) {
                this.$emit('showToast', 'default', `请填写${item.name}`, false);
                isComplete = false;
                break;
              }
            }
            if (item.showClasses && !item.classesValue) {
              this.$emit('showToast', 'default', `请填写${item.name}种类`, false);
              isComplete = false;
              break;
            }
            if (item.type === 13) {
              if (item.showClasses && !item.frequencyValue) {
                this.$emit('showToast', 'default', `请填写${item.name}频率`, false);
                isComplete = false;
                break;
              }
            }
            if (item.type === 11) {
              if (item.value === 2) {
                if (!item.value2) {
                  this.$emit('showToast', 'default', `请填写烟龄`, false);
                  isComplete = false;
                  break;
                }
                if (!item.value3) {
                  this.$emit('showToast', 'default', `请填写每天抽烟数量`, false);
                  isComplete = false;
                  break;
                }
              }
            }
            if (item.type === 13) {
              if (item.value === 2 && !item.value2) {
                this.$emit('showToast', 'default', `请填写持续喝茶时长`, false);
                isComplete = false;
                break;
              }
            }
          } else {
            if (item.value.length === 0) {
              this.$emit('showToast', 'default', `请填写${item.name}`, false);
              isComplete = false;
              break;
            }
            if (item.showOther && !item.otherValue) {
              this.$emit('showToast', 'default', `请填写其他${item.name}`, false);
              isComplete = false;
              break;
            }
          }
        }
        if (!isComplete) {
          break;
        }
      }
      if (isComplete) return true;
    },

    // 回显
    setDate(date) {
      if (!date || !date.userName) return;
      const { infoList } = this;
      for (let i = 0; i < infoList.length; i++) {
        const info = infoList[i];
        for (let j = 0; j < info.length; j++) {
          const item = info[j];
          item.value = date[item.label];
          if (item.label === 'jobTitle' || item.label === 'educateStatus' || item.label === 'diagnoseResult') {
            const list = item.radioList.find(i => i === date[item.label]);
            if (!list) {
              item.value = '其他';
              item.otherValue = date[item.label];
              item.showOther = true;
            } else {
              item.showOther = false;
            }
          }
          if (item.type === 4) {
            const index = item.radioList.findIndex(list => list.value === date[item.label]);
            item.defaultValue = index;
          }
          if (item.type === 5) {
            const array = date[item.label].split(',');
            item.value = array;
            for (let i = 0; i < item.checkList.length; i++) {
              const A = item.checkList[i];
              for (let j = 0; j < array.length; j++) {
                const arr = array[j];
                if (A.name === arr) {
                  A.checked = true;
                }
                if (A.name === arr.substring(0, 2) && A.name === '其他') {
                  A.checked = true;
                  item.showOther = true;
                  item.otherValue = arr.split(':')[1];
                }
              }
            }
          }
          if (item.type === 7) {
            item.value = date.minBloodPressure + '/' + date.maxBloodPressure;
            item.defaultValue = [date.minBloodPressure, date.maxBloodPressure];
          }
          if (item.type === 9) {
            item.defaultValue = [date.hospitalProvinceId, date.hospitalCityId];
          }
          if (item.type === 10 && date[item.label] === 1) {
            item.value1 = date.insomnicPeriod;
          }
          if (item.type === 11) {
            if (date[item.label] === 1) {
              item.value1 = date.quitPeriod;
            }
            if (date[item.label] === 2) {
              item.value2 = date.sustainPeriod;
              item.value3 = date.averageNum;
            }
          }
          if (item.type === 12) {
            if (date[item.label] === 1) {
              item.value1 = date.abstinencePeriod;
            }
            if (date[item.label] === 2) {
              item.showClasses = true;
              const list = item.radioList.find(list => list === date.drinkType);
              if (list) {
                item.classesValue = date.drinkType;
              } else {
                item.classesValue = '其他';
                item.showOther = true;
                item.otherValue = date.drinkType;
              }
            }
          }
          if (item.type === 13) {
            if (date[item.label] === 1) {
              item.value1 = date.quitTea;
            }
            if (date[item.label] === 2) {
              item.value2 = date.continuePeriod;
              item.showClasses = true;
              item.classesValue = date.teaType;
              item.frequencyValue = date.teaPeriod;
            }
          }
        }
      }
    },

    /**
     * 修改用户信息
     */
    async updateTrainee(item) {
      try {
        if (!this.validationRequired(item)) return;
        const params = this.setParams(item);
        await this.$u.api.updateTrainee(params);
      } catch (error) {
        console.error('error: ', error);
        this.$emit('showToast', 'default', `${item.name}修改失败`, false);
      }
    },

    // 验证必填项
    validationRequired(item) {
      if (item.showOther && !item.otherValue) {
        this.$emit('showToast', 'default', `请填写其他${item.name}`, false);
        return;
      }
      if (item.type === 10 || item.type === 11 || item.type === 12 || item.type === 13) {
        if (item.value === 1 && !item.value1) {
          this.$emit('showToast', 'default', `请填写${item.name}`, false);
          return;
        }
      }
      if (item.showClasses && !item.classesValue) {
        this.$emit('showToast', 'default', `请填写${item.name}种类`, false);
        return;
      }
      if (item.type === 13) {
        if (item.showClasses && !item.frequencyValue) {
          this.$emit('showToast', 'default', `请填写${item.name}频率`, false);
          return;
        }
      }
      if (item.type === 11) {
        if (item.value === 2) {
          if (!item.value2) {
            this.$emit('showToast', 'default', `请填写烟龄`, false);
            return;
          }
          if (!item.value3) {
            this.$emit('showToast', 'default', `请填写每天抽烟数量`, false);
            return;
          }
        }
      }
      if (item.type === 13) {
        if (item.value === 2 && !item.value2) {
          this.$emit('showToast', 'default', `请填写持续喝茶时长`, false);
          return;
        }
      }
      return true;
    },

    // 设置参数
    setParams(item) {
      let params = {};
      params.projectId = this.projectId;
      params[item.label] = item.value;
      // 其他
      if (item.value === '其他' && item.showOther && item.otherValue) {
        params[item.label] = item.otherValue;
      }
      // 血压
      if (item.label === 'BloodPressure') {
        const arr = item.value.split('/');
        params.minBloodPressure = arr[0];
        params.maxBloodPressure = arr[1];
      }
      // 疾病史
      if (item.showType === 1 && item.type === 5) {
        let arrValue = '';
        if (item.showOther && item.otherValue) {
          const index = item.value.indexOf('其他');
          item.value.splice(index, 1, '其他:' + item.otherValue);
        }
        arrValue = item.value.toString();
        params[item.label] = arrValue;
      }
      // 医院地址
      if (item.type === 9) {
        params.hospitalCityId = item.hospitalCityId;
        params.hospitalProvinceId = item.hospitalProvinceId;
      }
      // 失眠
      if (item.type === 10) {
        params.insomnicPeriod = item.value1;
      }
      // 吸烟
      if (item.type === 11) {
        params.quitPeriod = item.value1;
        params.sustainPeriod = item.value2;
        params.averageNum = item.value3;
      }
      // 饮酒
      if (item.type === 12) {
        params.abstinencePeriod = item.value1;
        if (item.classesValue) {
          params.drinkType = item.classesValue;
          if (item.classesValue === '其他' && item.showOther && item.otherValue) {
            params.drinkType = item.otherValue;
          }
        }
      }
      // 饮茶
      if (item.type === 13) {
        params.quitTea = item.value1;
        params.continuePeriod = item.value2;

        params.teaType = item.classesValue;
        params.teaPeriod = item.frequencyValue;
      }
      return params;
    },
  },
};
</script>

<style lang="scss" scoped></style>
