<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-form
          size="mini"
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >

          <el-form-item label="仓库">
            <label slot="label">仓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;库:</label>
            <el-select
              v-model="query.stor_id"
              clearable
              style="width: 220px;"
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in storList"
                :label="item.stor_name"
                :value="item.stor_id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="日期">
            <label slot="label">日&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;期:</label>
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="crud.toQuery"
            />

          </el-form-item>

          <el-form-item label="物料编码">
            <el-input
              style="width: 220px"
              v-model="query.material_code"
              clearable
              placeholder="物料编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="批次">
            <label slot="label">批&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;次:</label>
            <el-input
              style="width: 220px"
              v-model="query.pcsn"
              clearable
              placeholder="请输入批次"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <rrOperation/>
        </el-form>

      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission"/>
      <!--表格渲染-->
      <el-table ref="table" :cell-style="cellStyleMst" show-summary :summary-method="getSum" :max-height="590" v-loading="crud.loading" :data="crud.data" size="mini" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column prop="stor_name" label="仓库"/>
        <el-table-column prop="sect_date" label="日期" width="100px" />
<!--        <el-table-column prop="class_code" label="物料分类编码" width="100px" />-->
<!--        <el-table-column prop="class_name" label="物料分类名称" width="100px" />-->
        <el-table-column prop="material_code" label="物料编码" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="material_name" label="物料名称" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="material_model" label="规格/型号" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="unit_name" label="主单位"/>
        <el-table-column prop="pcsn" label="批次号" min-width="120" show-overflow-tooltip/>
<!--        <el-table-column prop="dept_name" label="所属组织" min-width="120" show-overflow-tooltip/>-->
<!--        <el-table-column prop="quality_scode" label="品质类型" :formatter="formatQualityName" />-->
<!--        <el-table-column prop="ivt_level" label="质量等级" :formatter="formatIvtName" />-->
<!--        <el-table-column prop="is_active" label="是否可用" :formatter="formatIsName" />-->
        <el-table-column prop="start_num" label="期初主数量" :formatter="crud.formatNum3" width="100px" />
        <el-table-column prop="in_num" label="入库主数量" :formatter="crud.formatNum3" width="100px" />
        <el-table-column prop="out_num" label="出库主数量" :formatter="crud.formatNum3" width="100px" />
<!--        <el-table-column prop="less_num" label="其他出" :formatter="crud.formatNum3"/>-->
<!--        <el-table-column prop="more_num" label="其他入" :formatter="crud.formatNum3"/>-->
        <el-table-column prop="end_num" label="结存主数量" :formatter="crud.formatNum3" width="100px" />
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudIostordaily from '@/views/wms/statistics/sendReceiveQuery/iostordaily'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import crudMaterialbase from '@/views/wms/basedata/master/material/materialbase'
import crudUserStor from '@/views/wms/basedata/st/userStor/userStor'

const defaultForm = {
  stordaily_id: null,
  stor_id: null,
  stor_code: null,
  stor_name: null,
  sect_date: null,
  material_id: null,
  quality_scode: null,
  pcsn: null,
  ivt_level: null,
  is_active: null,
  start_num: null,
  in_num: null,
  out_num: null,
  more_num: null,
  less_num: null,
  end_num: null,
  qty_unit_id: null,
  qty_unit_name: null
}
export default {
  name: 'SendReceiveQuery',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '收发存查询',
      url: 'api/iostordaily',
      idField: 'stordaily_id',
      sort: '',
      crudMethod: { ...crudIostordaily },
      props: {
        // 每页数据条数
        size: 20
      },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      permission: {},
      classes: [],
      QualityList: [],
      IvtList: [],
      class_idStr: null,
      storList: []
    }
  },
  created() {
    crudUserStor.getUserStor().then(res => {
      this.storList = res
    })
    const param = {
      'materOpt_code': '00'
    }
 /*   crudMaterialbase.getMaterOptType(param).then(res => {
      this.class_idStr = res.class_idStr
      this.crud.query.class_idStr = res.class_idStr
      this.queryClassId()
    })*/
  /*  crudBucketrecord.getQualityList().then(res => {
      this.QualityList = res
    })
    crudBucketrecord.getIvtList().then(res => {
      this.IvtList = res
    })*/
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    formatIvtName(row, column) {
      for (const item of this.IvtList) {
        if (item.code === row.ivt_level) {
          return item.name
        }
      }
    },
    formatQualityName(row, column) {
      for (const item of this.QualityList) {
        if (item.code === row.quality_scode) {
          return item.name
        }
      }
    },
    formatIsName(row, column) {
      if (row.is_active === '1') {
        return '是'
      } else if (row.is_active === '0') {
        return '否'
      }
    },
    getSum(param) {
      // 将所有number的字段合计
      const {columns, data} = param
      const sums = []
      let values = []
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = '合计'
          return
        }
        values = data.map(item => Number(item[column.property]))
        if (column.property === 'start_num') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + 'kg' // 保留三位小数
        } else if (column.property === 'in_num') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + 'kg' // 保留三位小数
        } else if (column.property === 'out_num') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + 'kg' // 保留三位小数
        } else if (column.property === 'end_num') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + 'kg' // 保留三位小数
        } else if (column.property === 'more_num') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + 'kg' // 保留三位小数
        } else if (column.property === 'less_num') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + 'kg' // 保留三位小数
        } else {
          sums[index] = '--'
        }
      })
      return sums
    },
    cellStyleMst({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'dept_name') {
        if (row.is_yellow !== undefined) {
          return 'background: yellow'
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
