<template>
  <div class="app-container coupon-list">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="商品名称">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="queryParams.pageNum = 1;getList()"
        />
      </el-form-item>
      <el-form-item label="商品ID">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="queryParams.pageNum = 1;getList()"
        />
      </el-form-item>
      <el-form-item label="商品分类">
       <el-select
          v-model="queryParams.categoryId"
          placeholder="请选择商品分类"
          clearable
          @change="queryParams.pageNum = 1;getList()"
        >
          <el-option :label="item.name" :value="item.categoryId" v-for="(item, index) in goodsCategoryItemsList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品标签">
        <el-select
          v-model="queryParams.tagId"
          placeholder="请选择商品标签"
          clearable
          @change="queryParams.pageNum = 1;getList()"
        >
          <el-option :label="item.name" :value="item.tagId" v-for="(item, index) in goodsTagItemsList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态">
        <el-select v-model="queryParams.status" placeholder="请选择商品状态" clearable @change="queryParams.pageNum = 1;getList()">
          <el-option label="全部" value="" />
          <el-option label="待上架" value="init" />
          <el-option label="已上架" value="on" />
          <el-option label="已下架" value="off" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否支持兑换">
        <el-select v-model="queryParams.exchangeShow" placeholder="请选择是否支持兑换" clearable @change="queryParams.pageNum = 1;getList()">
          <el-option label="全部" value="" />
          <el-option label="支持兑换" value="1" />
          <el-option label="不支持兑换" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="采购价格">
        <el-input v-model="queryParams.minCost" placeholder="最低采购价格" clearable @keyup.enter.native="queryParams.pageNum = 1;getList()" />
      </el-form-item>
      <el-form-item label="-">
        <el-input v-model="queryParams.maxCost" placeholder="最高采购价格" clearable @keyup.enter.native="queryParams.pageNum = 1;getList()" />
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="queryParams.minValue" placeholder="最低价格" clearable @keyup.enter.native="queryParams.pageNum = 1;getList()" />
      </el-form-item>
      <el-form-item label="-">
        <el-input v-model="queryParams.maxValue" placeholder="最高价格" clearable @keyup.enter.native="queryParams.pageNum = 1;getList()" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="queryParams.pageNum = 1;getList()">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getList(true)">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="10">
        <el-button v-hasPermi="['business:goods:add']" type="primary" icon="el-icon-plus" size="mini" @click="$router.push('/goods/add')">添加商品</el-button>
        <!-- <el-button v-hasPermi="['business:goods:on']" type="primary" plain size="mini">上架</el-button> -->
        <!-- <el-button v-hasPermi="['business:goods:off']" type="primary" plain size="mini">下架</el-button> -->
        <!-- <el-button v-hasPermi="['business:goods:remove']" type="danger" plain size="mini">删除</el-button> -->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="商品ID" prop="goodsId" width="60" />
      <el-table-column label="商品图片" prop="picUrl" align="center">
        <template slot-scope="{row}">
          <div v-if="row.picUrl">
            <el-image
              style="width: 100px; height: 100px"
              :src="IMG_URL + row.picUrl.split(',')[0]"
              :preview-src-list="row.picUrl.split(',').map(item => { return IMG_URL + item })"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="title" />
      <el-table-column label="价格" prop="value">
        <template slot-scope="{row}">
          ¥{{$numberFormat(row.value)}}
        </template>
      </el-table-column>
      <el-table-column label="采购价格" prop="cost">
        <template slot-scope="{row}">
          ¥{{$numberFormat(row.cost)}}
        </template>
      </el-table-column>
      <el-table-column label="是否支持兑换" prop="exchangeShow">
        <template slot-scope="{row}">
          <span v-if="row.exchangeShow == '1'">支持</span>
          <span v-if="row.exchangeShow == '0'">不支持</span>
        </template>
      </el-table-column>
      <el-table-column label="兑换盲豆数量" prop="exchangePrice" />
      <el-table-column label="销量" prop="exchangedQty" />
      <el-table-column label="库存" prop="quantity" />
      <el-table-column label="状态" prop="status">
        <template slot-scope="{row}">
          <el-tag :type="JSON.parse(row.status).value === 'on' ? 'success' : 'info'">{{ JSON.parse(row.status).desc }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="排序" prop="sort" /> -->
      <el-table-column fixed="right" align="right" label="操作" width="140">
        <template slot-scope="{row}">
          <!-- <el-button v-hasPermi="['business:goods:query']" type="text">查看</el-button> -->
          <el-button v-hasPermi="['business:goods:edit']" type="text" @click="$router.push('/goods/edit?id=' + row.goodsId)">编辑</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'off' || JSON.parse(row.status).value === 'init'" v-hasPermi="['business:goods:on']" type="text" @click="setStatus(row, 'on')">上架</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'on'" v-hasPermi="['business:goods:off']" type="text" @click="setStatus(row, 'off')">下架</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'init'" v-hasPermi="['business:coupon:remove']" class="del" type="text" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList()" />
  </div>
</template>
<script>
import { publicFileGetUrl } from "@/api/common"
import { getGoodsList, delGoods, setGoodsStatus } from '@/api/business/goods'
import { goodsCategoryItems } from '@/api/business/category'
import { goodsTagItems } from '@/api/business/tag'
import { accMul } from '@/utils/util'
export default {
  name: 'List',
  data() {
    return {
      IMG_URL: publicFileGetUrl,
      loading: false,
      showSearch: true,
      queryParams: {},
      tableData: [],
      total: 0,
      goodsCategoryItemsList: [],
      goodsTagItemsList: [],
    }
  },
  created() {
    this.getList(true)
    this.getGoodsCategoryItems()
    this.getGoodsTagItems()
  },
  methods: {
    getGoodsCategoryItems(){
      goodsCategoryItems({}).then(res => {
        this.goodsCategoryItemsList = res && res.data
      })
    },
    getGoodsTagItems(){
      goodsTagItems({}).then(res => {
        this.goodsTagItemsList = res && res.data
      })
    },
    getList(reset) {
      if (this.loading) {
        return
      }
      this.loading = true
      if (reset) {
        this.queryParams = { pageNum: 1, pageSize: 20 }
      }
      getGoodsList('pageNum='+this.queryParams.pageNum + '&pageSize='+this.queryParams.pageSize+'&', {
        title: this.queryParams.title,
        goodsId: this.queryParams.goodsId,
        categoryId: this.queryParams.categoryId,
        tagId: this.queryParams.tagId,
        status: this.queryParams.status,
        exchangeShow: this.queryParams.exchangeShow,
        minCost: accMul(this.queryParams.minCost, 100),
        maxCost: accMul(this.queryParams.maxCost, 100),
        minValue: accMul(this.queryParams.minValue, 100),
        maxValue: accMul(this.queryParams.maxValue, 100)
      }).then(res => {
        this.loading = false
        if (res.code === 0) {
          this.tableData = res.rows
          this.total = res.total
        }
      }).catch(() => {
        this.loading = false
      })
    },
    setStatus(item, status) {
      this.$confirm(`确认${status === 'on' ? '上架' : '下架'}商品 “${item.title}” 吗？`, `${status === 'on' ? '上架' : '下架'}商品`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        setGoodsStatus({
          goodsId: item.goodsId,
          status
        }).then(res => {
          if (res.code === 0) {
            this.$message.success('操作已完成！')
            this.getList()
          }
        })
      })
    },
    del(item) {
      this.$confirm(`确认删除商品 “${item.title}” 吗？`, '删除商品', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delGoods(item.goodsId).then(res => {
          if (res.code === 0) {
            this.$message.success('操作已完成！')
            this.getList()
          }
        })
      })
    }
  }
}
</script>
