<template>
  <div class="band">
    <div class="header_title">存栏走势</div>
    <div style="padding-top: 60px;">
      <div class="box">
        <div class="box_header">
          <span>母猪存栏统计</span>
          <div class="right">
            <el-date-picker
                v-model="timeSelected"
                v-show="daySelected === 1"
                type="daterange"
                range-separator="—"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="onChange"
                value-format="yyyy-MM-dd"
                size="mini">
            </el-date-picker>
            <el-date-picker
                v-model="monthSelected"
                v-show="daySelected === 2"
                type="monthrange"
                range-separator="至"
                start-placeholder="开始月份"
                end-placeholder="结束月份"
                @change="onChange"
                size="mini"
                value-format="yyyy-MM">
            </el-date-picker>
            <div v-show="daySelected === 3">
              <el-date-picker
                  v-model="startYear"
                  type="year"
                  placeholder="选择年"
                  size="mini"
                  style="width: 100px;"
                  value-format="yyyy">
              </el-date-picker>
              -
              <el-date-picker
                  v-model="endYear"
                  type="year"
                  placeholder="选择年"
                  size="mini"
                  @change="onChange"
                  style="width: 100px;"
                  value-format="yyyy">
              </el-date-picker>
            </div>
          </div>
          <div class="right">
            <span :class="{'scopeEle': true, 'ifScoped': daySelected === 1, 'borderLeft': true}" @click="scopeDay(1)">日</span>
            <span :class="{'scopeEle': true, 'ifScoped': daySelected === 2}" @click="scopeDay(2)">月</span>
            <span :class="{'scopeEle': true, 'ifScoped': daySelected === 3, 'borderRight': true}" @click="scopeDay(3)">年</span>
          </div>
        </div>
        <div class="box_content">
          <chart-mzcl :mzclList="mzclList"></chart-mzcl>
        </div>
      </div>
      <div class="box">
        <div class="box_header">
          <span>母猪新增-离线走势图</span>
          <div class="right">
            <el-date-picker
                v-model="timeSelected"
                v-show="daySelected === 1"
                type="daterange"
                range-separator="—"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="onChange"
                value-format="yyyy-MM-dd"
                size="mini">
            </el-date-picker>
            <el-date-picker
                v-model="monthSelected"
                v-show="daySelected === 2"
                type="monthrange"
                range-separator="至"
                start-placeholder="开始月份"
                end-placeholder="结束月份"
                @change="onChange"
                size="mini"
                value-format="yyyy-MM">
            </el-date-picker>
            <div v-show="daySelected === 3">
              <el-date-picker
                  v-model="startYear"
                  type="year"
                  placeholder="选择年"
                  size="mini"
                  style="width: 100px;"
                  value-format="yyyy">
              </el-date-picker>
              -
              <el-date-picker
                  v-model="endYear"
                  type="year"
                  placeholder="选择年"
                  size="mini"
                  @change="onChange"
                  style="width: 100px;"
                  value-format="yyyy">
              </el-date-picker>
            </div>
          </div>
          <div class="right">
            <span :class="{'scopeEle': true, 'ifScoped': daySelected === 1, 'borderLeft': true}" @click="scopeDay(1)">日</span>
            <span :class="{'scopeEle': true, 'ifScoped': daySelected === 2}" @click="scopeDay(2)">月</span>
            <span :class="{'scopeEle': true, 'ifScoped': daySelected === 3, 'borderRight': true}" @click="scopeDay(3)">年</span>
          </div>
        </div>
        <div class="box_main">
          <chart-mzxz :mzxzList="mzxzList"></chart-mzxz>
        </div>
      </div>
    </div>
<!--    <div class="box">-->
<!--      <div class="box_header">公猪存栏走势</div>-->
<!--      <div class="box_main">-->
<!--        <chart-gzxz></chart-gzxz>-->
<!--      </div>-->
<!--    </div>-->
  </div>
</template>

<script>
import chartMzcl from "./chart/chartMzcl";
import chartMzxz from "./chart/chartMzxz";
// import chartGzxz from "./chart/chartGzxz";
export default {
  name: "hand",
  components: {
    chartMzcl,
    chartMzxz,
    // chartGzxz
  },
  data() {
    return {
      daySelected: 1, // 年月日选中值，默认月
      timeSelected: [], // 时间选择器选中的时间
      monthSelected: [], // 月份选择
      startYear: '',
      endYear: '',
      farmCode: '330703010000422',
      mzclList: [],
      mzxzList: [],
    }
  },
  methods: {
    init() {
      let params = {
        farmCode: this.farmCode,
        type: this.daySelected,
      }
      if(this.daySelected === 1) {
        params.startDate = this.timeSelected[0];
        params.endDate = this.timeSelected[1];
      } else if(this.daySelected === 2) {
        params.startDate = this.monthSelected[0];
        params.endDate = this.monthSelected[1];
      } else if(this.daySelected === 3) {
        params.startDate = this.startYear;
        params.endDate = this.endYear
      }
      this.$http({
        url: this.$http.adornUrl('http://122.112.224.199:9000/manager/dailytabulatedata/countAllChange'),
        method: 'get',
        params: this.$http.adornParams(params)
      })
      .then(res => {
        if(res.data.code === 0) {
          let arr = [];
          let arr1 = [];
          let arr2 = [];
          let arr3 = [];
          let arr4 = [];
          let arr5 = [];
          res.data.page.forEach(item => {
            arr.push(item.ydate);
            arr1.push(item.stockNew);
            arr2.push(item.offlineNo);
            // 配怀
            arr3.push(item.stock1);
            // 分娩
            arr4.push(item.stock2);
            // 后备
            arr5.push(item.stock3);
          })
          let time = arr.reverse()
          let stockNew = arr1.reverse();
          let offlineNo = arr2.reverse()
          let stock1 = arr3.reverse();
          let stock2 = arr4.reverse();
          let stock3 = arr5.reverse()
          this.mzclList = [time, stock1, stock2, stock3];
          this.mzxzList = [time, stockNew, offlineNo];
        } else {
          this.mzclList = [];
        }
      })
    },
    onChange() {
      if(this.daySelected === 3) {
        if(this.startYear > this.endYear) {
          this.$message.error('起始年份不能大于终止年份')
          this.startYear = '';
          this.endYear = '';
          return false;
        } else {
          this.init()
        }
      } else {
        this.init()
      }
    },
    scopeDay(val) {
      this.daySelected = val
      this.init();
      // switch (val) {
      //   case 1: // 日
      //     this.init()
      //     break;
      //   case 2: // 月
      //     this.init()
      //     break;
      //   case 3: // 年
      //     this.init()
      //     break;
      //   default:
      //     break;
      // }
    }
  },
  mounted() {
    this.init();
  }
}
</script>

<style scoped>
  .band {
    width: 100%;
    box-sizing: border-box;
    /*padding: 20px 0;*/
  }
  .box {
    border: 1px solid #ddd;
    box-sizing: border-box;
    margin-bottom: 20px;
  }
  .box_header {
    box-sizing: border-box;
    padding: 10px 20px;
    background-color: #F3F3F3;
    height: 50px;
    border-bottom: 1px solid #ddd;
  }
  .box_content {
    box-sizing: border-box;
    padding: 10px 20px;
    background-color: #fff;
    height: 500px;
  }
  .box_main {
    box-sizing: border-box;
    padding: 10px 20px;
    background-color: #fff;
    height: 340px;
  }
  .right {
    float: right;
    margin-right: 20px;
    font-size: 12px;
  }
  /** 年月日选择 */
  .scopeEle {
    display: inline-block;
    border: 1px solid #ccc;
    width: 75px;
    text-align: center;
    height: 26px;
    line-height: 26px;
    margin-left: -1px;
    cursor: pointer;
    background-color: #fff;
  }
  .borderLeft {
    border-radius: 5px 0 0 5px;
  }
  .borderRight {
    border-radius: 0 5px 5px 0 ;
  }
  /* 如果选中了 */
  .ifScoped {
    background-color: #1abc9c;
    color: #fff;
    border: 1px solid #1abc9c;
  }
  .header_title {
    position: fixed;
    width: 1640px;
    height: 40px;
    background-color: #F7F7F7;
    border: 1px solid #ddd;
    line-height: 40px;
    color: #6FA8C8;
    margin-bottom: 10px;
    padding-left: 20px;
    z-index: 99;
  }
</style>
