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

    <el-menu :router="true" default-active="/goods/list/preferred" 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:225px;float: left;margin-bottom: 8px;">
          <el-input v-model="listQuery.id" placeholder="请输入商品ID" maxlength="30" style="width: 150px;" 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;">
          <el-button class="filter-item" type="primary" icon="el-icon-circle-plus-outline">
            <router-link :to="'/goods/list/select/'+ modulesId">添加商品</router-link>
          </el-button>
        </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="50px">
        <template slot-scope="scope">
          {{ 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" 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}">
          <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="mini" style="width: 80px;margin:5px 0 0 0;" @click="cancelGoods(row)">取消</el-button>
        </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: '1', // 模块1
      list: null,
      classify: null,
      modules: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 20,
        organizeID: this.$store.getters.organizeID,
        preferredProduct: '1', // 星范优选
        id: '',
        productName: '',
        shopName: '',
        classifyID: ''
      }
    }
  },
  computed: {
    organizeID() {
      return this.$store.getters.organizeID
    }
  },
  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.classify = listByEffective
        this.listLoading = false
      })
    },
    handleFilter() {
      this.fetchData()
    },
    batchSaveSort() {
      const sorts = []
      this.list.map(item => {
        if (item.preferredProductSort) {
          sorts.push(item.id + '-' + item.preferredProductSort)
        }
      })
      saveGoodsSort({ newProducSorttList: sorts.join(','), modules: this.modulesId }).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(row) {
      this.$confirm('确定取消该商品吗？取消后该商品将不在出现在本页面', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        canncelProduct({ id: row.id, modules: this.modulesId }).then(() => {
          this.fetchData()
        }).catch(() => {
          this.fetchData()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container .filter-item,.el-form-item{
  margin-bottom: 0;
}
.filter-container{
  width: 100%;
  height: auto;
  float: left;
  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>

