<template>
  <div class="app-container">
    <!-- 检索区 -->
    <el-card shadow="never" class="search-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="入库搜索" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="入库单号/商品名称"
            clearable
            style="width: 180px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="入库时间">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="入库类型" prop="inboundType">
          <el-select v-model="queryParams.inboundType" placeholder="全部" clearable style="width: 150px">
            <el-option v-for="item in inboundTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="operatorId">
          <el-select v-model="queryParams.operatorId" placeholder="全部" clearable filterable style="width: 140px">
            <el-option
              v-for="item in operatorOptions"
              :key="item.operatorId"
              :label="item.operatorName"
              :value="item.operatorId"
            />
          </el-select>
        </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-card>

    <br/>

    <!-- 列表区 -->
    <el-card shadow="never" class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleCreate" v-hasPermi="['agri:seller:stock:inbound:add']">商品入库</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="inboundList" :empty-text="emptyTableText">
        <el-table-column label="入库单号" align="center" prop="inboundNo" min-width="180" :show-overflow-tooltip="true" />
        <el-table-column label="入库类型" align="center" width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.inboundTypeText || inboundTypeLabel(scope.row.inboundType) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入库时间" align="center" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.inboundTime) || '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" min-width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ scope.row.remark || '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="经办人" align="center" prop="operatorName" width="100" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ scope.row.operatorName || '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="90" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['agri:seller:stock:inbound:query']">详情</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-card>

    <!-- 详情抽屉 -->
    <seller-stock-inbound-detail
      ref="inboundDetail"
      :visible.sync="detailOpen"
      :inbound-id="currentInboundId"
    />

    <!-- 手工入库抽屉 -->
    <seller-stock-inbound-form
      :visible.sync="formOpen"
      @success="handleFormSuccess"
    />
  </div>
</template>

<script>
import {
  listSellerStockInbound,
  listStockInboundOperators
} from "@/api/agri/seller/stock/inbound"
import { getSellerContext } from "@/api/agri/seller/context"
import { setSellerShopContext } from "@/utils/sellerShop"
import SellerStockInboundDetail from "./detail"
import SellerStockInboundForm from "./form"

const INBOUND_TYPE_MAP = {
  "0": "新增商品规格",
  "1": "采购入库",
  "2": "退货入库",
  "3": "其他入库"
}

export default {
  name: "AgriSellerStockInbound",
  components: { SellerStockInboundDetail, SellerStockInboundForm },
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      inboundList: [],
      dateRange: [],
      operatorOptions: [],
      detailOpen: false,
      formOpen: false,
      currentInboundId: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        inboundType: undefined,
        operatorId: undefined
      },
      inboundTypeOptions: [
        { value: "0", label: "新增商品规格" },
        { value: "1", label: "采购入库" },
        { value: "2", label: "退货入库" },
        { value: "3", label: "其他入库" }
      ]
    }
  },
  computed: {
    hasSearchFilter() {
      return !!(this.queryParams.keyword || this.queryParams.inboundType || this.queryParams.operatorId || (this.dateRange && this.dateRange.length))
    },
    emptyTableText() {
      return this.hasSearchFilter ? "未找到符合条件的入库单" : "暂无入库记录"
    }
  },
  created() {
    this.initPage()
  },
  methods: {
    inboundTypeLabel(type) {
      return INBOUND_TYPE_MAP[type] || "—"
    },
    initPage() {
      this.loadShopContext().then(() => {
        this.loadOperators()
        this.getList()
      }).catch(() => {
        this.loadOperators()
        this.getList()
      })
    },
    loadShopContext() {
      return getSellerContext().then(response => {
        const data = response.data || {}
        if (data.shopId != null) {
          setSellerShopContext(data.shopId, data.shopName)
        }
      })
    },
    loadOperators() {
      listStockInboundOperators().then(response => {
        this.operatorOptions = response.data || []
      }).catch(() => {
        this.operatorOptions = []
      })
    },
    getList() {
      this.loading = true
      listSellerStockInbound(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.inboundList = response.rows || []
        this.total = response.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleCreate() {
      this.formOpen = true
    },
    handleDetail(row) {
      this.currentInboundId = row.inboundId
      this.detailOpen = true
    },
    handleFormSuccess() {
      this.loadOperators()
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.mb8 {
  margin-bottom: 8px;
}
</style>
