<template>
  <div>
    <x-header :left-options="{ backText: '', showBack: false }">
      调查表
    </x-header>
    <v-content :has-header="true" :has-footer="true">
      <v-form
        :form-data="forms"
        :formEdit="formEdit"
        :isPart="true"
        @handleConfirm="submit"
        ref="form"
        :loading="btnLoading"
      ></v-form>
    </v-content>
  </div>
</template>
<script>
import VForm from "@/components/FormComponents"
export default {
  components: { VForm },
  data() {
    return {
      forms: [
        {
          // type: "input",
          // label: "身高（厘米）",
          // dot: "48",
          // prop: "height",
          // unit: "cm",
          // isp: true,
          // inputType: "number",
        }
      ],
      formEdit: {},
      btnLoading: false
    }
  },
  methods: {
    getPatientInfo() {
      let params = {
        idCard: 111111111,
        patientName: "张三",
        phone: 18888888888
      }
      this.$API
        .getPatientInfo(params)
        .then((res) => {
          this.baseInfo = {
            ...res.data
          }
        })
        .catch(() => {
          this.baseInfo = {}
        })
    },
    getQuestionList() {
      this.$API.getQuestionnaireList().then((res) => {
        if (res.code == 1) {
          this.forms = [...res.data]
          this.forms.map((e) => {
            e.rules = [{ required: true, trigger: "blur", message: "该项必传" }]
          })

          this.forms.push({
            type: "button",
            buttonType: "info",
            size: "large",
            buttonMargin: "10px",
            borderRadius: "50px",
            buttonWidth: "95%",
            buttonText: "保存",
            nativeType: "submit"
          })
        }
      })
    },
    getQuestionnaireRecords() {
      this.$API
        .getQuestionnaireRecords({
          patientId: 1
        })
        .then((res) => {
          if (res.code == 1) {
            if (res.data) {
              this.records = res.data.map((e) => e.records)
              this.formEdit = { ...this.records[0] }
            }
          }
        })
    },
    submit() {
      this.btnLoading = true
      let form = this.$refs.form.form
      const data = {
        params: form,
        patientId: this.baseInfo.id
      }
      this.$API
        .setQuestionnaire(data)
        .then((res) => {
          this.btnLoading = false
          if (res.code == 1) {
            this.$toast.success({
              type: "success",
              forbidClick: true,
              message: "提交成功",
              onClose: () => {
                // this.$router.push("/peopleList")
              },
              duration: 1500
            })
          }
        })
        .catch((error) => {
          console.log(error)
          this.btnLoading = false
        })
    }
  },
  created() {
    this.getPatientInfo()
    this.getQuestionList()
  },
  mounted() {
    this.getQuestionnaireRecords()
  },
  watch: {}
}
</script>
<style lang="scss" scoped></style>
