<template>
  <div id="mechanism">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <table-list
      rowKey="goodsId"
      ref="tableList"
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      :loading="loading"
      @addClick="addClick"
    >
      <template slot="customize">
        <el-button @click="remove"> 批量删除 </el-button>
        <el-button> 导出excel </el-button>
      </template>
      <template slot="about" slot-scope="props">
        <span
          v-for="(item, index) in arrayStr(props.scope.row.useType)"
          :key="index"
        >
          <span v-if="item == 1"> 积分商城 </span>
          <span v-if="item == 2"> 会员卡 </span>
        </span>
      </template>
      <template slot="images" slot-scope="props">
        <el-image
          style="width: 100px; height: 70px"
          :src="
            $methodsTools.splitImgHost(
              imgListComputer(props.scope.row.mainImg)[0]
            )
          "
          :preview-src-list="
            imgListComputer(props.scope.row.mainImg).map((i) =>
              $methodsTools.splitImgHost(i)
            )
          "
        >
        </el-image>
      </template>
      <template slot="status" slot-scope="props">
        <el-switch
          v-model="props.scope.row.goodsStatus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          @change="statusChange($event, props.scope.row)"
        >
        </el-switch>
      </template>
      <template slot="rule" slot-scope="props">
        {{
          parseInt(
            parseFloat(props.scope.row.standPrice) -
              parseFloat(props.scope.row.specialPrice)
          ) * getRules
        }}
      </template>
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row)"
          >编辑</el-button
        ><el-button type="text" @click="del(props.scope.row)">删除</el-button>
        <el-button type="text" @click="record(props.scope.row)">记录</el-button>
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize.sync="formData.pageSize"
      :currentPage.sync="formData.pageNum"
      @search="search"
    />
    <dislog
      :dialogVisible.sync="dialogVisible"
      @search="search"
      :goodsId="goodsId"
      :getRules="getRules"
    ></dislog>
    <record
      :dialogVisible.sync="dialogRecordVisible"
      :goodsId="goodsId"
    ></record>
  </div>
</template>

<script>
import dislog from "./dislog.vue";
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
import record from "./record.vue";
export default {
  name: "GiftGoods",
  components: { record, searchBoxNew, tableList, pagination, dislog },
  data() {
    return {
      loading: false,
      navText: {
        title: "礼品商品",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: false,
        custom: false,
        openCheckMore: true,
      },
      formList: [
        {
          prop: "merId",
          placeholder: "请选择合作商户",
          scope: "merList",
        },
        {
          prop: "storeId",
          placeholder: "请选择商户店铺",
          scope: "storeList",
        },
        {
          prop: "useType",
          placeholder: "关联使用",
          scope: "select",
          options: [
            {
              label: "积分商城",
              value: 1,
            },
            {
              label: "会员卡",
              value: 2,
            },
          ],
        },
        {
          prop: "goodsName",
          placeholder: "请输入商品名称",
        },
      ],
      formData: {},
      tableSet: [
        {
          label: "合作商户",
          prop: "merName",
          hidden: true,
        },
        {
          label: "关联使用",
          prop: "useType",
          hidden: true,
          scope: "solt",
          soltName: "about",
        },
        {
          label: "商品名称",
          prop: "goodsName",
          hidden: true,
        },
        {
          label: "商品主图",
          prop: "mainImg",
          hidden: true,
          scope: "solt",
          soltName: "images",
        },
        {
          label: "兑换积分",
          hidden: true,
          scope: "solt",
          soltName: "rule",
        },
        {
          label: "商品原价",
          prop: "standPrice",
          hidden: true,
        },
        {
          label: "会员优惠价",
          prop: "specialPrice",
          hidden: true,
        },
        {
          label: "上架状态",
          prop: "goodsStatus",
          hidden: true,
          scope: "solt",
          soltName: "status",
        },
        {
          label: "录入时间",
          prop: "createTime",
          hidden: true,
          scope: "aTimeList",
        },
      ],
      tableData: [],
      total: 0,
      dialogVisible: false,
      dialogRecordVisible: false,
      goodsId: null,
      getRules: 0,
    };
  },
  computed: {
    imgListComputer: function () {
      return function (str) {
        return str?.split(",") || [];
      };
    },
    arrayStr: function () {
      return function (str) {
        return str?.split(",") || [];
      };
    },
  },
  created() {
    this.$api.mallstoreconfigKeypointsRule().then((res) => {
      this.getRules = res.data.oneExchangeValue;
    });
    this.search(2);
  },
  methods: {
    record(row) {
      this.goodsId = row.goodsId;
      this.dialogRecordVisible = true;
    },
    statusChange(e, row) {
      this.$api
        .editgoodspoints({
          goodsId: row.goodsId,
          goodsStatus: e,
          status: row.status,
          goodsType: row.goodsType,
        })
        .then((res) => {
          this.$message.success("操作成功");
          row.goodsStatus = e;
        })
        .catch(() => {
          return (row.goodsStatus = e ? 0 : 1);
        });
    },
    addClick(data) {
      this.goodsId = data?.goodsId || 0;
      this.dialogVisible = true;
    },
    search(v) {
      this.loading = true;
      if (v === 2) {
        this.formData = {
          pageSize: 10,
          pageNum: 1,
        };
        this.$nextTick(() => {
          this.$refs.tableList.clearMoreActive();
        });
      }
      if (v === 3) {
        this.$nextTick(() => {
          this.$refs.tableList.clearMoreActive();
        });
      }
      this.$api
        .goodslistPoints(this.formData)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
    remove() {
      if (this.$refs.tableList.allCheckData.length === 0) {
        this.$message.error("请勾选数据");
        return;
      }
      this.$confirm("确定批量删除选中的数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .editgoodsbatchRemove({
              goodsIds: this.$refs.tableList.allCheckData.map((i) => i.goodsId),
            })
            .then(() => {
              this.$message.success("批量删除成功");
              this.search(3);
            });
        })
        .catch(() => {});
    },
    del(v) {
      this.$confirm("确定删除此内容？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .editgoodsbatchRemove({
              goodsIds: [v.goodsId],
            })
            .then(() => {
              this.$message.success("删除成功");
              this.search(3);
            });
        })
        .catch(() => {});
    },
  },
};
</script>

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