<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="600px">
    <el-form 
      :model="dataForm" 
      :rules="dataRule" 
      ref="dataForm" 
      @keyup.enter.native="dataFormSubmit()" 
      label-width="100px" 
      size="mini"
      style="margin-left: 20px;width: 500px">
      <el-form-item label="耳标" prop="eartagNo">
        <el-input
          v-model="dataForm.eartagNo"
          :disabled="dataForm.id? true: false">
        </el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" style="width: 100%">
          <el-option
            v-for="item in type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker
          v-model="dataForm.birthDate"
          type="date"
          value-format="yyyy-MM-dd"
          @change="changeBirth"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="品种" prop="blood">
        <el-select v-model="dataForm.blood" style="width: 100%">
          <el-option
            v-for="item in bloodList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="源产地" prop="origin">
        <el-select v-model="dataForm.origin" style="width: 100%">
          <el-option
            v-for="item in originList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="产仔窝数" prop="batchNp" v-if="dataForm.type!==1">
        <el-input v-model.number="dataForm.batchNp" placeholder="请输入数字">
        </el-input>
      </el-form-item>
      <el-form-item label="产仔总数量" prop="pigletTotal" v-if="dataForm.type!==1">
        <el-input 
          v-model.number="dataForm.pigletTotal" 
          placeholder="请输入数字">
        </el-input>
      </el-form-item>
      <el-form-item label="PSY" prop="psy" v-if="dataForm.type!==1">
        <el-input 
          v-model="dataForm.psy" 
          placeholder="请输入小数">
        </el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark">
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">关闭</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          eartagNo: '',
          type: '',
          birthDate: '',
          blood: '',
          // origin: '',
          batchNp: '',
          pigletTotal: '',
          psy: '',
          remark: ''
        },
        dataRule: {
          eartagNo: [
            { required: true, message: '耳标不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          birthDate: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          blood: [
            { required: true, message: '种类不能为空', trigger: 'blur' }
          ],
          origin: [
            { required: true, message: '源产地不能为空', trigger: 'blur' }
          ],
          psy: [
            { type: 'string' , message: '只允许输入小数', pattern: /^[0-9]{1,2}\.[0-9]{1,}$/ }
          ]
        },
        bloodList: [], // 品种list
        // originList: [], // 源产地list
        type: [
          {
            value: 1,
            label: '种公猪'
          }, 
          {
            value: 2,
            label: '母猪'
          },
          {
            value: 3,
            label: '后备母猪'
          }
        ],
      }
    },
    methods: {
      // 在pbentry.vue调用该方法
      init (id) {
        this.visible = true
        this.dataForm.id = id || 0
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/management/breedparents/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams({}, false)
          }).then(async ({data}) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.breedParents.id
              this.dataForm.eartagNo = data.breedParents.eartagNo
              this.dataForm.type = data.breedParents.type
              this.dataForm.birthDate = data.breedParents.birthDate
              this.dataForm.blood = data.breedParents.blood
              this.dataForm.origin = data.breedParents.origin
              this.dataForm.remark = data.breedParents.remark
              this.dataForm.batchNp = data.breedParents.batchNp
              this.dataForm.pigletTotal = data.breedParents.pigletTotal
              this.dataForm.psy = data.breedParents.psy
            }
          })
        
        }
        // 品种list + 产地list
        this.$http({
          url: this.$http.adornUrl(`/management/basebloodline/findAll`),
          method: 'get',
          params: this.$http.adornParams({}, false)
        }).then(({data}) => {
          this.bloodList = []
          // this.originList = []
          let breedList = []
          if (data && data.code === 0) {
            breedList = data.all
            breedList.forEach(breed => {
              let item = {
                value: breed.name,
                label: breed.name,
                origin: breed.origin
              }
              // let item02 = {
              //   value: breed.origin,
              //   label: breed.origin
              // }
              this.bloodList.push(item)
              // this.originList.push(item02)
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/management/breedparents/${!this.dataForm.id ?'save': 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'eartagNo': this.dataForm.eartagNo,
                'farmId': parseInt(this.$cookie.get('formFarmId')),
                'type': this.dataForm.type,
                'gender': this.dataForm.type < 2? 1: 2,
                'birthDate': this.dataForm.birthDate,
                'blood': this.dataForm.blood,
                'origin': this.bloodList.filter(item => item.label === this.dataForm.blood)[0].origin,
                'batchNp': this.dataForm.batchNp || undefined,
                'pigletTotal': this.dataForm.pigletTotal || undefined,
                'psy': this.dataForm.psy || undefined,
                'remark': this.dataForm.remark || undefined
              }, false)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.visible = false
                this.$emit('refreshDataList')
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  // onClose: () => {
                  //   this.visible = false
                  //   this.$emit('refreshDataList')
                  // }
                })
                // this.resetForm()
                this.visible = false
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取出生日期
      changeBirth(val) {
        let birth = new Date(val);
        let now = new Date();
        let days = now.getTime() - birth.getTime();
        if (days < 0) {
          this.dataForm.birthDate = ''
          this.$confirm(`出生日期有误`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.dataForm.birthday = ''
            console.log("还没出生呢...");
          }).catch(() => {});
        }
      }
    },
    watch: {
      'dataForm.pigstyId': {
        handler(newValue, oldValue) {
          if (oldValue) {
            this.dataForm.unitId = ''
            this.dataForm.periodId = ''
          }
        }
      }
    }
  }
</script>
