<template>
  <el-dialog
    title="统计表"
    width="800px"
    :visible.sync="visible"
    :before-close="_close"
  >
    <div>
      <el-date-picker
        style="width:150px;margin-right:10px"
        v-model="startTime"
        type="date"
        placeholder="选择日期"
        @change="selectStartime">
      </el-date-picker>
      <el-date-picker
        style="width:150px;margin-right:10px"
        v-model="endTime"
        type="date"
        placeholder="选择日期"
        @change="selectEndtime">
      </el-date-picker>
      <el-button icon="el-icon-printer" type="primary" @click="toPrint">打印</el-button>

    </div>
    <div style="margin-top:10px;">
      <el-table :data="tableData" border style="width: 100%" height="300px">
        <el-table-column align="center" prop="date" label="商品类型" width="180">
          <template slot-scope="scope">
            {{scope.row.good_type_name}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="规格名称" width="180">
          <template slot-scope="scope">
            {{scope.row.good_name}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="address" label="数量">
          <template slot-scope="scope">
            {{scope.row.count}}
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>


<script>
  const moment = require('moment')
  import { GetDialysisGoodStatistics } from '@/api/dialysis'
  import { parseTime } from '@/utils'

  export default {
    props: {
      list: Array
    },
    data() {
      return {
        query:{
          start_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
          end_time:moment(new Date()).add('year', 0).format('YYYY-MM-DD')
        },
        visible: false,
        startTime: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
        endTime: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
        schedulArr: [
          { value: 0, label: '全部班' },
          { value: 1, label: '上午' },
          { value: 2, label: '中午' },
          { value: 3, label: '下午' }
        ],

        schedulType: 0,
        partitionArr: [],
        partitionType: 0,
        tableData: [],
        start_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD'),
        end_time: moment(new Date()).add('year', 0).format('YYYY-MM-DD')
      }
    },
    created() {
      //统计今日出库的耗材

    },
    methods: {
      calCount(query_warehouseout_info) {
        let total = 0
        var array = []
        array = query_warehouseout_info
        for (let i = 0; i < array.length; i++) {
          total = total + array[i].count
        }
        return total

      },
      GetDialysisGoodStatistics(){
        GetDialysisGoodStatistics(this.query).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.tableData = []
            for (let i = 0; i < response.data.data.stock_out.length; i++) {
              if(this.calCount(response.data.data.stock_out[i].query_warehouseout_info) > 0) {
                let obj = {
                  good_name: response.data.data.stock_out[i].specification_name,
                  good_type_name: response.data.data.stock_out[i].type.type_name,
                  count: this.calCount(response.data.data.stock_out[i].query_warehouseout_info)
                }
                this.tableData.push(obj)
              }
            }
          }
        })
      },

      _close: function(done) {
        done()
      },
      show() {
        this.GetDialysisGoodStatistics()

        this.visible = true
      },

      selectStartime() {
        this.start_time = parseTime(this.startTime, '{y}-{m}-{d}')
        this.query.start_time = parseTime(this.startTime, '{y}-{m}-{d}')
        this.GetDialysisGoodStatistics()

      },
      selectEndtime() {
        this.end_time = parseTime(this.endTime, '{y}-{m}-{d}')
        this.query.end_time = parseTime(this.endTime, '{y}-{m}-{d}')
        this.GetDialysisGoodStatistics()

      },
      toPrint() {
        this.$router.push('/dialysis/consumables_print?startime=' + this.start_time + '&endtime=' + this.end_time)
      }

    }
  }
</script>
