<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="所属库区">
            <el-cascader
              placeholder="所属库区"
              :options="sects"
              :props="{ checkStrictly: true }"
              clearable
              class="filter-item"
              @change="sectQueryChange"
            />
          </el-form-item>

          <el-form-item label="物料名称">
            <el-input
              v-model="query.material_name"
              clearable
              placeholder="物料编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="物料型号">
            <el-input
              v-model="query.material_model"
              clearable
              placeholder="物料型号"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="物料分类">
            <treeselect
              v-model="query.material_type_id"
              :load-options="loadClass"
              :options="classes"
              style="width: 180px;"
              placeholder="请选择"
            />
          </el-form-item>

          <el-form-item label="托盘">
            <label slot="label">托&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;盘:</label>
            <el-input
              v-model="query.storagevehicle_code"
              clearable
              placeholder="托盘号"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="货位名称">
            <el-input
              v-model="query.struct_name"
              clearable
              placeholder="货位编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="品质类型">
            <el-select
              v-model="query.quality_scode"
              clearable
              size="mini"
              placeholder="品质类型"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.ST_QUALITY_SCODE"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="库存等级">
            <el-select
              v-model="query.ivt_level"
              clearable
              size="mini"
              placeholder="库存等级"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.ST_IVT_LEVEL"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否可用">
            <el-select
              v-model="query.is_active"
              clearable
              size="mini"
              placeholder="是否可用"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.is_usable"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="批次">
            <label slot="label">批&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;次:</label>
            <el-input
              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-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-check"
          :disabled="downdtl_flag"
          size="mini"
          @click="downdtl"
        >
          导出Excel
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="stor_name" label="仓库" />
        <el-table-column prop="sect_name" label="库区" />
        <el-table-column prop="struct_code" label="货位编码" width="120" />
        <el-table-column prop="struct_name" label="货位名称" min-width="150" show-overflow-tooltip />
        <el-table-column prop="storagevehicle_code" label="托盘号">
          <template slot-scope="scope">
            <el-link type="warning" @click="openBucket(scope.row)">{{ scope.row.storagevehicle_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="material_code" label="物料编码" min-width="150" show-overflow-tooltip />
        <el-table-column prop="material_name" label="物料名称" min-width="120" show-overflow-tooltip />
        <el-table-column prop="material_spec" label="物料规格" show-overflow-tooltip/>
        <el-table-column prop="material_model" label="型号" />
        <el-table-column prop="pcsn" label="批次" show-overflow-tooltip width="150px"/>
        <el-table-column prop="quality_scode" label="品质类型" :formatter="format_quality_scode" />
        <el-table-column prop="ivt_level" label="库存等级" :formatter="format_ivt_level" />
        <el-table-column prop="is_active" label="是否可用" :formatter="format_is_active" />
        <el-table-column prop="ivt_qty" label="库存重量" :formatter="formatQty" />
        <el-table-column prop="canuse_qty" label="可用重量" :formatter="formatQty" />
        <el-table-column prop="frozen_qty" label="冻结数" :formatter="formatQty" />
        <el-table-column prop="warehousing_qty" label="待入数" :formatter="formatQty" />
        <el-table-column prop="qty_unit_name" label="单位" />
        <el-table-column prop="instorage_time" label="入库时间" width="135" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <BucketDialog :dialog-show.sync="bucketDialog" :open-param="openParam"/>
  </div>
</template>

<script>
import crudPoint from '@/api/wms/sch/point'
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import crudSectattr from '@/api/wms/basedata/st/sectattr'
import crudOperation from '@crud/CRUD.operation'
import BucketDialog from '@/views/wms/statistics/ivtQuery/BucketDialog'
import crudClassstandard from '@/api/wms/basedata/master/classstandard'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import crudMaterialbase from '@/api/wms/basedata/master/materialbase'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'

export default {
  name: 'IvtQuery',
  dicts: ['ST_QUALITY_SCODE', 'ST_IVT_LEVEL', 'is_usable'],
  components: { pagination, rrOperation, crudOperation, BucketDialog, Treeselect },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '点位',
      url: 'api/ivtQuery',
      idField: 'point_id',
      sort: 'point_id,desc',
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      },
      crudMethod: { ...crudPoint }
    })
  },

  data() {
    return {
      sects: [],
      classes: [],
      permission: {
      },
      class_idStr: null,
      openParam: null,
      bucketDialog: false
    }
  },
  created() {
    crudSectattr.getSect().then(res => {
      this.sects = res.content
    })
    crudSectattr.getSect({ 'is_materialstore': '1' }).then(res => {
      this.sects = res.content
    })
    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()
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    sectQueryChange(val) {
      if (val.length === 1) {
        this.query.stor_id = val[0]
        this.query.sect_id = ''
      }
      if (val.length === 0) {
        this.query.sect_id = ''
        this.query.stor_id = ''
      }
      if (val.length === 2) {
        this.query.stor_id = val[0]
        this.query.sect_id = val[1]
      }
      this.crud.toQuery()
    },
    hand(value) {
      this.crud.toQuery()
    },
    formatQty(row, column) {
      return parseFloat(row[column.property]).toFixed(3)
    },
    format_quality_scode(row, column) {
      return this.dict.label.ST_QUALITY_SCODE[row.quality_scode]
    },
    format_ivt_level(row, column) {
      return this.dict.label.ST_IVT_LEVEL[row.ivt_level]
    },
    format_is_active(row, column) {
      return this.dict.label.is_usable[row.is_active]
    },
    openBucket(row) {
      this.openParam = row.storagevehicle_code
      this.bucketDialog = true
    },
    loadClass({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudClassstandard.getClass({ pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    downdtl() {
      if (this.currentRow !== null) {
        crud.downloadLoading = true
        download('/api/ivtQuery/download', this.crud.query).then(result => {
          downloadFile(result, '原材料库存', 'xlsx')
          crud.downloadLoading = false
        }).catch(() => {
          crud.downloadLoading = false
        })
      }
    },
    queryClassId() {
      const param = {
        'class_idStr': this.class_idStr
      }
      crudClassstandard.queryClassById(param).then(res => {
        this.classes = res.content.map(obj => {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
