<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-cascader
              placeholder="所属库区"
              :options="sects"
              :props="{ checkStrictly: true }"
              clearable
              class="filter-item"
              style="width: 200px;"
              @change="sectQueryChange"
            />
          </el-form-item>
          <el-form-item label="仓位搜索">
            <el-input
              v-model="query.struct"
              clearable
              size="small"
              placeholder="仓位编码或名称"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="物料搜索">
            <el-input
              v-model="query.material"
              clearable
              size="small"
              placeholder="物料编码、名称或规格"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="木箱码">
            <el-input
              v-model="query.package_box_sn"
              clearable
              size="small"
              placeholder="木箱码"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="子卷号">
            <el-input
              v-model="query.pcsn"
              clearable
              size="small"
              placeholder="子卷号"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="sap批次">
            <el-input
              v-model="query.sap_pcsn"
              clearable
              size="small"
              placeholder="sap批次"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="销售订单">
            <el-input
              v-model="query.sale_order_name"
              clearable
              size="small"
              placeholder="销售订单"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="库存状态">
            <el-select
              v-model="query.ivt_status"
              clearable
              size="mini"
              placeholder="全部"
              style="width: 200px;"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in ivtStatusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="生产区域">
            <el-select
              v-model="query.product_area"
              clearable
              multiple
              size="mini"
              class="filter-item"
              style="width: 200px;"
              @change="hand"
            >
              <el-option
                v-for="item in dict.product_area"
                :key="item.value"
                :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"
              style="width: 200px;"
              @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.sub_type"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 200px;"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.SUB_TYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="库存类型">
            <el-select
              v-model="query.ivt_flag"
              clearable
              multiple
              size="mini"
              style="width: 200px;"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in ivtTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否退货">
            <el-select
              v-model="query.quality_scode"
              clearable
              size="mini"
              style="width: 200px;"
              placeholder="请选择"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in ivtQualityList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <rrOperation :crud="crud" />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <!--<el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-check"
          size="mini"
          :loading="showDtlLoading"
          @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="struct_code"
          label="仓位编码"
          :min-width="flexWidth('struct_code',crud.data,'仓位编码')"
        />
        <el-table-column
          prop="struct_name"
          label="仓位名称"
          :min-width="flexWidth('struct_name',crud.data,'仓位名称')"
        />
        <el-table-column prop="stor_name" label="仓库" :min-width="flexWidth('stor_name',crud.data,'仓库')" />
        <el-table-column prop="sect_name" label="库区" :min-width="flexWidth('sect_name',crud.data,'库区')" />
        <el-table-column
          prop="material_code"
          label="物料编码"
          :min-width="flexWidth('material_code',crud.data,'物料编码')"
        />
        <el-table-column
          prop="material_name"
          label="物料名称"
          :min-width="flexWidth('material_name',crud.data,'物料名称')"
        />
        <el-table-column
          prop="package_box_sn"
          label="木箱码"
          :min-width="flexWidth('package_box_sn',crud.data,'木箱码')"
        />
        <el-table-column
          prop="quanlity_in_box"
          label="子卷数"
          :min-width="flexWidth('quanlity_in_box',crud.data,'子卷数')"
        />
        <el-table-column prop="pcsn" label="子卷号" :min-width="flexWidth('pcsn',crud.data,'子卷号')" />
        <el-table-column prop="sap_pcsn" label="sap批次" :min-width="flexWidth('sap_pcsn',crud.data,'sap批次')" />
        <el-table-column prop="sale_order_name" label="销售订单" :min-width="flexWidth('sale_order_name',crud.data,'销售订单')" />
        <el-table-column prop="box_weight" label="毛重" :formatter="rounding2" />
        <el-table-column prop="canuse_qty" label="可用数" :formatter="rounding" />
        <el-table-column prop="frozen_qty" label="冻结数" :formatter="rounding" />
        <el-table-column prop="ivt_qty" label="库存数" :formatter="rounding" />
        <el-table-column prop="warehousing_qty" label="待入数" :formatter="rounding" />
        <el-table-column prop="unit_name" label="计量单位" />
        <el-table-column prop="confirm_time" label="入库时间" min-width="150" />
        <el-table-column prop="stock_age" label="库龄" min-width="150" />
        <el-table-column prop="joint_type" label="接头数" min-width="150" />
        <el-table-column prop="sub_type" label="子卷状态" min-width="150" :formatter="formatSubType" />
        <el-table-column prop="produce_age" label="生产时长（天）" min-width="120" />
        <el-table-column prop="paper_type" label="品质类型" min-width="150" />
        <el-table-column prop="paper_code" label="管件编码" min-width="150" />
        <el-table-column prop="paper_name" label="管件描述" min-width="250" />
       <!-- <el-table-column prop="remark" label="超期原因" min-width="250">
          <template scope="scope">
            <el-select
              v-model="scope.row.remark"
              clearable
              size="mini"
              placeholder="全部"
              style="width: 200px;"
              class="filter-item"
            >
              <el-option
                v-for="item in remarkList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="120" fixed="right">
          <template scope="scope">
            <el-button
              type="primary"
              class="filter-item"
              size="mini"
              icon="el-icon-edit"
              @click.native.prevent="save(scope.row)"
            />
          </template>
        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <UploadDialog :dialog-show.sync="viewShow" @tableChanged="querytable" />
  </div>
</template>

<script>
import crudStructivt from '@/views/wms/basedata/st/ivt/structivt'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudUserStor from '@/views/wms/basedata/st/userStor/userStor'
import UploadDialog from '@/views/wms/basedata/st/ivt/UploadDialog'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'

export default {
  name: 'ControlIvt',
  dicts: ['ST_QUALITY_SCODE', 'product_area', 'IS_OR_NOT', 'SUB_TYPE'],
  components: { pagination, crudOperation, rrOperation, UploadDialog },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '库存管理', url: 'api/structivt/control', idField: 'stockrecord_id', sort: 'stockrecord_id,desc',
      optShow: {
        add: false,
        edit: false,
        showDtlLoading: false,
        del: false,
        download: false,
        reset: true
      },
      crudMethod: { ...crudStructivt }
    })
  },
  data() {
    return {
      sects: [],
      viewShow: false,
      ivtStatusList: [
        { 'value': 'canuse_qty', 'label': '可用数' },
        { 'value': 'warehousing_qty', 'label': '待入数' },
        { 'value': 'frozen_qty', 'label': '冻结数' }
      ],
      ivtTypeList: [
        { 'value': ' ', 'label': '生产' },
        { 'value': '2', 'label': '改切' },
        { 'value': '1', 'label': '返检' }
      ],
      ivtQualityList: [
        { 'value': '01', 'label': '否' },
        { 'value': '02', 'label': '是' }
      ],
      remarkList: [
        { 'value': '1', 'label': '客户原因' },
        { 'value': '2', 'label': '生产质量原因' },
        { 'value': '3', 'label': '市场原因' }
      ],
      permission: {},
      rules: {}
    }
  },
  created() {
    crudUserStor.getSect({ 'stor_id': '' }).then(res => {
      this.sects = res.content
    })
    this.crud.query.ivt_status = 'canuse_qty'
    this.crud.toQuery()
  },
  methods: {
    // 取两位小数点
    rounding(row, column) {
      return parseFloat(row[column.property]).toFixed(2)
    },
    rounding2(row, column) {
      if (row.box_weight !== '') {
        return parseFloat(row[column.property]).toFixed(2)
      }
    },
    formatSubType(row) {
      return this.dict.label.SUB_TYPE[row.sub_type]
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    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()
    },
    querytable() {
      this.crud.toQuery()
    },
    downdtl() {
      if (this.currentRow !== null) {
        this.showDtlLoading = true
        download('/api/structivt/download', this.crud.query).then(result => {
          downloadFile(result, '成品库存', 'xlsx')
          this.showDtlLoading = false
        }).catch(() => {
          this.showDtlLoading = false
        })
      }
    },
    Import() {
      this.viewShow = true
    },
    save(row) {
      crudStructivt.save(row).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    }
  }
}
</script>

<style scoped>

</style>
