<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 {
  name: "questionnaire",
  components: { VForm },
  data() {
    return {
      forms: [
        {
          // type: "input",
          // label: "身高（厘米）",
          // dot: "48",
          // prop: "height",
          // unit: "cm",
          // isp: true,
          // inputType: "number",
        }
      ],
      formEdit: {},
      btnLoading: false
    }
  },
  methods: {
    // login() {
    //   this.$API.login().then((res) => {
    //     console.log(res)
    //   })
    // },
    getPatientInfo() {
      // let data = 'NfQ4zQ+wWuvnPfQQgL6wHMQb5XGNtIbxXUiYHs5NmhnI7tSHpQob3FCUx59D/nPWbmKlW7pp5d00I/P9L0q9skLoGpXaf10EAwXJjyT8JFWwu5FFapdBHsjV0KLq+erWns7g70e4wYH2eTxQHpMwxj7+GOuPL5LtPR0x2RLJDyY='
      // data = data.replaceAll("+", "%2B")
      let params = {
        // data: data
        data: this.$route.query.data
      }
      this.$API
        .getPatientInfo(params)
        .then((res) => {
          if (res.code == 1) {
            this.baseInfo = {
              ...res.data
            }
            this.getQuestionList()
            this.getQuestionnaireRecords()
          }
        })
        .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: this.baseInfo.id
        })
        .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.login()
    if (this.$route.query.data) {
      this.getPatientInfo()
    }
  },
  mounted() {},
  watch: {}
}
</script>
<style lang="scss" scoped></style>
