<template>
  <el-dialog :visible.sync="visible"
             :title="!dataForm.id ? $t('add') : $t('update')"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="接种时间"
                    prop="vaccinationTime">
        <el-date-picker v-model="dataForm.vaccinationTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        placeholder="接种时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="剂次"
                    prop="dose">
        <el-input-number v-model="dataForm.dose"
                         :min="1"
                         :max="2"
                         label="剂次，第几针"></el-input-number>
      </el-form-item>
      <el-form-item label="疫苗批次"
                    prop="batchNo">
        <el-input v-model="dataForm.batchNo"
                  placeholder="疫苗生产批次、批号"></el-input>
      </el-form-item>
      <template>
        <el-form-item label="生产企业"
                      prop="companyId">
          <el-select v-model="dataForm.companyId"
                     filterable
                     placeholder="请选择">
            <el-option v-for="item in compantyOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
      <el-form-item label="接种地点"
                    prop="siteId">
        <el-select v-model="dataForm.siteId"
                   filterable
                   placeholder="请选择">
          <el-option v-for="item in siteOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary"
                 @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      siteOptions: [],
      compantyOptions: [],
      dataForm: {
        id: '',
        dose: '',
        companyId: '',
        siteId: '',
        vaccinationTime: '',
        batchNo: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        vaccinationTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dose: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        companyId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        siteId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.getSiteOptions()
      this.getCompantyOptions()
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`sys/vaccinationinfo/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm.id = res.data.id
        this.dataForm.dose = res.data.dose
        this.dataForm.companyId = res.data.companyId
        this.dataForm.siteId = res.data.siteId
        this.dataForm.batchNo = res.data.batchNo
        this.dataForm.vaccinationTime = res.data.vaccinationTime
        console.log(this.dataForm)
      }).catch(() => { })
    },
    getSiteOptions () {
      this.$http
          .get(`sys/vaccinationsite/getSiteList`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.siteOptions = res.data
            this.vueLoading = false
            this.isAble = false
          })
          .catch(() => {
          })
    },
    getCompantyOptions () {
      this.$http
        .get(`sys/vaccinecompany/getCompantyList`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.compantyOptions = res.data
          this.vueLoading = false
          this.isAble = false
        })
        .catch(() => {
        })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('sys/vaccinationinfo/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
