<template>
  <div class="app-container">
    <h3>开启设置</h3>
    <el-form ref="form" :model="form" label-width="200px" style="margin-top: 20px">
      <el-row>
        <el-col :span="12">
          <div style="display: flex; flex-direction: row">
            <el-form-item label="店铺是否开启代金券使用：">
              <el-switch
                v-model="enableVoucher"
                active-text="开启"
                inactive-text="关闭"
                :active-value="1"
                :inactive-value="0"
                @change="submitConfig"
              >
              </el-switch>
            </el-form-item>
            <!-- <el-form-item label="折扣卷类型">
              <el-radio-group v-model="useVoucherType">
                <el-radio :label="0" :value="0">普通满减券</el-radio>
                <el-radio :label="1" :value="1">折扣卷</el-radio>
              </el-radio-group>
            </el-form-item>
            <div style="margin-left: 20px; margin-top: 5px">
              <el-button
                v-hasPermi="['voucher:switch']"
                type="primary"
                size="small"
                style="height: 30px"
                @click="submitConfig"
                >确认</el-button
              >
            </div> -->
          </div>
        </el-col>
      </el-row>
    </el-form>
    <h3>规则配置</h3>
    <el-radio-group v-model="queryParams.useVoucherType" @change="radioChang">
      <el-radio :label="0" value="0">普通满减券</el-radio>
      <!-- <el-radio :label="1" value="1">折扣卷</el-radio> -->
    </el-radio-group>

    <div style="margin-top: 20px">
      <el-button type="primary" size="small" @click="addRole" v-hasPermi="['voucher:add']"
        >新增规则</el-button
      >
    </div>
    <template v-if="queryParams.useVoucherType == 0">
      <el-col :span="24" v-if="this.form.length < 1">
        <el-result icon="info" title="暂无规则" subTitle="请新增代金券规则"> </el-result>
      </el-col>
      <template v-else>
        <el-form ref="form" :model="form" label-width="140px" style="margin-top: 20px">
          <el-row>
            <el-col :span="24" v-for="(item, index) in form" :key="index">
              <el-form-item label="满">
                <div>
                  <el-input
                    v-model="item.min"
                    placeholder="请输入金额"
                    style="width: 140px"
                    min="1"
                  >
                    <span slot="suffix">元 </span>
                  </el-input>
                  --
                  <el-input
                    v-model="item.max"
                    placeholder="请输入金额"
                    style="width: 140px"
                    :min="1"
                  >
                    <span slot="suffix">元 </span>
                  </el-input>

                  可抵扣
                  <el-input
                    v-model="item.amount"
                    placeholder="请输入金额"
                    style="width: 200px"
                    :min="1"
                  >
                    <span slot="suffix">元 </span>
                  </el-input>
                  <i
                    class="el-icon-delete"
                    style="color: red; margin-left: 20px; font-size: 18px"
                    @click="removeRule(item, index)"
                  ></i>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div style="text-align: center; margin-top: 20px">
          <el-button type="primary" @click="InviteSubmit" v-hasPermi="['voucher:edit']"
            >确 定</el-button
          >
        </div>
      </template>
    </template>
    <template v-if="queryParams.useVoucherType == 1">
      <el-col :span="24" v-if="person.length < 1">
        <el-result icon="info" title="暂无规则" subTitle="请新增代金券规则"> </el-result>
      </el-col>
      <template v-else>
        <el-form ref="form" :model="person" label-width="140px" style="margin-top: 20px">
          <el-row>
            <el-col :span="24" v-for="(item, index) in person" :key="index">
              <el-form-item label="满">
                <div>
                  <el-input
                    v-model="item.min"
                    placeholder="请输入金额"
                    style="width: 140px"
                  >
                    <span slot="suffix">元 </span>
                  </el-input>
                  --
                  <el-input
                    v-model="item.max"
                    placeholder="请输入金额"
                    style="width: 140px"
                  >
                    <span slot="suffix">元 </span>
                  </el-input>

                  打

                  <el-input-number v-model="item.ratio" :min="0.5" :step="0.5" :max="10">
                  </el-input-number>
                  <span>折 </span>
                  <i
                    class="el-icon-delete"
                    style="color: red; margin-left: 20px; font-size: 18px"
                    @click="removeRule(item, index)"
                  ></i>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div style="text-align: center; margin-top: 20px">
          <el-button type="primary" @click="InviteSubmit" v-hasPermi="['voucher:edit']"
            >确 定</el-button
          >
        </div>
      </template>
    </template>
  </div>
</template>

<script>
import {
  voucherCreate,
  voucherList,
  voucherEdit,
  voucherRemove,
  voucherEnable,
} from "@/api/system/voucher";
import { getStoreInfo } from "@/api/common/index";
import axios from "axios";

export default {
  name: "User",
  data() {
    return {
      // 表单参数
      form: [],
      person: [],
      queryParams: {
        subType: 2,
        type: 1,
        useVoucherType: 0,
      },
      enableVoucher: "",
      useVoucherType: 0,
    };
  },
  mounted() {
    this.getvoucherList();
    this.getInfo();
  },
  methods: {
    getInfo() {
      getStoreInfo().then((res) => {
        if (res.code == 200) {
          this.enableVoucher = res.data.enableVoucher;
          this.useVoucherType = res.data.useVoucherType;
        }
      });
    },
    submitConfig() {
      let params = {
        enableVoucher: this.enableVoucher,
        useVoucherType: this.queryParams.useVoucherType,
      };
      voucherEnable(params).then((res) => {
        if (res.code == 200) {
          this.$modal.msgSuccess("操作成功");
        }
      });
    },

    radioChang() {
      this.getvoucherList();
    },
    removeRule(record, index) {
      if (record.id) {
        voucherRemove(record.id).then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess("删除成功！");
            this.getvoucherList();
          }
        });
      } else {
        if (this.queryParams.useVoucherType == 0) {
          this.form.splice(index, 1);
        } else {
          this.person.splice(index, 1);
        }
      }
    },
    addRole() {
      if (this.queryParams.useVoucherType == 0) {
        this.form.push({
          amount: "",
          max: "",
          min: "",
          status: 0,
          subType: 2,
          type: 1,
          useVoucherType: 0,
        });
      } else if (this.queryParams.useVoucherType == 1) {
        this.person.push({
          ratio: 10,
          max: "",
          min: "",
          status: 0,
          subType: 2,
          type: 1,
          useVoucherType: 1,
        });
      }
    },
    handleClick() {
      this.getvoucherList();
    },
    getvoucherList() {
      voucherList(this.queryParams).then((res) => {
        if (res.code == 200) {
          if (this.queryParams.useVoucherType == 1) {
            this.person = res.data;
          } else {
            this.form = res.data;
          }
        }
      });
    },
    InviteSubmit() {
      let params = null;
      if (this.queryParams.useVoucherType == 0) {
        params = this.form;
      } else {
        params = this.person;
        params.forEach((e) => {
          e.ratio = e.ratio / 10;
        });
      }
      voucherEdit(params).then((res) => {
        if (res.code == 200) {
          this.$modal.msgSuccess("保存成功！");
        }
      });

      setTimeout(() => {
        this.getvoucherList();
      }, 500);
    },
    personSubmit() {
      if (this.person.id) {
        voucherEdit([this.person]).then((res) => {
          this.$modal.msgSuccess("修改成功！");
          this.getvoucherList();
          console.log("res", res);
        });
      } else {
        voucherCreate([this.person]).then((res) => {
          this.$modal.msgSuccess("新增成功！");
          this.getvoucherList();
          console.log("res", res);
        });
      }
    },
  },
};
</script>

<style>
.hide > .el-upload--picture-card {
  display: none;
}
</style>
<style lang="scss" scoped>
.detail_Header {
  margin-bottom: 35px;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  .item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .count {
      font-weight: bold;
      font-size: 18px;
    }
  }
}
</style>
