<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="90px"
          label-suffix=":"
        >
          <el-form-item label="所属仓库">
            <el-select
              v-model="query.stor_id"
              clearable
              size="mini"
              placeholder="全部"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in storlist"
                :key="item.stor_id"
                :label="item.stor_name"
                :value="item.stor_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单据类型">
            <el-select
              v-model="query.bill_type"
              clearable
              filterable
              size="mini"
              placeholder="业务类型"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in billtypelist"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单据日期">
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              @input="onInput()"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              @change="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="物料">
            <el-input
              v-model="query.material_code"
              size="mini"
              clearable
              placeholder="物料编码或名称模糊查询"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="批次">
            <el-input
              v-model="query.pcsn"
              size="mini"
              clearable
              placeholder="批次模糊查询"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="工令">
            <el-input v-model="query.source_bill_code"  size="mini" placeholder="工令号" disabled class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="queryMater" />
            </el-input>
          </el-form-item>
          <el-form-item label="人员">
            <el-input
              v-model="query.username"
              size="mini"
              clearable
              placeholder="人员"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        size="mini"
        :data="crud.data"
        style="width: 100%;"
        :highlight-current-row="true"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="index" label="序号" width="50" align="center" />
        <el-table-column prop="material_code" label="物料编码" width="150" align="center" />
        <el-table-column prop="material_name" label="物料名称" width="150" align="center" />
        <el-table-column min-width="90" prop="pcsn" label="批次号" align="center" />
        <el-table-column prop="sum_plan_qty" label="计划重量(KG)" :formatter="crud.formatNum3" width="150"  align="center" />
        <el-table-column prop="sum_real_qty" label="实际重量(KG)" :formatter="crud.formatNum3" width="150"  align="center" />
        <el-table-column prop="quality_scode" label="品质类型" align="center" width="110px" :formatter="quality_scodeFormat" />
        <el-table-column prop="ivt_level" label="库存等级" align="center" width="100px" :formatter="ivt_levelFormat" />
        <el-table-column prop="is_active" label="是否可用" align="center" width="110px" :formatter="is_activeFormat" />
        <el-table-column min-width="120" prop="qty_unit_name" label="单位" align="center" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <StructIvt :dialog-show.sync="structshow" @StructIvtClosed="queryTableDtl" />
  </div>
</template>

<script>
import checkoutbill from '@/api/wms/st/core/outbill/checkoutbill'
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudStorattr from '@/api/wms/basedata/st/storattr'
import Date from '@/utils/datetime'
import StructIvt from '@/views/wms/statistics/outStorQuery/StructIvt'
import crudRawAssist from '@/api/wms/st/core/inbill/rawassist'

export default {
  name: 'outStorQuery',
  components: { StructIvt, crudOperation, rrOperation, pagination },
  cruds() {
    return CRUD({ title: '生产配粉领料', idField: 'iostorinv_id', url: 'api/checkoutbill/queryDtl', crudMethod: { ...checkoutbill },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: false
      }})
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['ST_QUALITY_SCODE', 'ST_IVT_LEVEL', 'is_used', 'is_again_put', 'is_usable'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      permission: {
      },
      structshow: false,
      currentRow: null,
      storlist: [],
      billtypelist: []
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudStorattr.getStor({ 'is_materialstore': '1' }).then(res => {
      this.storlist = res.content
    })
    crudRawAssist.getType({ 'io_code': '0103', 'io_flag': '01' }).then(res => {
      this.billtypelist = res
    })
    this.crud.query.createTime = [new Date(), new Date()]
    this.crud.toQuery()
  },
  methods: {
    onInput() {
      this.$forceUpdate()
    },
    async queryMater() {
      this.structshow = true
    },
    quality_scodeFormat(row, column) {
      return this.dict.label.ST_QUALITY_SCODE[row.quality_scode]
    },
    ivt_levelFormat(row, column) {
      return this.dict.label.ST_IVT_LEVEL[row.ivt_level]
    },
    is_activeFormat(row, column) {
      return this.dict.label.is_usable[row.is_active]
    },
    queryTableDtl(rows) {
      let devices = ''
      for (let i = 0; i < rows.length; i++) {
        if ( i === 0 ) {
          devices = rows[i].workorder_code
        }else{
          devices = devices + ',' + rows[i].workorder_code
        }
      }
      if(rows.length > 0){
        this.crud.query.source_bill_code = devices
        this.crud.toQuery()
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
