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

    <div class="search">
      <div>
        <el-input
          v-model="query.goodsName"
          size="small"
          placeholder="请输入商品名称"
          class="item-width-200 "
        ></el-input>

        <el-input
          v-model="query.shopId"
          size="small"
          placeholder="请输入店铺ID"
          class="item-width-200 ml10"
        ></el-input>

        <el-select
          v-model="platformType"
          multiple
          size="small"
          clearable
          class="item-width-200 ml10"
          placeholder="请选择所属平台"
        >
          <el-option
            v-for="item in masterShopOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>

        <el-button
        class="ml10"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      </div>
    </div>

   <el-tabs v-model="activeName">
    <el-tab-pane label="上架商品" name="上架商品"></el-tab-pane>
    <el-tab-pane label="待审核商品" name="待审核商品"></el-tab-pane>
    <el-tab-pane label="审核未通过商品" name="审核未通过商品"></el-tab-pane>
  </el-tabs>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >

      <el-table-column
        align="center"
        label="商品图片"
        prop="goodsName"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        <img class="icon" :src="scope.row.goodsImg" alt="">
      </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="商品名称"
        prop="goodsName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="goodsType"
        align="center"
        label="类型"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
        {{goodsTypeObj[scope.row.goodsType] || '-' }}
       </template>
      </el-table-column>
      <el-table-column
        prop="state"
        align="center"
        label="商品状态"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
        <span>{{scope.row.status|filterState}}</span>
       </template>
      </el-table-column>
       <el-table-column
          prop="accountChildType"
          align="center"
          label="操作平台"
          fit
        >
       <template slot-scope="scope">
            <div>{{filterMasterName(scope.row.prePlatformType)}}</div>
          </template>
        </el-table-column>

      <el-table-column
        prop="shopName"
        align="center"
        label="店铺名称"
        show-overflow-tooltip
      >
      </el-table-column>

     

       <el-table-column
          prop="accountChildType"
          align="center"
          label="所属平台"
          fit
        >
       <template slot-scope="scope">

         <div v-for="item in JSON.parse(scope.row.prices)" :key="item.platformType">
            <div>{{filterMasterName(item.platformType)}}</div>
          </div>
          </template>
        </el-table-column>

      <template v-if="activeName == '审核未通过商品'">
        <el-table-column
          prop="applyTime"
          align="center"
          label="申请时间"
          show-overflow-tooltip
        >
        </el-table-column>
        
        <el-table-column
          prop="checkTime"
          align="center"
          label="审核时间"
          show-overflow-tooltip
        >
        </el-table-column>
      </template>

      <el-table-column prop="address" align="center" label="操作">
        <template slot-scope="scope">
           <el-button type="text" size="small" v-if="activeName== '上架商品' || activeName== '审核未通过商品'" @click="handleDetail(scope.row,'detail')">查看</el-button>
          <el-button type="text" size="small" v-if="activeName== '待审核商品'" @click="handleDetail(scope.row,'audit')">审核</el-button>
          <el-button type="text" size="small" v-else-if="activeName== '上架商品'" @click="handleDetail(scope.row,'edit')">配置</el-button>
          <el-button v-if="scope.row.status == 3" type="text" size="small" @click="handleApply(scope.row,4)">下架</el-button>
          <el-button v-if="scope.row.status == 4" type="text" size="small" @click="handleApply(scope.row,3)">上架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

  <script>
import { debounce } from "@/utils/index";
import { goodsList,operateByAdmin } from '@/api/goods';
export default {
  name: "goodsList",
  data() {
    return {
      activeName:'上架商品',
      options: [
        {
          value: "3",
          label: "上架",
        },
        {
          value: "4",
          label: "下架",
        },
      ],
      tableData: [],
      loading: false,
      goodsTypeObj:JSON.parse(sessionStorage.getItem('goodsTypeObj')),
      platformType:"",
      query: {
        platformType:null,
        applyState:'',
        status: '3',
        goodsName: "",
        currentPage: 1,
        pageSize: 10,
      },
      title: "",
      total: 0,
    };
  },
  watch: {
    activeName(val){
      if(val == '上架商品'){
        this.query.status = 3
        this.query.applyState = ''

      }else if(val == '待审核商品'){
        this.query.status = null
        this.query.applyState = 1
      }else if(val == '审核未通过商品'){
        this.query.status = ''
        this.query.applyState = 3
      }
      this.query.currentPage = 1
        this.goodsList()

    },
    platformType(val) {
        if(val.length){
          this.query.platformType = val.join(',')
        }else{
          this.query.platformType = null
        }
      }
    // query: {
    //   handler: debounce(function (val) {
    //     this.goodsList()
    //   }),
    //   deep: true,
    // },
  },
  filters: {
    filterState: function(value) {
    //  0-保存 1-待上架，2-待下架，3-上架，4-下架
      if (value == 0) {
        return '保存'
      }else if (value == 1) {
        return '待上架'
      }
      else if (value == 2) {
        return '待下架'
      }
      else if (value == 3) {
        return '上架'
      }
      else if (value == 4) {
        return '下架'
      }
    },
    filterType:function(val){
      if(val== 801){
        return '研学商品'
      }else if(val == 802){
        return '虚拟商品充值'
      }else if(val == 803){
        return '餐饮商品'
      }else{
        return '新添加商品'

      }
    }
  },
  methods: {
    handleSearch(){
      this.query.currentPage = 1;
      this.goodsList()
    },
    handleDetail(row,mode){
      this.$router.push({
        path:'/operationManage/goodsManage/goodsDetail',
        query:{
          id:row.goodsId,
          mode,
          // shopId:row.shopId
        }
      })
    },
    handleAdd(){
      this.$router.push('/operationManage/goodsManage/addGoods')
    },
    // 点击下架
    handleApply(row,state) {
       let msg;
       if(row.status == 3){//已上架
            msg = '下架'
        }else if(row.status == 4){//已下架
            msg = '上架'
        }
      this.$confirm(`此操作将${msg}该商品, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          operateByAdmin({goodsId:row.goodsId,state}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.goodsList()
            }
          })
        })
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
      this.goodsList()

    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.goodsList()

    },

    goodsList(){
      this.loading = true
      goodsList(this.query).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.records
          this.total = res.content.total
        }
      })
    }
  },
  created() {
    this.goodsList()
  },
};
</script>

<style lang="scss" scoped>
.goodsList {
  .search{
    justify-content: space-between;
  }
  .icon{
    width: 80px;
    height: 80px;
  }
}
</style>
