<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入商品名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="所属供应商" prop="supplierId">
          <el-select v-model="queryParams.supplierId" placeholder="请选择所属供应商" clearable>
            <el-option v-for="(v,i) in supplierList" :key="i" :label="v.supplierName" :value="v.supplierId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="置顶状态" prop="recommendStatus">
          <el-select v-model="queryParams.recommendStatus" placeholder="请选择上架状态" clearable>
            <el-option label="置顶" :value="true"></el-option>
            <el-option label="未置顶" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态" prop="shelvedStatus">
          <el-select v-model="queryParams.shelvedStatus" placeholder="请选择上架状态" clearable>
            <el-option label="已上架" :value="true"></el-option>
            <el-option label="已下架" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">发布商品</el-button>
        <el-button type="primary" plain size="mini" @click="handleExport">导出商品</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <div class="table-container">
      <el-table v-loading="loading" :data="list" @sort-change="sortChange">
        <el-table-column prop="index" label="序号" width="60" align="center">
          <template slot-scope="scope">
            <div>
              {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品图片" width="100" align="center">
          <template slot-scope="scope">
            <el-image style="width: 50px; height: 50px" :src="scope.row.coverImage" :preview-src-list="scope.row.coverImage ? [scope.row.coverImage] : []"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="supplierId" label="所属供应商" align="center">
          <template slot-scope="scope">
            <div>
              {{scope.row.supplier.supplierName}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="categoryId" label="所属分类" align="center">
          <template slot-scope="scope">
            <div>
              {{scope.row.productCategory.categoryName}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="salePrice" label="价格" align="center"></el-table-column>
        <el-table-column prop="realSales" label="实际销量" align="center"></el-table-column>
        <el-table-column prop="points" label="所需兑换豆" align="center"></el-table-column>
        <el-table-column prop="stock" label="库存" align="center"></el-table-column>
        <el-table-column prop="orderIndex" label="排序" width="80" align="center" sortable="custom"></el-table-column>
        <el-table-column label="是否置顶" width="100" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.recommendStatus" :active-value="true" :inactive-value="false" @change="setRecommendStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="上架状态" width="100" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.shelvedStatus" :active-value="true" :inactive-value="false" @change="setStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" width="100px"></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { supplierList, productPage, productShelvedStatus, productDel, productRecommendStatus } from '@/api/pointsMall.js'
export default {
  name: 'GoodsList',
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierId: undefined,
        shelvedStatus: undefined,
        title: undefined,
        recommendStatus: undefined
      },
      supplierList: []
    }
  },
  created () {
    let queryParams = localStorage.getItem('queryParams');
    if (queryParams) {
      this.queryParams = JSON.parse(queryParams);
      localStorage.setItem('queryParams', '');
    }
    this.getSupplierList()
    this.getList();
  },
  methods: {
    getSupplierList () {
      supplierList().then(res => {
        this.supplierList = res.data
      })
    },
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd () {
      this.$router.push('/pointsMall/goodsList/add')
    },
    //  导出积分商品
    handleExport () {
      this.download('/shop/product/export', {
        ...this.queryParams
      }, `积分商品_${new Date().getTime()}.xlsx`)
    },
    handleEdit (row) {
      this.$router.push({
        path: '/pointsMall/goodsList/add',
        query: {
          productId: row.productId,
          queryParams: JSON.stringify(this.queryParams)
        }
      })
    },
    sortChange (e) {
      this.queryParams.orderByColumnName = 'order_index';
      this.queryParams.ascType = e.order;
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.productId || this.ids;
      this.$modal.confirm('是否确认删除商品名称为"' + row.title + '"的数据项？').then(() => {
        this.loading = true;
        return productDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    handleStatusChange (row) {
      const statusText = row.status === 1 ? '上架' : '下架'
      this.$message.success(`${statusText}成功`)
    },
    getList () {
      this.loading = true;
      this.queryParams["startTime"] = undefined;
      this.queryParams["endTime"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["startTime"] = this.daterangeCreateTime[0];
        this.queryParams["endTime"] = this.daterangeCreateTime[1];
      }
      productPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 上下架按钮操作 */
    setStatus (row) {
      const ids = row.productId || this.ids;
      let statusText = row.shelvedStatus ? '上架' : '下架';
      // this.$modal.confirm('是否确认' + statusText + '商品名称为"' + row.title + '"的数据项？').then(() => {
      //   this.loading = true;
      //   return productShelvedStatus(ids);
      // }).then(() => {
      //   this.loading = false;
      //   this.getList();
      //   this.$modal.msgSuccess(statusText + "成功");
      // }).finally(() => {
      //   this.loading = false;
      // });
      productShelvedStatus(ids).then(res => {
        this.loading = false;
        this.$modal.msgSuccess(statusText + "成功");
      }).finally(() => {
        this.loading = false;
        this.getList();
      });
    },
    /** 修改置顶按钮操作 */
    setRecommendStatus (row) {
      const ids = row.productId || this.ids;
      let statusText = row.recommendStatus ? '开启置顶' : '取消置顶';
      productRecommendStatus(ids).then(res => {
        this.loading = false;
        this.$modal.msgSuccess(statusText + "成功");
      }).finally(() => {
        this.loading = false;
        this.getList();
      });
    }
  },
}
</script>

<style lang="scss" scoped>
</style>