<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.search" clearable size="mini" class="filter-item"
        style="width: 200px;"
        placeholder="请输入商品名称"
      />
      <el-select
        v-model="listQuery.stock" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择库存状态"
      >
        <el-option label="全部" :value="null" />
        <el-option label="有库存" :value="1" />
        <el-option label="无库存" :value="0" />
      </el-select>
      <el-cascader
        v-model="listQuery.groupId" placeholder="请选择分组类型" :options="groupList"
        :props="{ checkStrictly: true, expandTrigger: 'hover', label: 'groupName', value: 'shopGroupId', children: 'childs' }"
        clearable size="mini" class="filter-item" style="width: 200px;margin-left: 10px;"
      />
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left: 10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.PlatformSelection && $refs.PlatformSelection.handleOpen()"
      >
        加入平台选品
      </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" prop="productId" label="商品id" show-overflow-tooltip />
        <el-table-column align="center" width="100" label="公司图片" prop="productImage">
          <template slot-scope="{ row }">
            <el-image
              v-if="row.productImage" lazy :src="common.seamingImgUrl(row.productImage)"
              style="width:40px; height:40px" fit="cover"
              :preview-src-list="[ common.seamingImgUrl(row.productImage) ]"
            />
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="100" label="公司图片" prop="productImage">
          <template slot-scope="{ row }">
            <div v-if="row.goodsRotationChart && row.goodsRotationChart.split(',').length">
              <el-image
                :src="row.goodsRotationChart.split(',')[0]" style="width:40px; height:40px" fit="cover"
                :preview-src-list="row.goodsRotationChart.split(',')"
              />
              <span v-if="row.goodsRotationChart.split(',').length > 1" style="margin-left:8px;">
                +{{
                  row.goodsRotationChart.split(',').length }}
              </span>
            </div>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="productName" label="商品名称" width="220" />
        <el-table-column align="center" min-width="150" prop="manufacturerBasePriceInterval" label="供货价区间" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" prop="sectionOriginalPrice" label="原价区间" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" prop="sectionPrice" label="售价区间" show-overflow-tooltip />
        <el-table-column align="center" width="100" prop="memberSection" label="会员价" show-overflow-tooltip />
        <el-table-column align="center" width="100" prop="ifCredit" label="积分兑换" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.ifCredit == 1">允许</span>
            <span v-if="scope.row.ifCredit == 0">不允许</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="100" prop="stockNumber" label="库存" show-overflow-tooltip />
        <el-table-column align="center" width="100" prop="volume" label="销量" show-overflow-tooltip />
        <el-table-column align="center" width="100" prop="volume" label="上架状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.shelveState == 0">未上架</span>
            <span v-if="scope.row.shelveState == 1">已上架</span>
            <span v-if="scope.row.shelveState == 2">待审核</span>
            <span v-if="scope.row.shelveState == 3">审核失败</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" prop="reject" label="驳回原因" show-overflow-tooltip />
        <el-table-column align="center" width="100" prop="brandName" label="品牌" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" prop="createTime" label="创建时间" width="180" />
        <el-table-column align="center" label="操作" width="100" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button size="mini" @click="handleDetail(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>

    <!-- 新增编辑 -->
    <PlatformSelection ref="PlatformSelection" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" @success="getList" />
  </div>
</template>

<script>
import { getShopId } from '@/utils/auth'
import PlatformSelection from './components/PlatformSelection'
import DetailModal from './components/DetailModal'
import { shopSelectionGetLibrary } from '@/api/selectionCenter/selectionCenter'
import { getBrandList, getGroupSelect } from '@/api/commodity'
import XeUtils from 'xe-utils'

export default {
  name: 'ProductSelectionList',
  components: {
    PlatformSelection,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        search: '',
        groupId: [],
        stock: ''
      },
      groupList: []
    }
  },
  created() {
    this.getList()
    this.getGroupDataList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await shopSelectionGetLibrary({
          ...this.listQuery,
          groupId: Array.isArray(this.listQuery.groupId) && this.listQuery.groupId.length ? this.listQuery.groupId[this.listQuery.groupId.length - 1] : ''
        })
        const { data: brandList } = await getBrandList()
        this.list = res.data.list.map((item) => {
          item.brandName = (brandList.find((section) => section.id === item.brandId) || { brandName: '' }).brandName
          return item
        })
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    async getGroupDataList() {
      const result = await getGroupSelect({ shopId: getShopId() })
      XeUtils.eachTree(result.data, (item) => {
        if (Array.isArray(item.childs) && item.childs.length === 0) {
          item.childs = undefined
        }
      }, { children: 'childs' })
      this.groupList = result.data
    }
  }
}
</script>

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

	.filter-container {
		// padding-bottom: 10px;

		.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>
