<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="500px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="预付类型" prop="name">
              <el-select
                style="width: 100%"
                v-model="ruleForm['name']"
                :disabled="id == '007' || id == '005'"
                placeholder="预付类型"
                maxlength="20"
              >
                <el-option
                  v-for="p in prepaidList"
                  :key="p.value"
                  :label="p.label"
                  :value="p.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="支付比例" prop="ratio">
              <digital-input
                :values="ruleForm.ratio"
                :placeholder="'支付比例'"
                :min="0"
                :max="100"
                :position="'right'"
                size="default"
                :precision="2"
                :controls="false"
                :append="'%'"
                @reschange="rate_change($event, 'ratio')"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="id !== '007'" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            id == "007" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/serviceParam/advRatio";
import resToken from "@/mixins/resToken";
import { prepaidList } from "./columns";

/**
 * @props
 * ratio //支付比例
 * name //预付类型
 */

export default {
  name: "Brand",
  mixins: [resToken],
  props: ["showModel", "id", "sitem"],
  data() {
    return {
      prepaidList,
      loading: false,
      title: "添加预付比例",
      showModelThis: this.showModel,
      select: "1",
      activeOptions: [],
      actionList: [],
      ruleForm: {
        id: "",
        ratio: "",
        name: "",
      },
      rulesThis: this.rules,
      rules: {
        name: [{ required: true, message: "请选择预付类型", trigger: "blur" }],
        ratio: [{ required: true, message: "请输入支付比例", trigger: "blur" }],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    rate_change(e, key) {
      this.ruleForm[key] = e + "";
      this.$refs.ruleForm.validateField(key);
    },
    async initForm() {
      this.loading = true;
      const basicTitle = "预付比例";
      let prefix = this.id === "005" ? "修改" : this.id === "2" ? "添加" : "";
      this.title = prefix ? prefix + basicTitle : basicTitle + "详情";
      await this.resetForm();
      console.log(this.ruleForm);
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { id, ratio, name } = this.sitem;
          this.ruleForm = {
            id: id || "",
            ratio: ratio || "",
            name: name || "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            const model = JSON.parse(JSON.stringify(this.ruleForm));
            let res = {};
            if (this.id === "002") {
              delete model["id"];
              res = await asyncRequest.add(model);
            } else {
              res = await asyncRequest.update(model);
            }
            this.loading = false;
            const { code } = res;
            if (code === 1) {
              const title = this.id === "002" ? "添加成功!" : "修改成功!";
              this.$notify.success({
                title,
                message: "",
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh");
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.brand {
}
</style>
