<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select
        v-model="listQuery.shelveState" clearable size="mini" class="filter-item"
        style="width: 200px;"
        placeholder="请选择商品状态"
      >
        <el-option label="全部" value="" />
        <el-option label="已下架" :value="0" />
        <el-option label="已上架" :value="1" />
        <el-option label="待审核" :value="2" />
        <el-option label="审核失败" :value="3" />
      </el-select>
      <el-input
        v-model="listQuery.productId" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入商品ID"
      />
      <el-input
        v-model="listQuery.productName" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入商品名称"
      />
      <el-input
        v-model="listQuery.shopName" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入商户名称"
      />
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <el-button size="mini" type="info" class="filter-item" @click="handleReset">
        重置
      </el-button>
      <br />
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="handleProductDataExport">
        导出商品
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="100" label="商品ID" prop="productId" fixed="left" />
        <el-table-column align="center" width="100" label="商品主图" prop="image">
          <template slot-scope="{ row }">
            <el-image
              v-if="row.image" lazy :src="common.seamingImgUrl(row.image)" style="width:40px; height:40px"
              fit="cover" :preview-src-list="[ common.seamingImgUrl(row.image) ]"
            />
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="productName" label="商品名称" width="180" />
        <el-table-column prop="shopName" label="商家名称" width="180" />
        <el-table-column prop="supplierName" label="供应商名称" width="180" />
        <el-table-column prop="classifyHierarchy" label="分类层级" width="180" />
        <el-table-column prop="sectionPrice" label="售价区间" show-overflow-tooltip />
        <!-- <el-table-column prop="memberSection" label="会员价" show-overflow-tooltip /> -->
        <el-table-column prop="stockNumber" label="库存" show-overflow-tooltip />
        <el-table-column prop="volume" label="实际销售" show-overflow-tooltip />
        <el-table-column prop="fictitiousNumber" label="虚拟销售" show-overflow-tooltip />
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="上架状态" prop="shelveState">
          <template slot-scope="{ row }">
            <el-tag v-if="row.shelveState === 0">已下架</el-tag>
            <el-tag v-else-if="row.shelveState === 1">已上架</el-tag>
            <el-tag v-else-if="row.shelveState === 2">待审核</el-tag>
            <el-tag v-else-if="row.shelveState === 3">审核失败</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="220" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button v-if="row.shelveState === 1" type="text" @click="handleOffShelf(row)">
              强制下架
            </el-button>
            <el-button
              v-if="row.shelveState === 1" type="text"
              @click="$refs.VirtualSales && $refs.VirtualSales.handleOpen(row)"
            >
              虚拟销量
            </el-button>
            <el-button
              v-if="row.shelveState === 2" type="danger" size="mini"
              @click="$refs.ProductReview && $refs.ProductReview.handleOpen(row)"
            >
              审核
            </el-button>
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button
              v-if="row.shelveState === 1" type="danger" size="mini"
              @click="$refs.AddSelection && $refs.AddSelection.handleOpen(row)"
            >
              加入选品
            </el-button>
            <el-button type="text" @click="copyShelf(row)">
              复制商品
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 商品审核 -->
    <ProductReview ref="ProductReview" @success="getList" />
    <!-- 设置虚拟销量 -->
    <VirtualSales ref="VirtualSales" @success="getList" />
    <!-- 加入选品 -->
    <AddSelection ref="AddSelection" :is-copy="isCopy" @success="getList" @changeIsCopy="isCopy = false" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import {
  getClassifyGetAll,
  Forced,
  productExport
} from '@/api/commodity'
import ProductReview from './components/ProductReview'
import VirtualSales from './components/VirtualSales'
import AddSelection from './components/AddSelection'
import DetailModal from './components/DetailModal'

export default {
  name: 'CommoditySystem',
  components: {
    ProductReview,
    VirtualSales,
    AddSelection,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        shelveState: '', // 商品状态 0-已下架 1-已上架 2-待审核 3-审核失败
        productName: '', // 商品名称
        productId: '', // 商品ID
        shopName: '', // 商户名称
        page: 1, // 当前页
        pageSize: 20
      },
      //   控制是否是复制商品
      isCopy: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getClassifyGetAll(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = { shelveState: '', productName: '', productId: '', shopName: '', page: 1, pageSize: 20 }
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleOffShelf(row) {
      this.$confirm('确定下架此商品吗？')
        .then(async () => {
          await Forced({ productId: row.productId })
          this.$message({ message: '下架成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => { })
    },
    //  复制商品
    copyShelf(row) {
      this.$refs.AddSelection && this.$refs.AddSelection.handleOpen(row)
      this.isCopy = true
    },
    // 导出商品
    async handleProductDataExport() {
      this.$message({
        message: '数据导出中，请勿重复操作！',
        type: 'success'
      })
      const res = await productExport(this.listQuery)
      if (!res) {
        return
      }
      const blob = new Blob([ res ], { type: 'application/vnd.ms-excel' })
      const fileName = '商品数据明细表.xls'
      if ('download' in document.createElement('a')) {
        // 非IE下载
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
      } else {
        // IE10+下载
        navigator.msSaveBlob(blob, fileName)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
