<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="物料编码" prop="materialNo">
            <el-input v-model="queryParams.materialNo" placeholder="请输入物料编码" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料名称" prop="materialName">
            <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="存货类别" prop="categoryNumber">
            <CategorySelect v-model="queryParams.categoryNumber" filterable placeholder="请选择存货类别" />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:material-total:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="采购物料" name="materialInventory" lazy>
        <MaterialInventory ref="materialInventory" :active-name="activeName" :query-paramspro="queryParams" />
      </el-tab-pane>
      <el-tab-pane label="生产成品" name="finishProductInventory" lazy>
        <!-- 已发 -->
        <FinishProductInventory ref="finishProductInventory" :active-name="activeName" :query-paramspro="queryParams" />
      </el-tab-pane>
    </el-tabs>
    <!-- 分页组件 -->

    <!-- 对话框(添加 / 修改) -->
  </div>
</template>

<script>
import FinishProductInventory from './components/FinishProductInventory.vue'
import MaterialInventory from './components/MaterialInventory.vue'
import CategorySelect from './components/CategorySelect.vue'
import { exporMaterialExcel } from '@/api/wms/inquirePurchase/inventoryInquire'

export default {
  name: 'InventoryInquire',
  components: {
    MaterialInventory,
    FinishProductInventory,
    CategorySelect
  },
  provide() {
    return {
      paramsQuery: this.queryParams
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 显示哪一个表格
      activeName: 'materialInventory',
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialNo: null,
        materialName: null,
        categoryNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wmsIncomingReceiptId: [{ required: true, message: '来料登记记录ID不能为空', trigger: 'blur' }],
        orderType: [{ required: true, message: '单据类型不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsIncomingReceiptId: undefined,
        orderType: undefined,
        erpStockId: undefined,
        wmsStoreAreaId: undefined,
        wmsStoreId: undefined,
        inQty: undefined,
        funitId: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$refs[this.activeName].getList()
      if (this.$refs[this.activeName].list.length === 0) {
        if (this.activeName === 'finishProductInventory') {
          this.activeName = 'materialInventory'
        } else {
          this.activeName = 'finishProductInventory'
        }
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.$refs[this.activeName].handleQuery()
      this.$refs.materialInventory.handleQuery()
      this.$refs.finishProductInventory.handleQuery()
      this.activeName = 'materialInventory'
    },
    handleClick(tab, event) {
      this.activeName = tab.name
    },
    // 导出操作
    handleExport() {
      this.$modal.confirm('是否确认导出所有WMS库存数据项?').then(() => {
        this.exportLoading = true
        return exporMaterialExcel()
      }).then(response => {
        this.$download.excel(response, 'WMS库存物料.xls')
        this.exportLoading = false
      }).catch(() => {})
    }
  }
}
</script>

