<template>
  <el-container>
    <el-main class="containMain">
      <div class="contentTitle">{{pageTitle}}</div>
      <el-form ref="form" :model="formData" label-width="80px" style="margin-top:20px;">
        <el-form-item label label-width="0">
          <el-autocomplete prefix-icon="el-icon-search" v-model="formData.cowID" :fetch-suggestions="queryCow" placeholder="请输入牛号" style="width:300px" clearable @select="getNowCow"></el-autocomplete>
          <el-button type="primary" @click="getNowCow" style="width:90px;margin-left:12px">确认</el-button>
        </el-form-item>
        <div class="sub" v-if="tableData.length>0">操作牛号：{{tableData[0].value}}</div>
        <div class="subStr">只能上传JPG / PNG文件，尺寸比例750x750px，且不超过500KB</div>
        <div class="list">
          <div class="item">
            <el-upload action class="avatar-uploader" :show-file-list="false" :before-upload="beforeLeft">
              <img v-if="formData.left" :src="formData.left" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <p>左侧照片</p>
          </div>
          <div class="item">
            <el-upload action class="avatar-uploader" :show-file-list="false" :before-upload="beforeFront">
              <img v-if="formData.front" :src="formData.front" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <p>正面照片</p>
          </div>
          <div class="item">
            <el-upload action class="avatar-uploader" :show-file-list="false" :before-upload="beforeRight">
              <img v-if="formData.right" :src="formData.right" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <p>右侧照片</p>
          </div>
        </div>

        <div style="margin-top:80px">
          <el-button @click="onSave" type="primary" style="width:98px;" size="mediu">提交信息</el-button>
        </div>
      </el-form>
    </el-main>
    <el-aside class="containAside" width="450px">
      <div class="asideInfo">
        <div class="contentTitle">牛只信息</div>
        <el-table class="handleTable" border :data="tableData" style="width: 100%" :height="(allHeight-120)+'px'" size="small">
          <el-table-column prop="key" label="参数"></el-table-column>
          <el-table-column prop="value" label="当前牛只信信息"></el-table-column>
        </el-table>
      </div>
    </el-aside>
  </el-container>
</template>
<script>
import formValidator from '../formValidator'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  created() {

  },
  data() {
    return {
      saving: false,
      formData: {
        cowID: '',
        left: '',
        front: '',
        right: ''
      },
      tableData: []
    }
  },
  computed: {
    ...mapState(['userInfo', 'allHeight']),
    pageTitle() {
      return this.$route.meta.title
    }
  },
  watch: {
    'formData.cowID'() {

      if (this.formData.cowID.length != 8) {
        return
      }
      this.getNowCow()
    }
  },
  methods: {
    beforeLeft(flie) {
      var reader = new FileReader();
      reader.onload = (e) => {
        this.formData.left = reader.result
      }
      reader.readAsDataURL(flie)

      return false
    },
    beforeFront(flie) {
      var reader = new FileReader();
      reader.onload = (e) => {
        this.formData.front = reader.result
      }
      reader.readAsDataURL(flie)

      return false
    },
    beforeRight(flie) {
      var reader = new FileReader();
      reader.onload = (e) => {
        this.formData.right = reader.result
      }
      reader.readAsDataURL(flie)

      return false
    },
    onSave() {
      if (this.formData.cowID.length != 8) {
        this.$message({
          message: '请录入牛只牛号',
          type: 'warning'
        });
        return
      }

      if (!this.formData.left) {
        this.$message({
          message: '请录入牛只左侧照片',
          type: 'warning'
        });
        return
      }

      if (!this.formData.front) {
        this.$message({
          message: '请录入牛只正面照片',
          type: 'warning'
        });
        return
      }
      if (!this.formData.right) {
        this.$message({
          message: '请录入牛只右侧照片',
          type: 'warning'
        });
        return
      }

      this.savePhoto(0)


    },
    savePhoto(index) {
      if (index >= 3) {
        this.$message({
          message: '提交成功',
          type: 'success'
        });
        this.formData = {
          cowID: '',
          left: '',
          front: '',
          right: ''
        }
        return
      }
      var formData = new FormData()
      formData.append('cowId', this.formData.cowID)
      formData.append('operators', this.userInfo.username)
      if (index == 0) {
        formData.append('photo', this.formData.left)
        formData.append('description', '牛只三面照L')
      }
      else if (index == 1) {
        formData.append('photo', this.formData.front)
        formData.append('description', '牛只三面照F')
      }
      else if (index == 2) {
        formData.append('photo', this.formData.right)
        formData.append('description', '牛只三面照R')
      }


      axios.post('http://118.178.226.110:8090/cowOut/addPhotoInfo', formData, {
        withCredentials: false
      }).then(res => {
        if (res.data.success) {
          this.savePhoto(index + 1)
        }
        else {
          this.$message({
            message: res.data.error,
            type: 'warning'
          });
        }


      })

      setTimeout(() => {

      }, 100)
    },
    getNowCow() {
      this.getHaleInfo('wsVeterinary', 'getFeedMagnetByCowID', [this.formData.cowID], 'json').then((data) => {
        // console.log(data)
        data = this.JsonSort(['牛号', '当前牛舍', '繁殖状态', '泌乳状态', '投喂磁石日期', '投喂磁石人'], data)
        var list = []
        Object.keys(data).forEach(item => {
          list.push({
            key: item,
            value: data[item]
          })
        })

        // console.log(list)

        this.tableData = list

      }).catch(() => {
        this.formData.cowID = ''
      })
    },

  }
}
</script>
<style lang="less" scoped>
.containMain {
  background: rgba(255, 255, 255, 1);
  border-radius: 4px;
  padding: 15px;
  // margin-right: 10px;
  .contentTitle {
    border-bottom: 1px solid #f2f4f5;
  }
  // min-width: 450px;

  .sub {
    font-size: 14px;
    font-weight: 600;
    color: rgba(102, 102, 102, 1);
    line-height: 20px;
    margin-bottom: 40px;
    padding-left: 20px;
  }
}

.contentTitle {
  font-size: 16px;
  font-weight: bold;
  color: rgba(0, 0, 0, 1);
  line-height: 22px;
  letter-spacing: 1px;
  padding-bottom: 14px;
}

.containAside {
  padding: 0;
  margin-left: 10px;
  display: flex;
  flex-direction: column;
  .asideInfo {
    flex-grow: 1;
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    padding: 15px;
  }
}

.subStr {
  height: 14px;
  font-size: 12px;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: rgba(144, 147, 153, 1);
  line-height: 14px;
  margin-top: 50px;
  margin-left: 15px;
}

.list {
  display: flex;
  margin-top: 10px;
  .item {
    text-align: center;
    margin-right: 20px;
  }
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-color: #fbfdff;
  &:hover {
    border-color: #409eff;
  }
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  &:hover {
    border-color: #409eff;
  }
}

.wrapper {
  position: relative;
}

.single-upload .el-upload {
  position: relative;
}

.loading {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.6);
  color: #333;
  font-size: 24px;
}
</style>
