<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="商品分类" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择商品分类" clearable>
          <el-option v-for="dict in typeIdOptions" :key="dict.id" :label="dict.name" :value="dict.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品标题" prop="title"><el-input v-model="queryParams.title" placeholder="请输入商品标题" clearable @keyup.enter.native="handleQuery" /></el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" icon="el-icon-plus" :disabled="ids.length > 0" @click="handleOp" v-hasPermi="['system/shop:goods:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit" :disabled="ids.length != 1" @click="handleOp" v-hasPermi="['system/shop:goods:edit']">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" :disabled="ids.length == 0" @click="handleDelete" v-hasPermi="['system/shop:goods:remove']">批量删除</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" border :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品标题" align="center" prop="title" />
      <el-table-column label="商品类型" align="center" prop="tbGoodsType.name" />
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <el-image class="lsw_square" :fit="'contain'" style="max-width:80px;" :src="baseUrl+scope.row.pic" :preview-src-list="[baseUrl+scope.row.pic]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" prop="price" />
      <el-table-column label="状态" align="center" prop="state" sortable width="120">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.state == 1">下架</el-tag>
          <el-tag type="success" v-if="scope.row.state == 0">上架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleOp(scope.row)" v-hasPermi="['system/shop:goods:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system/shop:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 添加或修改对话框 -->
    <edit v-if="dialogVisible" ref="dialog" @refresh="getList"></edit>
  </div>
</template>

<script>
import { typeList, listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods } from '@/api/system/shop/goods';
import edit from './edit';
export default {
  components: {
    edit
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      //新增,修改弹出框
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      // 日期范围
      dateRange: [],
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      typeIdOptions: [],
      // 商品表格数据
      goodsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        title: null,
        createTime: null,
        orderByColumn: 'id', //排序字段
        isAsc: 'desc' //排序方式
      }
    };
  },
  created() {
    typeList().then(response => {
      this.typeIdOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
    },
    /** 新增,修改按钮操作 */
    handleOp(row) {
      this.dialogVisible = true;
      const ids = row.id || this.ids;
      this.$nextTick(() => {
        this.$refs.dialog.init(ids);
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品编号为"' + ids + '"的数据项?', '警告', { type: 'warning' })
        .then(() => {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    }
  }
};
</script>
