<template>
    <div class="form-wrap">
      <!--渲染不同的模块表单-->
      <div  v-if="canRender" v-for="(item, index) in addComponents">
        <div class="content-box" >
          <div v-if="!item.hideTitle" class="title">{{item.title}}</div>
          <el-form
            :ref="'form' + index"
            :model="item.formObject"
            size="medium"
            label-position="left"
            class="form"
          >
            <FormScale
              :dataSource="item.dataSource"
              :form="item.formObject"
              :isRule="isRule"
              :class="item.className"
            />
          </el-form>
        </div>
      </div>
    </div>
</template>

<script>
  import { getFollowUpApi, getBasicDataApi, getFollowUpSC } from '@/utils/index'
  // 量表 template
  import FormScale from '@/components/followup/form/index'
  // 量表模块数据模型

  import dataSourceBaseInfo0 from '../models/stroke/dataSourceBaseInfo0'
  import dataSourceBaseInfo from '../models/stroke/dataSourceBaseInfo'
  import dataSourceBaseInfo2 from '../models/stroke/dataSourceBaseInfo2'
  import dataSourceMainSickControl from '../models/stroke/dataSourceMainSickControl'
  import dataSourceMainSickControl2 from '../models/stroke/dataSourceMainSickControl2'
  import dataSourceMainSickControl3 from '../models/stroke/dataSourceMainSickControl3'
  import dataSourceMainSickControl4 from '../models/stroke/dataSourceMainSickControl4'
  import dataSourceMainSickControl5 from '../models/stroke/dataSourceMainSickControl5'
  import dataSourceMainSickControl6 from '../models/stroke/dataSourceMainSickControl6'
  import dataSourceLifeStyleGuide from '../models/stroke/dataSourceLifeStyleGuide'
  import dataSourceTreatmentSituation from '../models/stroke/dataSourceTreatmentSituation'
  import dataSourceTreatmentSituation2 from '../models/stroke/dataSourceTreatmentSituation2'
  import dataSourceTreatmentSituation3 from '../models/stroke/dataSourceTreatmentSituation3'
  import dataSourceCheckBody from '../models/stroke/dataSourceCheckBody'
  import dataSourceCheckLab from '../models/stroke/dataSourceCheckLab'

  import axios from 'axios';
  import { submitScale } from '@/utils/followup/followapis';

  export default {
    data(){
      return {
        canRender: true,
        bmi: 0,
        addComponents: [
          {name: 'BaseInfo0',formObject: {},formName: 'stroke_001',className: 'obj-form-title',dataSource: dataSourceBaseInfo0(this), hideTitle: true, title: '一、量表脑卒中随访人员信息表'},
          {name: 'BaseInfo',formObject: {},formName: 'stroke_002',className: 'obj-form-title',dataSource: dataSourceBaseInfo(this), title: '一、基本信息'},
          {name: 'BaseInfo2',formObject: {},formName: 'stroke_003',className: 'obj-form-title',dataSource: dataSourceBaseInfo2(this), hideTitle: true, title: '一、基本信息'},
          {name: 'LifeStyleGuide',formObject: {},formName: 'stroke_004',className: 'obj-form-title',dataSource: dataSourceLifeStyleGuide(this), title: '二、随访期间生活方式'},
          {name: 'MainSickControl',formObject: {},formName: 'stroke_005',className: 'obj-form-title',dataSource: dataSourceMainSickControl(this), title: '三、随访期间主要病史及控制情况-脑血管病'},
          {name: 'MainSickControl2',formObject: {},formName: 'stroke_006',className: 'obj-form-title',dataSource: dataSourceMainSickControl2(this), hideTitle: true, title: '三、随访期间主要病史及控制情况-MRS评分（卒中患者12个月随访必填）'},
          {name: 'MainSickControl3',formObject: {},formName: 'stroke_007',className: 'obj-form-title',dataSource: dataSourceMainSickControl3(this), hideTitle: true, title: '三、随访期间主要病史及控制情况-心脏病'},
          {name: 'MainSickControl4',formObject: {},formName: 'stroke_008',className: 'obj-form-title',dataSource: dataSourceMainSickControl4(this), hideTitle: true, title: '三、随访期间主要病史及控制情况-高血压'},
          {name: 'MainSickControl5',formObject: {},formName: 'stroke_009',className: 'obj-form-title',dataSource: dataSourceMainSickControl5(this), hideTitle: true, title: '三、随访期间主要病史及控制情况-血脂异常'},
          {name: 'MainSickControl6',formObject: {},formName: 'stroke_010',className: 'obj-form-title',dataSource: dataSourceMainSickControl6(this), hideTitle: true, title: '三、随访期间主要病史及控制情况-糖尿病'},
          {name: 'TreatmentSituation',formObject: {},formName: 'stroke_011',className: 'obj-form-title',dataSource: dataSourceTreatmentSituation(this), title: '四、本次随访期间血管病变的外科手术或介入治疗情况-颈动脉'},
          {name: 'TreatmentSituation2',formObject: {},formName: 'stroke_012',className: 'obj-form-title',dataSource: dataSourceTreatmentSituation2(this), hideTitle: true, title: '四、本次随访期间血管病变的外科手术或介入治疗情况-冠状动脉'},
          {name: 'TreatmentSituation3',formObject: {},formName: 'stroke_013',className: 'obj-form-title',dataSource: dataSourceTreatmentSituation3(this), hideTitle: true, title: '四、本次随访期间血管病变的外科手术或介入治疗情况-出血性卒中外科治疗'},
          {name: 'CheckBody',formObject: {},formName: 'stroke_014',className: 'obj-form-title',dataSource: dataSourceCheckBody(this), title: '五、体格检查（高危人群12个月随访必做）'},
          {name: 'CheckLab',formObject: {},formName: 'stroke_015',className: 'obj-form-title',dataSource: dataSourceCheckLab(this), title: '六、实验室检查（高危人群12个月随访必填）'},
        ],
        formData: {},//整个表单对象
        isRule: false,//验证的依赖关系
      }
    },
    components: {
      FormScale,
    },
    props: {
      showBtn: Boolean,
      checkStart: Boolean,
    },
    watch: {
      checkStart(val){
        if(val){
          for(let i=0;i<this.addComponents.length;i++){
            let formName = this.addComponents[i].formName;
            // 将每个模块的fornName对应后台的表名放到表单对象
            this.formData[formName] = this.addComponents[i].formObject;

//            if(formName=='stroke_003'){
////              console.log('checkStart=inturn=',this.formData.stroke_003.inTurn)
//            }
//            if(formName=='stroke_006'||formName=='stroke_014'||formName=='stroke_015'){
//              if(this.formData.stroke_003.inTurn==2){
//                console.log(this.addComponents[i].dataSource)
////                this.setRulesToFalse(this.addComponents[i].dataSource)
//                this.addComponents[i].dataSource.forEach((item,index)=>{
//                  item.rules = [{ required: true, message: '请选择评估时间', trigger: 'change' }];
//                })
////                this.isRule = true;
//              }
//            }

            this.$refs['form' + i][0].validate((valid) => {
              this.valid = valid;
            });
          }
          console.log(this.formData)
          this.formData.doctorId = 1
          this.formData.saveWay = 1
          this.formData.scaleNo = 'SCALE0003'
          this.formData.planPatientsTimesId = 1
          console.log(JSON.stringify(this.formData))

          if(this.valid){
            axios({
              method: 'post',
              url: getFollowUpSC('/scale/save'),
              data: JSON.stringify(this.formData),
            }).then(res=>{
              console.log(res)
              if(res.data.code=='000000'){
                this.$message({
                  message: '录入成功',
                  type: 'success'
                });
                setTimeout(()=>{
                  this.$router.push({
                    path: '/followup/record-manage/record-list'
                  })
                },1000)
              }
            });
          }
          this.$emit('checkEnd',false);
        }
      },
      bmi(val){
        if(val>0){
          this.addComponents.forEach((item,index)=>{
            if(item.formName == 'stroke_014'){
              console.log('watch==>>',val)
              this.addComponents[index].formObject.bmi = val
              item.formObject.bmi = val
              this.$forceUpdate()
            }
          })
        }
      }
    },
    created(){
//      /scale/{patientTimesId}/{scaleNo}/info
      axios({
        method: 'get',
        url: getFollowUpSC('/scale/1/SCALE0003/info?type=2'),
        data: JSON.stringify(this.formData),
      }).then(res=>{
//        console.log(res.data.data.stroke_007)
        this.formData = res.data.data
//        let obj = res.data.data
        for(let i=0;i<this.addComponents.length;i++){
          let formName = this.addComponents[i].formName;
          this.addComponents[i].formObject = this.formData[formName];
//          this.formData[formName] = this.addComponents[i].formObject;
        }
        this.canRender = true;
        this.$forceUpdate();
      })
    }
  }
</script>

<style scoped lang="scss">
.form-wrap{
  .content-box{
    .title{
      line-height: 60px;
      height: 60px;
      font-weight: bold;
      font-size: 18px;
      border-bottom: 1px dashed #ccc;
    }

  }
}
</style>
