<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="70px">
        <el-form-item label="关键词" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="输入关键词"
            clearable
            style="width: 220px"
            @keyup.enter.native="handleQuery"
          >
            <el-button slot="append" icon="el-icon-search" @click="handleQuery" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <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-tabs v-model="directionTab" @tab-click="handleTabClick">
        <el-tab-pane label="全部" name="all" />
        <el-tab-pane label="入库" name="1" />
        <el-tab-pane label="出库" name="2" />
      </el-tabs>

      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="logList" :empty-text="emptyTableText">
        <!-- <el-table-column type="selection" width="50" align="center" /> -->
        <el-table-column label="商品名称" align="left" min-width="100" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div class="goods-name-cell">
              <!-- <div v-if="scope.row.goodsId" class="goods-id">ID {{ scope.row.goodsId }}</div> -->
              <div>{{ scope.row.goodsName || '—' }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品规格" align="center" min-width="280" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ specText(scope.row.specText) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="变化前库存" align="center" prop="stockBefore" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.stockBefore != null ? scope.row.stockBefore : '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="变化后库存" align="center" prop="stockAfter" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.stockAfter != null ? scope.row.stockAfter : '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="变化量" align="center" prop="changeQty" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.changeQty != null ? scope.row.changeQty : '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="130" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ operationText(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) || '—' }}</span>
          </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>
  </div>
</template>

<script>
import { listSellerStockLog } from "@/api/agri/seller/stock/log"
import { getSellerContext } from "@/api/agri/seller/context"
import { setSellerShopContext } from "@/utils/sellerShop"

/** UI 可将「取消订单返库存」简写 */
const REASON_TEXT_MAP = {
  ORDER_CANCEL: "取消订单返库"
}

export default {
  name: "AgriSellerStockLog",
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      logList: [],
      directionTab: "all",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        direction: undefined
      }
    }
  },
  computed: {
    hasSearchFilter() {
      return !!(this.queryParams.keyword || this.queryParams.direction)
    },
    emptyTableText() {
      return this.hasSearchFilter ? "未找到符合条件的库存日志" : "暂无库存日志"
    }
  },
  created() {
    this.initPage()
  },
  methods: {
    specText(text) {
      const val = text ? String(text).trim() : ""
      return val && val !== "—" ? val : "—"
    },
    operationText(row) {
      if (row.changeReasonText) {
        if (row.changeReason === "ORDER_CANCEL") {
          return REASON_TEXT_MAP.ORDER_CANCEL
        }
        return row.changeReasonText
      }
      return row.changeReason || "—"
    },
    initPage() {
      this.loadShopContext().then(() => {
        this.getList()
      }).catch(() => {
        this.getList()
      })
    },
    loadShopContext() {
      return getSellerContext().then(response => {
        const data = response.data || {}
        if (data.shopId != null) {
          setSellerShopContext(data.shopId, data.shopName)
        }
      })
    },
    getList() {
      this.loading = true
      listSellerStockLog(this.queryParams).then(response => {
        this.logList = response.rows || []
        this.total = response.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleTabClick() {
      this.queryParams.direction = this.directionTab === "all" ? undefined : this.directionTab
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.directionTab = "all"
      this.queryParams.direction = undefined
      this.queryParams.pageNum = 1
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.mb8 {
  margin-bottom: 8px;
}
.goods-name-cell {
  line-height: 1.4;
}
.goods-id {
  font-size: 12px;
  color: #909399;
}
</style>
