<template>
  <el-form
    ref="revenusForm"
    size="small"
    label-width="170px"
    :rules="rules"
    :model="form"
  >
    <el-form-item label="Trial ID: " prop="TrialCOde">
      <el-input :maxLength="$inputMax" v-model="form.TrialCode" disabled />
    </el-form-item>
    <el-form-item label="Indication: " prop="Indication">
      <el-input :maxLength="$inputMax" v-model="form.Indication" disabled />
    </el-form-item>
    <el-form-item label="Dispaly All: ">
      <el-switch v-model="isDispaly" />
    </el-form-item>
    <el-form-item label="Training ($): " prop="Training">
      <el-input :maxLength="$inputMax"
        v-model="form.Training"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item label="Refresher Training ($): " prop="RefresherTraining">
      <el-input :maxLength="$inputMax"
        v-model="form.RefresherTraining"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item
      v-if="(!isDispaly && form.Expedited == 0) || isDispaly"
      label="Timepoint ($): "
      prop="Timepoint"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.Timepoint"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>

    <el-form-item
      v-if="(!isDispaly && form.Expedited == 2) || isDispaly"
      label="Timepoint 48H ($): "
      prop="TimepointIn48H"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.TimepointIn48H"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item
      v-if="(!isDispaly && form.Expedited == 1) || isDispaly"
      label="Timepoint 24H ($): "
      prop="TimepointIn24H"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.TimepointIn24H"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item
      v-if="(!isDispaly && form.Expedited == 0 && form.ReviewMode == 'Double review with adjudication') || (isDispaly && form.ReviewMode == 'Double review with adjudication')"
      label="Adjudication ($): "
      prop="Adjudication"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.Adjudication"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item
      v-if="(!isDispaly && form.Expedited == 2 && form.ReviewMode == 'Double review with adjudication') || (isDispaly && form.ReviewMode == 'Double review with adjudication')"
      label="Adjudication 48H ($): "
      prop="AdjudicationIn48H"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.AdjudicationIn48H"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item
      v-if="(!isDispaly && form.Expedited == 1 && form.ReviewMode == 'Double review with adjudication') || (isDispaly && form.ReviewMode == 'Double review with adjudication')"
      label="Adjudication 24H ($): "
      prop="AdjudicationIn24H"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.AdjudicationIn24H"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>
    <el-form-item label="Global ($): " prop="Global">
      <el-input :maxLength="$inputMax"
        v-model="form.Global"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>

    <el-form-item label="Downtime ($): " prop="Downtime">
      <el-input :maxLength="$inputMax"
        v-model="form.Downtime"
        type="number"
        oninput="this.value=this.value.replace(/[^\d.]/g,'');"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" :disabled="btnDisabled" @click="handleSave">Save</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { addOrUpdateTrialRevenuesPrice } from '@/api/financials'
// const regx = /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/
const regx = /^([1-9]\d*(\.\d+)?|0)$/
export default {
  props: {
    form: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      rules: {
        Training: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        RefresherTraining: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        Timepoint: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        TimepointIn24H: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        TimepointIn48H: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        Adjudication: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        AdjudicationIn24H: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        AdjudicationIn48H: [
          { required: true, trigger: 'blur' },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ],
        Global: [
          { required: true, trigger: ['blur', 'change'] },
          { pattern: regx, message: '请注明 a number greater than 0' }
        ]
        // Downtime: [{ required: true, message: '请注明', trigger: 'blur' }]
      },
      isDispaly: false,
      btnDisabled: false
    }
  },
  methods: {
    handleSave() {
      this.$refs.revenusForm.validate(valid => {
        if (valid) {
          this.btnDisabled = true
          const param = (({ TrialId, Timepoint, TimepointIn24H, TimepointIn48H, Adjudication, AdjudicationIn24H, AdjudicationIn48H, Global, Training, Downtime, RefresherTraining }) => ({ TrialId, Timepoint, TimepointIn24H, TimepointIn48H, Adjudication, AdjudicationIn24H, AdjudicationIn48H, Global, Training, Downtime, RefresherTraining }))(this.form)
          // if (this.form.Id !== '00000000-0000-0000-0000-000000000000') {
          //   param.Id = this.form.Id
          // }
          addOrUpdateTrialRevenuesPrice(param).then(res => {
            if (res.IsSuccess) {
              this.$emit('getList')
              this.$message.success('Updated successfully!')
              this.$emit('closeDialog')
            }
          })
        }
      })
    }
  }
}
</script>
