<template>
  <el-dialog title="谱系" :visible.sync="outerVisible">
    <!-- 谱系信息 -->
    <el-table
      :data="dataList"
      border
      stripe
      style="width: 100%;"
      size="mini"
      :header-cell-style="{background:'rgb(245,245,245)',color:'#000',height: '45px',fontSize: '13px',fontWeight: 'normal',borderBottom: '1px solid #ccc'}" 
      :cell-style="{color: '#888',fontSize: '13px'}">
      <el-table-column
        header-align="center"
        align="center"
        label="身份">
        <template slot-scope="scope">
          <span>{{getName(scope.row.name)}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="eartag"
        header-align="center"
        align="center"
        label="耳标">
      </el-table-column>
      <el-table-column
        prop="dayAge"
        header-align="center"
        align="center"
        label="日龄">
      </el-table-column>
      <el-table-column
        prop="weight"
        header-align="center"
        align="center"
        label="体重（千克）">
      </el-table-column>
      <el-table-column
        prop="breed"
        header-align="center"
        align="center"
        label="品种">
      </el-table-column>
      <el-table-column
        prop="originPlace"
        header-align="center"
        align="center"
        label="产地">
      </el-table-column>
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="medium"
            style="color: rgb(24,144,255)"
            @click="detailsHandle(scope.row)">
            查看谱系
          </el-button>
          <el-button
            type="text"
            size="medium"
            @click="addOrUpdateHandle(scope.row)"
            style="color: rgb(24,144,255)">
            修改
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- <el-dialog
      width="30%"
      title="内层 Dialog"
      :visible.sync="innerVisible"
      append-to-body>
    </el-dialog> -->
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="outerVisible = false">关闭</el-button>
      <!-- <el-button type="primary" @click="innerVisible = true">打开内层 Dialog</el-button> -->
    </div>
  </el-dialog>
  <!-- <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="80px" 
      size="mini"
      style="margin-left: 20px;width: 500px">
      <el-form-item label="耳标" prop="eartag">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="每个耳标之间用英文逗号隔开，最多可以添加50头猪"
          v-model="dataForm.eartag"
          :disabled="dataForm.id? true: false">
        </el-input>
      </el-form-item>
      <el-form-item label="栋舍" prop="pigstyId">
        <el-select 
          v-model="dataForm.pigstyId"
          style="width: 100%">
          <el-option
            v-for="item in pigstyList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元" prop="unitId">
        <el-select 
          v-model="dataForm.unitId"
          style="width: 100%">
          <el-option
            v-for="item in unitList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="栏期" prop="periodId">
        <el-select 
          v-model="dataForm.periodId"
          style="width: 100%">
          <el-option
            v-for="item in periodList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select 
          v-model="dataForm.sex"
          style="width: 100%">
          <el-option
            v-for="item in gender"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期"
          @change="changeBirth"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="健康状态" prop="healthStatus">
        <el-select 
          v-model="dataForm.healthStatus"
          style="width: 100%">
          <el-option
            v-for="item in healthStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input-number
          v-model="dataForm.weight"
          :step="0.01"
          :max="1000000"
          :min="0"
          style="width: 91%">
        </el-input-number>
        千克
      </el-form-item>
      <el-form-item label="产地" prop="originPlace">
        <el-select 
          v-model="dataForm.originPlace"
          style="width: 100%">
          <el-option
            v-for="item in originPlaceList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="品种" prop="breed">
        <el-select 
          v-model="dataForm.breed"
          style="width: 100%">
          <el-option
            v-for="item in breedList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="养殖状态" prop="outFenceStatus">
        <el-select 
          v-model="dataForm.outFenceStatus"
          style="width: 100%">
          <el-option
            v-for="item in outFenceStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="父亲" prop="fatherEartag">
        <el-input v-model="dataForm.fatherEartag"></el-input>
      </el-form-item>
      <el-form-item label="母亲" prop="motherEartag">
        <el-input v-model="dataForm.motherEartag"></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>
  // import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        outerVisible: false,
        innerVisible: false,
        dataForm: {
          id: '',
          eartag: '',
          pigstyId: '',
          unitId: '',
          periodId: '',
          sex: '',
          birthday: '',
          healthStatus: true,
          weight: '',
          originPlace: '',
          // breed: '',
          outFenceStatus: 0,
          motherEartag: '',
          fatherEartag: ''
        },
        dataRule: {
          eartag: [
            { required: true, message: '耳标不能为空', trigger: 'blur' }
          ],
          pigstyId: [
            { required: true, message: '栋舍不能为空', trigger: 'blur' }
          ],
          unitId: [
            { required: true, message: '单元不能为空', trigger: 'blur' }
          ],
          periodId: [
            { required: true, message: '栏期不能为空', trigger: 'blur' }
          ],
          // mobile: [
          //   { required: true, message: '手机号码不能为空', trigger: 'blur' },
          //   { validator: validateMobile, trigger: 'blur' }
          // ],
          // email: [
          //   // { required: true, message: '邮箱不能为空', trigger: 'blur' },
          //   { validator: validateEmail, trigger: 'blur' }
          // ],
          // farmId: [
          //   // { required: true, message: '牧场不能为空', trigger: 'blur' }
          // ],
          // departmentCode: [
          //   // { required: true, message: '部门不能为空', trigger: 'blur' }
          // ],
          // jobCode: [
          //   // { required: true, message: '岗位不能为空', trigger: 'blur' }
          // ],
          // address: [
          //   // { required: true, message: '住址不能为空', trigger: 'blur' }
          // ],
          // roleIdList: [
          //   // { required: true, message: '角色不能为空', trigger: 'blur' }
          // ],
          // status: [
          //   // { required: true, message: '状态不能为空', trigger: 'blur' }
          // ]
        },
        gender: [
          {
            value: '公',
            label: '公'
          },
          {
            value: '母',
            label: '母'
          }
        ],
        pigstyList: [], // 栋舍List
        unitList: [], // 单元List
        unitSelectedList: [], // 单元SelectedList
        periodList: [], // 栏期List
        periodSelectedList: [], // 栏期SelectedList
        healthStatus: [
          {
            value: true,
            label: '健康'
          },
          {
            value: false,
            label: '不健康'
          }
        ],
        // breedList: [], // 品种List
        originPlaceList: [], // 产地List
        outFenceStatus: [
          {
            value: 0,
            label: '育肥'
          },
          {
            value: 1,
            label: '出栏'
          }
        ],
        dataList: []
      }
    },
    methods: {
      getName(val) {
        let names = {
          'self': '自身',
          'father': '父亲',
          'mother': '母亲',
          'fathermother': '祖母',
          'fatherfather': '祖父',
          'mothermother': '外祖母',
          'motherfather': '外祖父',
        }
        return names[val]
      },
      // 在pbentry.vue调用该方法
      init (eartag) {
        this.outerVisible = true
        this.$http({
          url: this.$http.adornUrl(`/management/yearpigbase/queryByRelationship`),
          method: "get",
          params: this.$http.adornParams({
            'eartag': eartag
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.Result.value
          } else {
            this.dataList = []
            this.$message.error(data.msg)
          }
        })
        // this.dataForm.id = ''
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
        //   if (pigs.length > 0) {
        //     console.log(pigs);
        //     let ids = []
        //     let eartags = []
        //     pigs.forEach(pig => {
        //       ids.push(pig.id)
        //       eartags.push(pig.eartag)
        //     })
        //     this.dataForm.id = ids.join()
        //     this.dataForm.eartag = eartags.join()
        //   }
        // })
        // if (pigs.length) {
        //   this.$http({
        //     url: this.$http.adornUrl(`/management/yearpigbase/info/${pigs[0].id}`),
        //     method: "get",
        //     params: this.$http.adornParams({})
        //   }).then(({data}) => {
        //     console.log(data.yearPigBase)
        //     // this.dataForm
        //   })
        // }
        // this.$http({
        //   url: this.$http.adornUrl("/management/pigsty/findAll"),
        //   method: "get",
        //   params: this.$http.adornParams({})
        // }).then(async({ data }) => {
        //   // 栋舍List
        //   this.pigstyList = []
        //   let pigstyList = []
        //   data.all && (pigstyList = data.all)
        //   pigstyList.forEach(pigsty => {
        //     let item = {
        //       value: pigsty.id,
        //       label: pigsty.number
        //     }
        //     this.pigstyList.push(item)
        //   })
        //   // 单元List
        //   this.unitList = []
        //   let unitList = []
        //   let unitR = await this.$http({
        //     url: this.$http.adornUrl("/management/unit/findAll"),
        //     method: "get",
        //     params: this.$http.adornParams({})
        //   })
        //   unitR.data.all && (unitList = unitR.data.all)
        //   unitList.forEach(unit => {
        //     let item = {
        //       value: unit.id,
        //       label: unit.number,
        //       pigstyId: unit.pigstyId,
        //       farmId: unit.farmId
        //     }
        //     this.unitList.push(item)
        //   })
        //   this.unitSelectedList = this.unitList
        //   // 栏期List
        //   this.periodList = []
        //   let periodList = []
        //   let periodR = await this.$http({
        //     url: this.$http.adornUrl("/management/period/findAll"),
        //     method: "get",
        //     params: this.$http.adornParams({})
        //   })
        //   periodR.data.all && (periodList = periodR.data.all)
        //   periodList.forEach(period => {
        //     let item = {
        //       value: period.id,
        //       label: period.number,
        //       pigstyId: period.pigstyId
        //     }
        //     this.periodList.push(item)
        //   })
        //   this.periodSelectedList = this.periodList




          
        // })
      },
      // // 表单提交
      // dataFormSubmit () {
      //   this.$refs['dataForm'].validate((valid) => {
      //     console.log(valid);
      //     if (valid) {
      //       this.$http({
      //         url: this.$http.adornUrl(`${!this.dataForm.id ?'/management/yearpigbase/batchLairage': '/management/yearpigbase/update'}`),
      //         method: 'post',
      //         data: this.$http.adornData({
      //           'id': this.dataForm.id || undefined,
      //           'eartag': this.dataForm.eartag,
      //           'pigstyId': this.dataForm.pigstyId,
      //           'unitId': this.dataForm.unitId,
      //           'periodId': this.dataForm.periodId,
      //           'sex': this.dataForm.sex,
      //           'birthday': this.dataForm.birthday,
      //           'healthStatus': this.dataForm.healthStatus,
      //           'weight': this.dataForm.weight,
      //           'originPlace': this.dataForm.originPlace,
      //           // 'breed': this.dataForm.breed,
      //           'outFenceTime': this.dataForm.outFenceTime,
      //           'motherEartag': this.dataForm.motherEartag,
      //           'fatherEartag': this.dataForm.fatherEartag
      //         })
      //       }).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.$confirm(`出生日期有误`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.dataForm.birthday = ''
            console.log("还没出生呢...");
          }).catch(() => {});
        }
      }
    }
  }
</script>
<style scoped>
/deep/ .el-table--mini td, .el-table--mini th {
  padding: 3px 0 !important;
  height: 20px !important;
}
</style>
