<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter.native="handleQuery">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="商品分类" prop="category">
        <SelectCategory v-model="queryParams.category" />    
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['system:goods:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:goods:add']">新增</el-button>
      </el-col>      
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>
    <PutShopOffShop ref="putShopOffShop" @callback="getList" />
    <el-table v-loading="loading" :data="spuList" >
      <el-table-column label="商品名称" align="center" prop="name" width="150">
        <template slot-scope="scope">
          <a @click="handleUpdate(scope.row)" style="color: blue">{{ scope.row.name }}</a>
        </template>  
      </el-table-column>
      <el-table-column label="编码" align="center" prop="code" width="150" />
      <el-table-column label="缩略图" align="center" prop="" width="150">
        <template slot-scope="scope">              
           <div class="image-tmp">
            <el-image style="width: 100px; height: 100px"  :src="getPicsList(scope.row.pics)" :preview-src-list="getPicsBigList(scope.row.pics)">
              <div slot="error" class="image-slot"></div>            
            </el-image>
          </div>    
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="categoryName" width="100" />
      <el-table-column label="简介" align="center" prop="desc" width="150" />
      <el-table-column label="售价" align="center" prop="price" />
      <el-table-column label="总销量" align="center" prop="salesVolume" width="100" />
      <el-table-column label="规格详情" align="center" width="100">
        <template slot-scope="scope">
          <el-button  @click.native.prevent="setGoodsConfig(scope.row)"  type="text"  size="small">
            配置
          </el-button>          
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="150" />
      <el-table-column label="上架时间" align="center" prop="shelfAt" width="150" /> 
      <el-table-column label="状态" align="center" prop="state" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">已创建</span>
          <span v-if="scope.row.state == 2">已完成编程</span>
          <span v-if="scope.row.state == 3">已上架</span>
          <span v-if="scope.row.state == 4">已下架</span>
          <span v-if="scope.row.state == 5">已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  @click="handleUpdateState(scope.row)"
            v-if="authBtn(['1'], scope.row.state)" v-hasPermi="['system:goods:finish']">已完成编程</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-if="authBtn(['1', '2', '3', '4'], scope.row.state)" v-hasPermi="['system:goods:remove']">删除</el-button>
          <el-button size="mini" type="text" v-if="authBtn(['2', '4'], scope.row.state)"
            @click="putShopOffShop(scope.row.id, true)" v-hasPermi="['system:goods:up']">上架</el-button>
          <el-button size="mini" type="text" v-if="authBtn(['3'], scope.row.state)"
            @click="putShopOffShop(scope.row.id)" v-hasPermi="['system:goods:down']">下架</el-button>
          <el-button size="mini" type="text" v-if="authBtn(['5'], scope.row.state)" @click="restGoods(scope.row)" 
            v-hasPermi="['system:goods:recover']">恢复
          </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" />
    <Form ref="Form" :getList="getList"/>
    <Specification ref="Specification"></Specification>
  </div>
</template>

<script>
import { listSpu, delSpu, updateSpu } from "@/api/system/goods";
import PutShopOffShop from './components/putShopOffShop.vue'
import Form from "./components/Form.vue";
import SelectCategory from "@/components/SelectCategory";
import SpecsSelect from '../spec/SpecsSelect.vue';
import Specification from './components/Specification.vue';
export default {
  name: "Spu",
  components: { Form, PutShopOffShop ,SelectCategory, SpecsSelect,Specification},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      spuList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        state: null,        
        category: null
      },
      options: [{
          value: '1',
          label: '已创建'
        }, {
          value: '2',
          label: '已完成编程'
        }, {
          value: '3',
          label: '已上架'
        }, {
          value: '4',
          label: '已下架'
        }, {
          value: '5',
          label: '已删除'
      }],
      categoryList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getPicsList(str){   
      let picObj = JSON.parse(str);  
      return picObj.thumbnail
    },
    getPicsBigList(str){
      let list=[];
      let picObj = JSON.parse(str);  
      list.push(picObj.thumbnail)
      return list;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listSpu(this.queryParams).then((response) => {
        this.spuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.Form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.Form.handleUpdate(row.id);
    },
    handleUpdateState(row){
      updateSpu({id:row.id,state:'2'}).then((response) => {
        if(response.code=="200"){
          this.$modal.msgSuccess("已完成编程成功!");
          this.getList();
        }        
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.state=="3"){
         this.$message.error("下架商品之后才可以删除!");
        return;
      }
      const ids = row.id || this.ids;
      this.$modal
        .confirm('本商品已在店铺中上架,删除商品会在店铺中下架,是否确认删除?')
        .then(function () {
          return delSpu(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/goods/export",
        {
          ...this.queryParams,
        },
        `spu_${new Date().getTime()}.xlsx`
      );
    },
    putShopOffShop(id, type) {
      if (type == 1) {
        this.$refs.putShopOffShop.putShopModal(id)
        return
      }
      this.$refs.putShopOffShop.offShopModal(id)


    },
    restGoods(data) {
      updateSpu({ ...data, isDeleted: 0, state: 2 }).then(res => {
        this.$modal.msgSuccess("恢复成功");
        this.getList()

      })
    },
    setGoodsConfig(prow){
       
      this.$refs.Specification.setSpecGoods(prow);
      
       
      this.$refs.Specification.showSpecial(true,prow)
       

      //this.$refs.Specification.initAll();

    }
  },
};
</script>
