<template>
  <el-form
    ref="wlForm"
    v-loading="loading"
    :model="form"
    :rules="rules"
    label-width="80px"
    size="small"
  >
    <!-- 名称 -->
    <el-form-item label="名称" prop="TemplateName">
      <el-input v-model="form.TemplateName" />
    </el-form-item>
    <el-form-item :label="$t('CustomWwwcForm:form:label:ww')" prop="WW">
      <el-input-number v-model="form.WW" controls-position="right" :min="1" :precision="0" :step="1" />
    </el-form-item>
    <el-form-item :label="$t('CustomWwwcForm:form:label:wl')" prop="WL">
      <el-input-number v-model="form.WL" controls-position="right" :precision="0" :step="1" />
    </el-form-item>
    <el-form-item style="text-align:right;">
      <!-- Cancel -->
      <el-button
        type="primary"
        @click="handleCancel"
      >
        {{ $t('common:button:cancel') }}
      </el-button>
      <!-- Save -->
      <el-button
        type="primary"
        @click="handleSave"
      >
        {{ $t('common:button:save') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { addOrUpdateUserWLTemplate } from '@/api/user'
export default {
  props: {
    row: {
      type: Object,
      default() {
        return {}
      }
    },
    isHaveFirstGiveMedicineDate: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        Id: '',
        TemplateName: '',
        WW: '',
        WL: ''
      },
      loading: false,
      rules: {
        TemplateName: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: `${this.$t('common:ruleMessage:maxLength')} 50` }
        ],
        WW: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ],
        WL: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    for (const k in this.form) {
      if (this.row.hasOwnProperty(k)) {
        this.form[k] = this.row[k]
      }
    }
  },
  methods: {
    handleSave() {
      this.$refs.wlForm.validate((valid) => {
        if (valid) {
          this.loading = true
          addOrUpdateUserWLTemplate(this.form).then((res) => {
            this.loading = false
            this.$emit('getWL')
            this.$emit('close')
            if (this.form.Id) {
              this.$message.success(this.$t('common:message:savedSuccessfully'))
            } else {
              this.$message.success(this.$t('common:message:addedSuccessfully'))
            }
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    handleCancel() {
      this.$emit('close')
    }
  }
}
</script>
