<template>
  <div class="dialog-wrap">
    <el-dialog title="用药情况" :visible.sync="showDialog">
      <el-form :model="formChosedMedical" ref="choseMed" :rules="rules">
        <el-form-item label="药物名称" :label-width="formLabelWidth" prop="medicinesName">
          <el-input v-model="formChosedMedical.medicinesName" autocomplete="off" placeholder="请输入药物名称" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="用法" :label-width="formLabelWidth" prop="dosageDay">
          <el-input v-model="formChosedMedical.dosageDay" type="number" autocomplete="off" placeholder="请输入每日几次">
            <template slot="append">次/日</template>
          </el-input>
        </el-form-item>
        <el-form-item label="用量" :label-width="formLabelWidth" prop="dosageNum">
          <el-input v-model="formChosedMedical.dosageNum" autocomplete="off" placeholder="请输入每次用量" maxlength="10">
            <template slot="append">每次</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirmSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { checkIsInteger } from '@/utils/followup/followupUtils/checkField';
  export default {
    data(){
      var checkRange = (rule, value, callback) => {
        // let min,max;
        let val = Number(value);
        if((val >= 0) && (val <= 5)){
          callback()
        }else{
          callback(new Error(`输入范围0~5`))
        }
      }
      return {
        showDialog: true,
        formLabelWidth: '120px',
        formChosedMedical: {
          medicinesName: '',  // 药物名称
          dosageDay: '',  // 每日次数
          dosageNum: '',  // 每次的剂量
        },
        rules: {
          medicinesName: [{ required: true, message: '请输入药物名称', trigger: 'change' }],
          dosageDay: [{ required: true, message: '请输入每日几次', trigger: ['change'] },{ validator: checkIsInteger, trigger: 'change' },{validator:checkRange,trigger: 'change'}],
          dosageNum: [{ required: true, message: '请输入每次用量', trigger: 'change' }],
        },
      }
    },
    watch: {
      showDialog(val){
        this.$emit('closeDialog',val)
      }
    },
    methods: {
      closeDialog(){
        this.$emit('closeDialog',false)
      },
      confirmSubmit(){
        this.$refs['choseMed'].validate((valid) => {
          if(valid){
            this.$emit('closeDialog',false);
            this.$emit('setMedication',this.formChosedMedical);
          }
        });
      }
    }
  }
</script>

<style scoped lang="scss">

</style>
