<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="库区" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择库区" clearable filterable>
          <el-option v-for="item in areaOptions" :key="item.id" :label="areaLabel(item)" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品信息" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="名称/品牌/类别/规格/单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"><el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['warehouse:stock:export']">导出</el-button></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="stockList">
      <el-table-column label="库区" align="center" prop="areaName" min-width="110" />
      <el-table-column label="产品信息" min-width="300">
        <template slot-scope="scope">
          <div class="product-name">{{ scope.row.productName || '-' }}</div>
          <div class="product-meta">
            <span v-if="scope.row.brand">品牌：{{ scope.row.brand }}</span>
            <span v-if="scope.row.category">类别：{{ scope.row.category }}</span>
            <span v-if="scope.row.spec">规格：{{ scope.row.spec }}</span>
            <span v-if="scope.row.unit">单位：{{ scope.row.unit }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="当前库存" align="right" prop="stockNum" min-width="120" />
      <el-table-column label="最近盘库时间" align="center" prop="lastCheckTime" width="180">
        <template slot-scope="scope">{{ parseTime(scope.row.lastCheckTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-sold-out" @click="handleOut(scope.row)" v-hasPermi="['warehouse:stock:out']">出库</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog title="库存出库" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="库区"><el-input :value="form.areaName" disabled /></el-form-item>
        <el-form-item label="产品"><el-input :value="productLabel(form)" disabled /></el-form-item>
        <el-form-item label="当前库存"><el-input :value="form.stockNum" disabled /></el-form-item>
        <el-form-item label="出库单号" prop="outNo"><el-input v-model="form.outNo" placeholder="不填则自动生成" /></el-form-item>
        <el-form-item label="出库人" prop="outOperator">
          <el-select v-model="form.outOperator" placeholder="请选择出库人" filterable>
            <el-option v-for="item in userOptions" :key="item.userId" :label="userLabel(item)" :value="userValue(item)" />
          </el-select>
        </el-form-item>
        <el-form-item label="出库类型" prop="outType">
          <el-select v-model="form.outType" placeholder="请选择出库类型">
            <el-option v-for="dict in dict.type.wms_out_type" :key="dict.value" :label="dict.label" :value="Number(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="出库数量" prop="outNum"><el-input-number v-model="form.outNum" :min="0.001" :precision="3" :step="1" controls-position="right" /></el-form-item>
        <el-form-item label="关联单据号" prop="sourceNo"><el-input v-model="form.sourceNo" placeholder="请输入关联单据号" /></el-form-item>
        <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" type="textarea" placeholder="请输入备注" /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOut">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStock, outStock } from '@/api/warehouse/stock'
import { optionselectArea } from '@/api/warehouse/area'
import { optionselectUser } from '@/api/warehouse/user'

export default {
  name: 'StockInfo',
  dicts: ['wms_out_type'],
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      stockList: [],
      areaOptions: [],
      userOptions: [],
      open: false,
      queryParams: { pageNum: 1, pageSize: 10, areaId: undefined, productName: undefined },
      form: {},
      rules: {
        outOperator: [{ required: true, message: '出库人不能为空', trigger: 'change' }],
        outType: [{ required: true, message: '出库类型不能为空', trigger: 'change' }],
        outNum: [{ required: true, message: '出库数量不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getOptions()
    this.getList()
  },
  methods: {
    userLabel(user) {
      return user.nickName || user.userName
    },
    userValue(user) {
      return user.nickName || user.userName
    },
    areaLabel(area) {
      return area.areaName || area.areaCode
    },
    productLabel(product) {
      const name = product.productName || product.productCode || '-'
      const details = [product.brand, product.category, product.spec, product.unit].filter(item => item)
      return details.length ? name + '（' + details.join(' / ') + '）' : name
    },
    getOptions() {
      optionselectArea().then(res => { this.areaOptions = res.data })
      optionselectUser().then(res => { this.userOptions = res.data })
    },
    getList() {
      this.loading = true
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    resetFormData(row) {
      this.form = {
        id: row.id,
        areaName: row.areaName,
        productName: row.productName,
        brand: row.brand,
        category: row.category,
        spec: row.spec,
        unit: row.unit,
        stockNum: row.stockNum,
        outNo: undefined,
        outOperator: undefined,
        outType: 1,
        outNum: undefined,
        sourceNo: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    cancel() { this.open = false },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleOut(row) { this.resetFormData(row); this.open = true },
    submitOut() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        outStock(this.form).then(() => {
          this.$modal.msgSuccess('出库成功')
          this.open = false
          this.getList()
        })
      })
    },
    handleExport() {
      this.download('warehouse/stock/export', { ...this.queryParams }, `stock_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
.product-name {
  line-height: 20px;
  font-weight: 600;
}
.product-meta {
  color: #909399;
  line-height: 18px;
}
.product-meta span {
  margin-right: 12px;
  white-space: nowrap;
}
</style>
