<template>
  <div class="app-container">

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

    <div class="filter-container" style="margin-top:20px;">
      <el-input v-model="listQuery.id" placeholder="商品ID" style="width: 120px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.productName" placeholder="商品名称" style="width: 120px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.shopName" placeholder="供应商名称" style="width: 120px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <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-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-sort" @click="batchSaveSort">批量更新排序</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-circle-plus-outline">
        <router-link :to="'/goods/list/select/'+ activeIndex">添加商品</router-link>
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width:100%;margin-top:20px;"
    >
      <el-table-column align="center" label="序号" width="50px">
        <template slot-scope="scope">
          {{ scope.$index }}
        </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" prop="normalPrice" />
      <el-table-column label="成本价" align="center" prop="costPrice" />
      <el-table-column label="售价" align="center" prop="retailPrice" />
      <el-table-column label="起订量" align="center" prop="minBuyNumber" />
      <el-table-column label="商品分类" align="center" prop="productClassifyName" />
      <el-table-column align="center" label="添加时间" prop="addTime">
        <template v-if="row.addTime" slot-scope="{row}">
          <i class="el-icon-time" />
          <span>{{ row.addTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序值" align="center" prop="preferredProductSort">
        <template slot-scope="{row}">
          <el-input v-model="row.preferredProductSort" size="small" type="number" min="1" />
        </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="small" @click="cancelGoods(row.id)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

  </div>
</template>

<script>
import { getList, getModule, saveGoodsSort, canncelProduct } from '@/api/goods'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      activeIndex: '1', // 模块1
      list: null,
      classify: null,
      modules: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 20,
        organizeID: this.organizeID,
        preferredProduct: '1', // 星范优选
        id: '',
        productName: '',
        shopName: '',
        classifyID: ''
      }
    }
  },
  created() {
    this.getModuleType()
    this.fetchData()
  },
  methods: {
    getModuleType() {
      getModule({ organizeID: this.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.classify = listByEffective
        this.listLoading = false
      })
    },
    handleFilter() {
      this.fetchData()
    },
    batchSaveSort() {
      const sorts = []
      this.list.map(item => {
        sorts.push(item.id + '-' + item.preferredProductSort)
      })
      saveGoodsSort({ newProducSorttList: sorts.join(','), modules: this.activeIndex }).then(response => {
        this.$notify({
          title: response.code * 1 === 1 ? 'Success' : 'Error',
          message: response.msg,
          type: response.code * 1 === 1 ? 'success' : 'error',
          duration: 2000
        })
        this.fetchData()
      }).catch(() => {
        this.fetchData()
      })
    },
    cancelGoods(id) {
      canncelProduct({ id: id, modules: this.activeIndex }).then(() => {
        this.fetchData()
      }).catch(() => {
        this.fetchData()
      })
    }
  }
}
</script>

<style scoped>

</style>

