<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item prop="phonenumber">
          <el-select
            size="small"
            v-model="queryParams.productTypeName"
            style="margin-right:8px;"
            clearable
            placeholder="商品类型">
            <el-option
              v-for="(item,index) in productTypelist"
              :key="index"
              :label="item.typeName"
              :value="item.guid"></el-option>
          </el-select>
          <el-select
            size="small"
            style="margin-right:8px;"
            v-model="queryParams.isSale"
            clearable
            placeholder="商品状态">
            <el-option
              v-for="(item,index) in productStatus"
              :key="index"
              :label="item.lable"
              :value="item.id"></el-option>
          </el-select>
          <el-input
            v-model="queryParams.goodsName"
            placeholder="请输入类别名称"
            clearable
            size="small"
            style="width: 240px;margin-right:8px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</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"
            icon="el-icon-plus"
            size="mini"
            @click="handelProductAction(1)"
          >上架</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handelProductAction(0)"
          >下架</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="商品首图" align="center" prop="typeName" width="160">
          <template slot-scope="scope">
            <el-image
                style="width:50px; height:50px"
                :src="imgRequest + scope.row.goodsImages.split(';')[0] + '?imageView2/1/w/200/h/200'"
                :preview-src-list="scope.row.goodsImages.split(';').map(item => imgRequest + item + '?imageView2/1/w/400/h/400')">
              </el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="商品类别" align="center" prop="productTypeName" />
        <el-table-column label="售出量" align="center" prop="saledCarbonSkin" />
        <el-table-column label="剩余量" align="center" prop="carbonVal" />
        <el-table-column label="认购期" align="center" prop="goodsSaleDate" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.goodsSaleDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建日期" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否上架" align="center" prop="isSale" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.isSale == 1 ? '已上架' : '已下架'}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="120"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              icon="el-icon-delete"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  </div>
</template>

<script>
import { listProduct ,listProductType , delProduct ,productAc} from "@/api/productManage/commodity";
export default {
  name: "commodity",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: null,
      // 商品类型
      productTypelist: [],
      productStatus:[
        {
          lable:'下架',
          id:'0'
        },
        {
          lable:'上架',
          id:'1'
        }
      ],
      // 表单参数
      form: {},
      //表格选中列表
      checkedList:[],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      }
    };
  },
  created() {
    this.initPages();
    this.getList();
  },
  methods: {
    initPages(){
       //获取产品类型
       let typeParams = {
         state: 1
       }
       listProductType(typeParams).then(res => {
           if(res.retHead.errCode == 0){
             this.productTypelist = res.retBody
           }
       });
    },
    /** 查询产婆类别列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(res => {
          if(res.retHead.errCode == 0){
            this.list = res.retBody
            this.total = res.retHead.total;
          }
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.isSale = null
      this.queryParams.productTypeName = null
      this.queryParams.goodsName = ""
      this.handleQuery();
    },
    /** 多选**/
    handleSelectionChange(val){
       this.checkedList = val;
    },
    /*商品状态操作*/
    handelProductAction(status){
      if(this.checkedList.length != 0){
        this.checkedList.map(item => item.isSale = status)
        productAc(this.checkedList).then(res => {
            this.msgSuccess(res.retHead.errMsg)
            this.getList();
          }
        );
      }else{
        this.msgError("请先选择你想要操作的商品!")
      }
      console.log(status)
    },
    /** 删除按钮操作 */
    handleDelete(item) {
      if(item.isSale == 1){
        this.msgError("上架的商品不能删除，若要删除，请先下架商品!")
        return;
      }else{
        let params = item
        this.$confirm('是否确认删除该数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delProduct(params);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(function() {});
      }
    }
  }
};
</script>
