<template>
  <div class="app-container" style="padding: 0 20px;padding-bottom: 20px;">

    <el-menu :router="true" default-active="/goods/list/commonly" class="el-menu-demo" mode="horizontal">
      <el-menu-item index="/goods/list">全部商品</el-menu-item>
      <el-menu-item v-if="modules && modules.firstModulesName" index="/goods/list/preferred">{{ modules.firstModulesName }}</el-menu-item>
      <el-menu-item v-if="modules && modules.secondModulesName" index="/goods/list/preferential">{{ modules.secondModulesName }}</el-menu-item>
      <el-menu-item v-if="modules && modules.thirdModulesName" index="/goods/list/commonly">{{ modules.thirdModulesName }}</el-menu-item>
    </el-menu>

    <div class="filter-container">
      <el-form class="demo-form-inline goods-list">
        <el-form-item label="商品ID:" style="width:190px;float: left;">
          <el-input v-model="listQuery.id" placeholder="请输入商品ID" oninput="if(value.length>9)value=value.slice(0,9)" type="number" style="width: 120px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="商品名称:" style="width:240px;float: left;">
          <el-input v-model="listQuery.productName" placeholder="请输入商品名称" style="width: 150px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="供应商名称:" style="width:250px;float: left;">
          <el-input v-model="listQuery.shopName" placeholder="请输入供应商名称" style="width: 150px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="商品分类:" style="width:200px;float: left;">
          <el-select v-model="listQuery.classifyID" placeholder="商品分类" clearable style="width: 110px" class="filter-item" value="" @change="handleFilter">
            <template v-for="item in classify">
              <el-option v-if="item.status*1 === 1" :key="item.id" :label="item.classifyName" :value="item.id" />
            </template>
          </el-select>
        </el-form-item>
        <el-form-item style="width:95px;float: left;">
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
        </el-form-item>
        <el-form-item style="width:150px;float: left;">
          <el-button class="filter-item" type="primary" icon="el-icon-sort" @click="batchSaveSort">批量更新排序</el-button>
        </el-form-item>
        <el-form-item style="width:120px;float: left;">
          <router-link :to="'/goods/list/select/'+ modulesId">
            <el-button class="filter-item" type="primary" icon="el-icon-circle-plus-outline">添加商品</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </div>
    <div class="filter-title">
      <p>排序值是指本页面和商城小程序对应商品列表的排列顺序，数值越小排序越靠前，排序值允许重复。</p>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      style="width:100%;margin-top:20px;"
    >
      <el-table-column align="center" label="序号" width="40px">
        <template slot-scope="scope">
          {{ ((pageNum-1)*listQuery.pageSize)+scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="商品ID" align="center" prop="id" width="70px" />
      <el-table-column label="商品图片" align="center" prop="mainImage">
        <template slot-scope="{row}">
          <img :src="row.mainImage" alt="" width="50">
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="productName" min-width="200px" />
      <el-table-column label="供应商" align="center" prop="shopName" min-width="120px" />
      <el-table-column label="市场价" align="center">
        <template slot-scope="{row}">
          ¥{{ row.normalPrice | toThousandFloatFilter }}
        </template>
      </el-table-column>
      <el-table-column label="成本价" align="center">
        <template slot-scope="{row}">
          ¥{{ row.costPrice | toThousandFloatFilter }}
        </template>
      </el-table-column>
      <el-table-column label="售价" align="center">
        <template slot-scope="{row}">
          <template v-if="row.ladderPriceFlag*1===1">
            <div v-for="(item, index) in row.ladderShownList" :key="row.id+'list_ladder_pri'+index">
              ¥{{ item.price | toThousandFloatFilter }}</div>
          </template>
          <template v-else>¥{{ row.retailPrice | toThousandFloatFilter }}</template>
        </template>
      </el-table-column>
      <el-table-column label="起订量" align="center" width="70">
        <template slot-scope="{row}">
          <template v-if="row.ladderPriceFlag*1===1">
            <div v-for="(item, index) in row.ladderShownList" :key="row.id+'list_ladder_pri'+index">{{ item.number }}</div>
          </template>
          <template v-else>{{ row.minBuyNumber }}</template>
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="productClassifyName" />
      <el-table-column label="排序值" align="center" prop="commonlyProductSort">
        <template slot-scope="{row}">
          <el-input-number v-model="row.commonlyProductSort" controls-position="right" :min="1" :max="999" :maxlength="3" size="mini" style="width:65px;" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" style="width: 70px;margin:5px 0 0 0;" @click="cancelGoods(row)">取消</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间" prop="addTime">
        <template v-if="row.addTime" slot-scope="{row}">
          <span>{{ row.addTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>20" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
    <template>
      <el-backtop style="right: 40px; bottom: 40px;">
        <i class="el-icon-upload2" />
      </el-backtop>
    </template>
  </div>
</template>

<script>
import { getList, getModule, saveGoodsSort, canncelProduct } from '@/api/goods'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      modulesId: '3', // 模块
      pageNum: 0,
      list: null,
      classify: null,
      modules: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 10,
        organizeId: this.$store.getters.organizeId,
        commonlyProduct: '1', // 常用商品
        id: '',
        productName: '',
        shopName: '',
        classifyID: ''
      }
    }
  },
  computed: {
    organizeId() {
      return this.$store.getters.organizeId
    },
    onKeypress() {
      return (/[\d]/.test(String.fromCharCode(event.keyCode)))
    }
  },
  created() {
    this.getModuleType()
    this.fetchData()
  },
  methods: {
    getModuleType() {
      getModule({ organizeId: this.$store.getters.organizeId }).then(response => {
        this.modules = response.data[0]
      })
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        const { organizeProductsPage, listByEffective } = response.data
        this.list = organizeProductsPage.results
        this.total = organizeProductsPage.totalRecord
        this.pageNum = organizeProductsPage.index
        this.classify = listByEffective
        this.listLoading = false
        this.list.map(row => {
          row.ladderShownList = []
          if (row.ladderPriceFlag * 1 === 1 && row.ladderPriceList != null) {
            for (let i = 0; i < row.ladderPriceList.length; i++) {
              if (row.ladderPriceList[i] && row.ladderPriceList[i].delFlag * 1 === 0) {
                let str = ''
                if (row.ladderPriceList[i + 1] && row.ladderPriceList[i + 1].delFlag * 1 === 0) {
                  str = row.ladderPriceList[i].buyNum + '-' + (row.ladderPriceList[i + 1].buyNum - 1)
                } else {
                  str = '≥' + row.ladderPriceList[i].buyNum
                }
                row.ladderShownList.push({
                  price: row.ladderPriceList[i].buyPrice,
                  number: str
                })
              }
            }
          }
        })
      })
    },
    handleFilter() {
      this.fetchData()
    },
    batchSaveSort() {
      const sorts = []
      this.list.map(item => {
        if (item.commonlyProductSort) {
          sorts.push(item.id + '-' + Math.abs(item.commonlyProductSort))
        }
      })
      saveGoodsSort({ newProducSorttList: sorts.join(','), modules: this.modulesId }).then(response => {
        this.$message({ message: response.msg, type: 'success', center: true })
        this.clearListQuery()
        this.fetchData()
      }).catch(() => {
        this.clearListQuery()
        this.fetchData()
      })
    },
    cancelGoods(row) {
      this.$confirm('确定取消该商品吗？取消后该商品将不在出现在本页面', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        canncelProduct({ id: row.id, modules: this.modulesId }).then(() => {
          this.clearListQuery()
          this.fetchData()
        }).catch(() => {
          this.clearListQuery()
          this.fetchData()
        })
      })
    },
    clearListQuery() {
      this.listQuery.id = ''
      this.listQuery.productName = ''
      this.listQuery.shopName = ''
      this.listQuery.classifyID = ''
    }
  }
}
</script>
<style>
.el-input-number.is-controls-right .el-input__inner {
    padding-left: 5px;
    padding-right: 30px;
}
</style>
<style scoped>

.filter-container .filter-item,.el-form-item{
  margin-bottom: 0;
}
.filter-container{
  width: 100%;
  float: left;
  height: auto;
  margin-top:20px;
  padding-bottom: 0;
}
.filter-title{
  float: left;
  font-size: 14px;
  line-height: 18px;
  text-align: left;
  color: #2E3748;
}
.filter-title p{
  margin:10px 0;
}
.goods-list .el-form-item{
  margin-bottom: 8px;
}
</style>

