<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="100px"
          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-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            />
          </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 dict.ST_INV_IN_TYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="厚度幅宽">
            <el-input
              v-model="query.with"
              size="mini"
              clearable
              placeholder="例如：8*868"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="厚度">
            <el-input
              v-model="query.thickness_request"
              size="mini"
              clearable
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="子卷号">
            <el-input
              v-model="query.pcsn"
              size="mini"
              clearable
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="生产区域">
            <el-select
              v-model="query.product_area"
              clearable
              multiple
              size="mini"
              class="filter-item"
            >
              <el-option
                v-for="item in dict.product_area"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否虚拟库">
            <el-select
              v-model="query.is_virtual"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.IS_OR_NOT"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="班次">
            <el-select
              v-model="query.classes"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in classes"
                :key="item.id"
                :label="item.code"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否汇总">
            <el-select
              v-model="query.is_all"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.IS_OR_NOT"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </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-thumb"
          size="mini"
          :loading="showDtlLoading"
          @click="downdtl"
        >
          导出
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        size="mini"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column show-overflow-tooltip prop="stor_name" label="仓库" :min-width="flexWidth('stor_name',crud.data,'仓库')" />
        <el-table-column show-overflow-tooltip prop="sect_name" label="库区" :min-width="flexWidth('sect_name',crud.data,'库区')" />
        <el-table-column show-overflow-tooltip prop="struct_code" label="仓位编码" :min-width="flexWidth('struct_code',crud.data,'仓位编码')" />
        <el-table-column show-overflow-tooltip prop="struct_name" label="仓位名称" :min-width="flexWidth('struct_name',crud.data,'仓位名称')" />
        <el-table-column show-overflow-tooltip prop="bill_type" :formatter="bill_typeFormat" label="业务类型" :min-width="flexWidth('bill_type',crud.data,'业务类型')" />
        <el-table-column show-overflow-tooltip prop="box_no" label="木箱号" :min-width="flexWidth('box_no',crud.data,'木箱号')" />
        <el-table-column show-overflow-tooltip prop="box_size" label="木箱尺寸" :min-width="flexWidth('box_size',crud.data,'木箱尺寸')" />
        <el-table-column show-overflow-tooltip prop="material_code" label="物料编码" :min-width="flexWidth('material_code',crud.data,'物料编码')" />
        <el-table-column show-overflow-tooltip prop="material_name" label="物料名称" :min-width="flexWidth('material_name',crud.data,'物料名称')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="pcsn" label="子卷号" :min-width="flexWidth('pcsn',crud.data,'子卷号')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="sap_pcsn" label="sap批次" :min-width="flexWidth('sap_pcsn',crud.data,'sap批次')" />
        <el-table-column show-overflow-tooltip prop="net_weight" label="净重" :formatter="crud.formatNum2" :min-width="flexWidth('net_weight',crud.data,'净重')" />
        <el-table-column show-overflow-tooltip prop="qty_unit_name" label="单位" :min-width="flexWidth('qty_unit_name',crud.data,'单位')" />
        <el-table-column show-overflow-tooltip prop="customer_name" label="客户编码" :min-width="flexWidth('customer_name',crud.data,'客户编码')" />
        <el-table-column show-overflow-tooltip prop="customer_description" label="客户名称" :min-width="flexWidth('customer_description',crud.data,'客户名称')" />
        <el-table-column show-overflow-tooltip prop="sale_order_name" label="销售订单" :min-width="flexWidth('sale_order_name',crud.data,'销售订单')" />
        <el-table-column show-overflow-tooltip prop="input_time" label="入库日期" :min-width="flexWidth('input_time',crud.data,'入库日期')" />
        <el-table-column show-overflow-tooltip prop="date_of_production" label="生产日期" :min-width="flexWidth('date_of_production',crud.data,'生产日期')" />
        <el-table-column show-overflow-tooltip prop="input_optname" label="入库人" :min-width="flexWidth('input_optname',crud.data,'入库人')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="width" label="产品规格" :min-width="flexWidth('width',crud.data,'产品规格')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="thickness" label="产品厚度" :min-width="flexWidth('thickness',crud.data,'产品厚度')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="paper_type" label="管件类型" :min-width="flexWidth('paper_type',crud.data,'管件类型')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="paper_code" label="管件编码" :min-width="flexWidth('paper_code',crud.data,'管件编码')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="paper_name" label="管件描述" :min-width="flexWidth('paper_name',crud.data,'管件描述')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="mass_per_unit_area" label="单位面积" :min-width="flexWidth('mass_per_unit_area',crud.data,'单位面积')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="demand_limit" label="客户需求抗拉下限" :min-width="flexWidth('demand_limit',crud.data,'客户需求抗拉下限')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="standard_limit" label="内控标准抗拉下线" :min-width="flexWidth('standard_limit',crud.data,'内控标准抗拉下线')" />
        <el-table-column v-if="crud.query.is_all === '0'" show-overflow-tooltip prop="actual_value" label="生产实际抗拉值" :min-width="flexWidth('actual_value',crud.data,'生产实际抗拉值')" />
        <el-table-column show-overflow-tooltip prop="remark" label="备注" :min-width="flexWidth('remark',crud.data,'备注')" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { crud, 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/index'
import crudUserStor from '@/views/wms/basedata/st/userStor/userStor'
import crudInbillquery from '@/views/wms/stat/inbillquery/inbillquery'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'

export default {
  name: 'InQuery',
  components: { crudOperation, rrOperation, udOperation, pagination, DateRangePicker },
  cruds() {
    return CRUD({
      title: '',
      optShow: { add: false, reset: true },
      idField: 'iostorinv_id',
      url: '/api/in/InQuery',
      query: { 'is_all': '0', 'bill_type': '0001', 'createTime': [new Date(), new Date()], 'stor_id': '1582991156504039424' },
      crudMethod: { ...crudInbillquery }
    })
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['io_bill_status', 'ST_INV_IN_TYPE', 'product_area', 'IS_OR_NOT'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      storlist: [],
      classes: [{ 'id': '1', 'code': '白班' }, { 'id': '2', 'code': '晚班' }],
      showDtlLoading: false
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudUserStor.getUserStor().then(res => {
      this.storlist = res
    })
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    selectTime() {
      var date = new Date()
      var y = date.getFullYear()
      var m = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      var d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()

      var start_day = y + '-' + m + '-' + '01' + ' ' + '00:00:00'
      var end_day = `${y}-${m}-${d}` + ' ' + '23:59:59'
      this.query.createTime = [new Date(start_day), new Date(end_day)]
    },
    bill_typeFormat(row, column) {
      return this.dict.label.ST_INV_IN_TYPE[row.bill_type]
    },
    downdtl() {
      if (this.currentRow !== null) {
        const data = this.crud.query
        if (this.crud.query.createTime !== undefined) {
          data.begin_time = this.crud.query.createTime[0]
          data.end_time = this.crud.query.createTime[1]
        }
        this.showDtlLoading = true
        download('/api/in/InQuery/download', data).then(result => {
          downloadFile(result, '成品入库查询', 'xlsx')
          this.showDtlLoading = false
        }).catch(() => {
          this.showDtlLoading = false
        })
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
