<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="商品名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入商品名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" 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-col :span="1.5">
      <el-button
        type="danger"
        :disabled="multipleSelection.length < 1"
        plain
        size="mini"
        @click="handleRemoves"
        >批量删除</el-button
      >
    </el-col>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
      row-key="goodsDraftId"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.pic"
            :preview-src-list="[scope.row.pic]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center">
        <template slot-scope="scope">
          {{ scope.row.categoryName }}
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center">
        <template slot-scope="scope">
          {{ scope.row.skuList ? scope.row.skuList[0].skuPriceList[0].price : "" }}/{{
            scope.row.unit
          }}
        </template>
      </el-table-column>

      <el-table-column label="起批量" align="center">
        <template slot-scope="scope">
          {{ scope.row.skuList ? scope.row.skuList[0].skuPriceList[0].minPurchase : ""
          }}{{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center">
        <template slot-scope="scope">
          {{ scope.row.skuList ? scope.row.skuList[0].stock + "/" + scope.row.unit : "" }}
        </template>
      </el-table-column>
      <el-table-column label="经营区域" align="center">
        <template slot-scope="scope">
          {{ scope.row.scope == 0 ? "全国" : scope.row.scope == 1 ? "同城" : "特价" }}
        </template>
      </el-table-column>
      <el-table-column label="销售方式" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.scope == 0">
            {{ scope.row.saleType == 1 ? "现货" : "预售" }}
          </span>
          <span v-else> </span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:order:detail']"
            >编辑商品</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: red"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:order:detail']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  deleteGoodsDraft,
  getGoodsDraftByUserId,
  deleteGoodsDrafts,
} from "@/api/manage/product";
import { getStoreInfo } from "@/api/common/index";
export default {
  name: "product",
  data() {
    return {
      // 遮罩层
      loading: false,
      configList: [],
      // 查询参数
      multipleSelection: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        title: "",
        spuType: 0,
      },
      // 表单参数
      form: {},
      shopInfo: {},
    };
  },
  mounted() {
    this.getList();
    this.getInfo();
  },
  methods: {
    getInfo() {
      getStoreInfo().then((res) => {
        if (res.code == 200) {
          this.shopInfo = res.data;
        }
      });
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      this.configList = [];
      getGoodsDraftByUserId(this.queryParams).then((res) => {
        for (let index = 0; index < res.data.length; index++) {
          res.data[index] = res.data[index].goodsDto;
          this.configList.push(res.data[index]);
        }
        // this.configList = res.data;
        console.log("1231231", this.configList);

        this.loading = false;
      });
    },
    handleDelete(record) {
      this.$confirm("是否确认删除该商品？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteGoodsDraft({
            id: record.goodsDraftId,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success(`删除成功！`);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
    handleRemoves() {
      this.$confirm("是否确认批量删除草稿箱？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = [];

          for (let index = 0; index < this.multipleSelection.length; index++) {
            console.log("xzcz", this.multipleSelection[index]);
            ids.push(this.multipleSelection[index].goodsDraftId);
          }
          deleteGoodsDrafts({
            ids: ids.toString(),
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success(`删除成功！`);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleUpdate(record) {
      let params = {
        id: record.goodsDraftId,
        type: "draft",
      };
      window.localStorage.setItem("productInfo", JSON.stringify(params));
      this.$router.push({
        path: "/product/product/updateProduct",
        query: { params },
      });
    },
  },
};
</script>
<style>
.el-form-item {
  margin-bottom: 10px;
}
</style>
