<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="库存统计" name="first">
        <div class="search">
          <el-date-picker v-model="tab.table.getdataListParm.parammaps.inputDatetime" :clearable="false" class="inputDatetime filter-item" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="float: left;width: 250px;margin-bottom:10px;" :picker-options="pickerOptions" />
          <el-input v-model="tab.table.getdataListParm.parammaps.feedname" class="filter-item" style="float: left;width: 245px;margin-bottom:10px;" placeholder="饲料名称" />
          <el-button class="successBorder" style="float: left;margin-left: 10px;margin-bottom:10px;" @click="form_search('first')">查询</el-button>
          <el-button class="export" style="float: right;margin-right: 10px;margin-bottom:10px;" @click="form_export">导出</el-button>
        </div>
        <div class="table">
          <el-table :key="tab.table.tableKey" v-loading="tab.table.listLoading" element-loading-text="给我一点时间" :data="tab.table.list" border fit highlight-current-row style="width: 100%;" :row-style="rowStyle" :cell-style="cellStyle" class="elTable table-fixed">
            <el-table-column label="饲料名称" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.feedname }}</span>
              </template>
            </el-table-column>
            <el-table-column label="期初" min-width="130px" align="center">
              <el-table-column prop="startsum" align="center" label="期初库存（kg）" min-width="120" />
              <el-table-column prop="startprice" align="center" label="期初金额（元）" min-width="120" />
            </el-table-column>
            <el-table-column label="用量" min-width="110px" align="center">
              <el-table-column prop="laidsum" align="center" label="入库重量（kg）" min-width="120" />
              <el-table-column prop="usesumXT" align="center" label="系统出库重量（kg）" min-width="120" />
              <el-table-column prop="usesumRG" align="center" label="人工用料重量（kg）" min-width="120" />
              <el-table-column prop="usesumXH" align="center" label="损耗重量（kg）" min-width="120" />
            </el-table-column>
            <el-table-column label="期末" min-width="130px" align="center">
              <el-table-column prop="stopsum" align="center" label="期末库存（kg）" min-width="120" />
              <el-table-column prop="stopprice" align="center" label="期末金额（元）" min-width="120" />
            </el-table-column>
          </el-table>
          <pagination v-show="tab.table.total>=0" :total="tab.table.total" :page.sync="tab.table.getdataListParm.offset" :limit.sync="tab.table.getdataListParm.pagecount" @pagination="getTabList" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="用料分析" name="second">
        <div class="search">
          <el-date-picker v-model="tab2.table.getdataListParm.parammaps.inputDatetime" :clearable="false" class="inputDatetime filter-item" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" :picker-options="pickerOptions" />
          <span style="margin-left: 10px;">统计类型：</span>
          <el-radio v-model="tab2.radio" label="1" @change="changeRadio2">牲畜类别</el-radio>
          <el-radio v-model="tab2.radio" label="2" @change="changeRadio2">栏舍名称</el-radio>
          <el-radio v-model="tab2.radio" label="3" @change="changeRadio2">日期</el-radio>
          <el-radio v-model="tab2.radio" label="4" @change="changeRadio2">TMR设备编号</el-radio>
          <el-radio v-model="tab2.radio" label="5" @change="changeRadio2">班次</el-radio>
          <el-radio v-model="tab2.radio" label="6" @change="changeRadio2">车次</el-radio>
          <el-input v-if="tab2.isRadio1" v-model="tab2.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="牲畜类别" />
          <el-input v-if="tab2.isRadio2" v-model="tab2.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="栏舍名称" />
          <el-input v-if="tab2.isRadio3" v-model="tab2.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="日期" />
          <el-input v-if="tab2.isRadio4" v-model="tab2.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="TMR设备编号" />
          <el-input v-if="tab2.isRadio5" v-model="tab2.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="班次" />
          <el-input v-if="tab2.isRadio6" v-model="tab2.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="车次" />
          <el-button class="successBorder" @click="form_search('second')">查询</el-button>
        </div>
        <el-table :key="tab2.table.tableKey" v-loading="tab2.table.listLoading" element-loading-text="给我一点时间" :data="tab2.table.list" border fit highlight-current-row style="width: 100%;" :row-style="rowStyle" :cell-style="cellStyle" class="elTable table-fixed">
          <el-table-column v-for="(item,index) in tab2.table.tableConfig" align="center" :label="item.label" :prop="item.prop">
            <el-table-column v-for="item1 in item.children" v-if="item.children || item.children.length>0" align="center" :label="item1.label" :prop="item1.prop">
              <el-table-column v-for="item2 in item1.children" v-if="item1.children || item1.children.length>0" :label="item2.label" :prop="item2.prop" />
            </el-table-column>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="价格分析" name="third">
        <div class="search">
          <el-date-picker v-model="tab3.table.getdataListParm.parammaps.inputDatetime" class="inputDatetime filter-item" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" :picker-options="pickerOptions" />
          <span style="margin-left: 10px;">统计类型：</span>
          <el-radio v-model="tab3.radio" label="1" @change="changeRadio3">牲畜类别</el-radio>
          <el-radio v-model="tab3.radio" label="2" @change="changeRadio3">栏舍名称</el-radio>
          <el-radio v-model="tab3.radio" label="3" @change="changeRadio3">日期</el-radio>
          <el-radio v-model="tab3.radio" label="4" @change="changeRadio3">TMR设备编号</el-radio>
          <el-input v-if="tab3.isRadio1" v-model="tab3.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="牲畜类别" />
          <el-input v-if="tab3.isRadio2" v-model="tab3.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="栏舍名称" />
          <el-input v-if="tab3.isRadio3" v-model="tab3.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="日期" />
          <el-input v-if="tab3.isRadio4" v-model="tab3.table.getdataListParm.parammaps.fname" class="filter-item" style="width: 245px;" placeholder="TMR设备编号" />
          <el-button class="successBorder" @click="form_search('third')">查询</el-button>
        </div>
        <el-table :key="tab3.table.tableKey" v-loading="tab3.table.listLoading" element-loading-text="给我一点时间" :data="tab3.table.list" border fit highlight-current-row style="width: 100%;" :row-style="rowStyle" :cell-style="cellStyle" class="elTable table-fixed">
          <el-table-column v-for="(item,index) in tab3.table.tableConfig" align="center" :label="item.label" :prop="item.prop">
            <el-table-column v-for="item1 in item.children" v-if="item.children || item.children.length>0" align="center" :label="item1.label" :prop="item1.prop">
              <el-table-column v-for="item2 in item1.children" v-if="item1.children || item1.children.length>0" :label="item2.label" :prop="item2.prop" />
            </el-table-column>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
  </div>

</template>

<script>
import { GetDataByName, GetReportform } from '@/api/common'
import Cookies from 'js-cookie'
import { parseTime } from '@/utils/index.js'
import Pagination from '@/components/Pagination'
export default {
  name: 'InventoryManagement',
  components: { Pagination },
  data() {
    return {
      pickerMinDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.pickerMinDate = minDate.getTime()
          if (maxDate) {
            this.pickerMinDate = ''
          }
        },
        // 限制不能选择今天之后的日期
        disabledDate: (time) => {
          if (this.pickerMinDate !== '') {
            const one = 31 * 24 * 3600 * 1000
            const minTime = this.pickerMinDate - one
            let maxTime = this.pickerMinDate + one
            if (maxTime > new Date()) {
              maxTime = new Date()
            }
            return time.getTime() < minTime || time.getTime() > maxTime
          }
          return time.getTime() > Date.now()
        }
      },
      fileComment: [{ 'label': '商务文件评分', 'children': [{ 'label': '1' }, { 'label': '2' }, { 'label': '管理员' }] }],
      activeName: 'first',
      tab: {
        chartDate: [],
        table: {
          getdataListParm: {
            name: 'getfsStaticlist',
            page: 1,
            offset: 1,
            pagecount: 10,
            returntype: 'Map',
            parammaps: {
              pastureid: Cookies.get('pastureid'),
              startTime: parseTime(new Date(), '{y}-{m}-{d}'),
              stopTime: parseTime(new Date(), '{y}-{m}-{d}'),
              inputDatetime: [new Date(), new Date()],
              feedname: ''
            }
          },
          tableKey: 1,
          list: [],
          total: 0,
          listLoading: true
        }
      },

      tab2: {
        chartDate: [],
        table: {
          getdataListParm: {
            name: 'getMaterialAnalysisSC',
            page: 1,
            offset: 1,
            pagecount: '',
            returntype: 'Map',
            parammaps: {
              pastureid: Cookies.get('pastureid'),
              startTime: parseTime(new Date(), '{y}-{m}-{d}'),
              stopTime: parseTime(new Date(), '{y}-{m}-{d}'),
              inputDatetime: [new Date(), new Date()],
              fname: ''
            }
          },
          tableKey: 1,
          list: [],
          tableConfig: [],
          total: 0,
          listLoading: true
        },
        radio: '1',
        isRadio1: true,
        isRadio2: false,
        isRadio3: false,
        isRadio4: false,
        isRadio5: false,
        isRadio6: false
      },
      tab3: {
        chartDate: [],
        table: {
          getdataListParm: {
            name: 'getPriceAnalysisSC',
            page: 1,
            offset: 1,
            pagecount: 10,
            returntype: 'Map',
            parammaps: {
              pastureid: Cookies.get('pastureid'),
              startTime: parseTime(new Date(), '{y}-{m}-{d}'),
              stopTime: parseTime(new Date(), '{y}-{m}-{d}'),
              inputDatetime: [new Date(), new Date()],
              fname: ''
            }
          },
          tableKey: 1,
          list: [],
          tableConfig: [],
          total: 0,
          listLoading: true
        },
        radio: '1',
        isRadio1: true,
        isRadio2: false,
        isRadio3: false,
        isRadio4: false
      },
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }

    }
  },

  created() {
    this.getTabList()
    this.getTimeFn()
  },

  methods: {
    getTimeFn() {
      const that = this
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      that.tab.chartDate[0] = parseTime(start, '{y}-{m}-{d}')
      that.tab.chartDate[1] = parseTime(end, '{y}-{m}-{d}')
      that.tab.table.getdataListParm.parammaps.inputDatetime[0] = parseTime(start, '{y}-{m}-{d}')
      that.tab.table.getdataListParm.parammaps.inputDatetime[1] = parseTime(end, '{y}-{m}-{d}')
      that.tab.table.getdataListParm.parammaps.startTime = parseTime(start, '{y}-{m}-{d}')
      that.tab.table.getdataListParm.parammaps.stopTime = parseTime(end, '{y}-{m}-{d}')
      that.tab2.chartDate[0] = parseTime(start, '{y}-{m}-{d}')
      that.tab2.chartDate[1] = parseTime(end, '{y}-{m}-{d}')
      that.tab2.table.getdataListParm.parammaps.inputDatetime[0] = parseTime(start, '{y}-{m}-{d}')
      that.tab2.table.getdataListParm.parammaps.inputDatetime[1] = parseTime(end, '{y}-{m}-{d}')
      that.tab2.table.getdataListParm.parammaps.startTime = parseTime(start, '{y}-{m}-{d}')
      that.tab2.table.getdataListParm.parammaps.stopTime = parseTime(end, '{y}-{m}-{d}')
      that.tab3.chartDate[0] = parseTime(start, '{y}-{m}-{d}')
      that.tab3.chartDate[1] = parseTime(end, '{y}-{m}-{d}')
      that.tab3.table.getdataListParm.parammaps.inputDatetime[0] = parseTime(start, '{y}-{m}-{d}')
      that.tab3.table.getdataListParm.parammaps.inputDatetime[1] = parseTime(end, '{y}-{m}-{d}')
      that.tab3.table.getdataListParm.parammaps.startTime = parseTime(start, '{y}-{m}-{d}')
      that.tab3.table.getdataListParm.parammaps.stopTime = parseTime(end, '{y}-{m}-{d}')
    },
    handleTabClick() {
      if (this.activeName == 'first') {
        this.getTabList()
      } else if (this.activeName == 'second') {
        this.getTab2List()
      } else if (this.activeName == 'third') {
        this.getTab3List()
      }
    },
    getTabList() {
      this.tab.table.listLoading = true
      GetDataByName(this.tab.table.getdataListParm).then(response => {
        console.log('库存统计table数据', response.data.list)
        if (response.data.list !== null) {
          this.tab.table.list = response.data.list
          this.tab.table.total = response.data.total
        } else {
          this.tab.table.list = []
        }
        setTimeout(() => {
          this.tab.table.listLoading = false
        }, 100)
      })
    },
    form_search(item) {
      if (item == 'first') {
        if (this.tab.table.getdataListParm.parammaps.inputDatetime !== '' && this.tab.table.getdataListParm.parammaps.inputDatetime !== null) {
          this.tab.table.getdataListParm.parammaps.startTime = parseTime(this.tab.table.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
          this.tab.table.getdataListParm.parammaps.stopTime = parseTime(this.tab.table.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
        } else {
          this.tab.table.getdataListParm.parammaps.inputDatetime = ''
          this.tab.table.getdataListParm.parammaps.startTime = ''
          this.tab.table.getdataListParm.parammaps.stopTime = ''
        }
        this.getTabList()
      } else if (item == 'second') {
        if (this.tab2.table.getdataListParm.parammaps.inputDatetime !== '' && this.tab2.table.getdataListParm.parammaps.inputDatetime !== null) {
          this.tab2.table.getdataListParm.parammaps.startTime = parseTime(this.tab2.table.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
          this.tab2.table.getdataListParm.parammaps.stopTime = parseTime(this.tab2.table.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
        } else {
          this.tab2.table.getdataListParm.parammaps.inputDatetime = ''
          this.tab2.table.getdataListParm.parammaps.startTime = ''
          this.tab2.table.getdataListParm.parammaps.stopTime = ''
        }
        this.getTab2List()
      } else if (item == 'third') {
        if (this.tab3.table.getdataListParm.parammaps.inputDatetime !== '' && this.tab3.table.getdataListParm.parammaps.inputDatetime !== null) {
          this.tab3.table.getdataListParm.parammaps.startTime = parseTime(this.tab3.table.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
          this.tab3.table.getdataListParm.parammaps.stopTime = parseTime(this.tab3.table.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
        } else {
          this.tab3.table.getdataListParm.parammaps.inputDatetime = ''
          this.tab3.table.getdataListParm.parammaps.startTime = ''
          this.tab3.table.getdataListParm.parammaps.stopTime = ''
        }
        this.getTab3List()
      }
    },
    form_export() { },
    // 用料分析
    getTab2List() {
      this.tab2.table.listLoading = true
      GetReportform(this.tab2.table.getdataListParm).then(response => {
        console.log('库存统计-用料分析', response.data.list)
        if (response.data.list !== null) {
          this.tab2.table.list = response.data.list.data1
          this.tab2.table.tableConfig = response.data.list.data2
          this.tab2.table.total = response.data.total
        } else {
          this.tab2.table.list = []
        }
        setTimeout(() => {
          this.tab2.table.listLoading = false
        }, 100)
      })
    },
    changeRadio2() {
      console.log(this.tab2.radio)
      if (this.tab2.radio == '1') {
        this.tab2.isRadio1 = true,
          this.tab2.isRadio2 = false,
          this.tab2.isRadio3 = false,
          this.tab2.isRadio4 = false,
          this.tab2.isRadio5 = false,
          this.tab2.isRadio6 = false,
          this.tab2.table.getdataListParm.name = 'getMaterialAnalysisSC'
        this.tab2.table.getdataListParm.offset = 1
        this.getTab2List()
      } else if (this.tab2.radio == '2') {
        this.tab2.isRadio1 = false,
          this.tab2.isRadio2 = true,
          this.tab2.isRadio3 = false,
          this.tab2.isRadio4 = false,
          this.tab2.isRadio5 = false,
          this.tab2.isRadio6 = false,
          this.tab2.table.getdataListParm.name = 'getMaterialAnalysisLS'
        this.tab2.table.getdataListParm.offset = 1
        this.getTab2List()
      } else if (this.tab2.radio == '3') {
        this.tab2.isRadio1 = false,
          this.tab2.isRadio2 = false,
          this.tab2.isRadio3 = true,
          this.tab2.isRadio4 = false,
          this.tab2.isRadio5 = false,
          this.tab2.isRadio6 = false,
          this.tab2.table.getdataListParm.name = 'getMaterialAnalysisRQ'
        this.tab2.table.getdataListParm.offset = 1
        this.getTab2List()
      } else if (this.tab2.radio == '4') {
        this.tab2.isRadio1 = false,
          this.tab2.isRadio2 = false,
          this.tab2.isRadio3 = false,
          this.tab2.isRadio4 = true,
          this.tab2.isRadio5 = false,
          this.tab2.isRadio6 = false,
          this.tab2.table.getdataListParm.name = 'getMaterialAnalysisTMR'
        this.tab2.table.getdataListParm.offset = 1
        this.getTab2List()
      } else if (this.tab2.radio == '5') {
        this.tab2.isRadio1 = false,
          this.tab2.isRadio2 = false,
          this.tab2.isRadio3 = false,
          this.tab2.isRadio4 = false,
          this.tab2.isRadio5 = true,
          this.tab2.isRadio6 = false,
          this.tab2.table.getdataListParm.name = 'getMaterialAnalysisBC'
        this.tab2.table.getdataListParm.offset = 1
        this.getTab2List()
      } else if (this.tab2.radio == '6') {
        this.tab2.isRadio1 = false,
          this.tab2.isRadio2 = false,
          this.tab2.isRadio3 = false,
          this.tab2.isRadio4 = false,
          this.tab2.isRadio5 = false,
          this.tab2.isRadio6 = true,
          this.tab2.table.getdataListParm.name = 'getMaterialAnalysisCC'
        this.tab2.table.getdataListParm.offset = 1
        this.getTab2List()
      }
    },
    // 价格分析
    getTab3List() {
      this.tab3.table.listLoading = true
      GetReportform(this.tab3.table.getdataListParm).then(response => {
        console.log('库存统计-价格分析', response.data.list)
        if (response.data.list !== null) {
          this.tab3.table.list = response.data.list.data1
          this.tab3.table.tableConfig = response.data.list.data2
          this.tab3.table.total = response.data.total
        } else {
          this.tab3.table.list = []
        }
        setTimeout(() => {
          this.tab3.table.listLoading = false
        }, 100)
      })
    },
    changeRadio3() {
      console.log(this.tab3.radio)
      if (this.tab3.radio == '1') {
        this.tab3.isRadio1 = true,
          this.tab3.isRadio2 = false,
          this.tab3.isRadio3 = false,
          this.tab3.isRadio4 = false,
          this.tab3.table.getdataListParm.name = 'getPriceAnalysisSC'
        this.tab3.table.getdataListParm.offset = 1
        this.getTab3List()
      } else if (this.tab3.radio == '2') {
        this.tab3.isRadio1 = false,
          this.tab3.isRadio2 = true,
          this.tab3.isRadio3 = false,
          this.tab3.isRadio4 = false,
          this.tab3.table.getdataListParm.name = 'getPriceAnalysisLS'
        this.tab3.table.getdataListParm.offset = 1
        this.getTab3List()
      } else if (this.tab3.radio == '3') {
        this.tab3.isRadio1 = false,
          this.tab3.isRadio2 = false,
          this.tab3.isRadio3 = true,
          this.tab3.isRadio4 = false,
          this.tab3.table.getdataListParm.name = 'getPriceAnalysisRQ'
        this.tab3.table.getdataListParm.offset = 1
        this.getTab3List()
      } else if (this.tab3.radio == '4') {
        this.tab3.isRadio1 = false,
          this.tab3.isRadio2 = false,
          this.tab3.isRadio3 = false,
          this.tab3.isRadio4 = true,
          this.tab3.table.getdataListParm.name = 'getPriceAnalysisTMR'
        this.tab3.table.getdataListParm.offset = 1
        this.getTab3List()
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.search {
  margin-bottom: 10px;
}
</style>
