<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
            :size="'small'"
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="起订量" prop="s1">
              <el-input
                placeholder="起订量"
                v-model="ruleForm.s1"
                type="number"
                :min="0"
                :max="9999999999"
                :step="0"
              />
            </el-form-item>
            <el-form-item label="成本裸价" prop="s2">
              <el-input
                placeholder="成本裸价"
                v-model="ruleForm.s2"
                type="number"
                :min="0"
                :max="9999999999"
                :step="2"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="成本合计" prop="s2">
              <el-input
                placeholder="成本合计"
                v-model="ruleForm.s2"
                type="number"
                :min="0"
                :max="9999999999"
                :step="2"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="其中运费" prop="s2">
              <el-input
                placeholder="其中运费"
                v-model="ruleForm.s2"
                type="number"
                :min="0"
                :max="9999999999"
                :step="2"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="使用状态" prop="s5">
              <el-select
                v-model="ruleForm.s5"
                style="width: 100%"
                placeholder="使用状态"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right; padding-top: 18px">
          <el-button
            v-if="id !== 'edit'"
            :size="'small'"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false" :size="'small'">{{
            id == "edit" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/goodStore/active";
import resToken from "@/mixins/resToken";
export default {
  name: "active",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "",
      showModelThis: this.showModel,
      options: [
        { value: "1", label: "启用" },
        { value: "0", label: "禁用" },
      ],
      ruleForm: {},
      rulesThis: this.rules,
      rules: {
        s1: [
          { required: true, message: "起订量应为合法数字", trigger: "blur" },
        ],
        s2: [
          { required: true, message: "成本裸价应为合法数字", trigger: "blur" },
        ],
        s3: [
          { required: true, message: "成本合计应为合法数字", trigger: "blur" },
        ],
        s4: [
          { required: true, message: "其中运费应为合法数字", trigger: "blur" },
        ],
        s5: [
          { required: true, message: "请选择使用状态", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.resetFormData();
      if (this.id === "add") {
        this.title = "添加成本阶梯";

        this.rulesThis = this.rules;
      } else if (this.id === "005") {
        this.title = "修改成本阶梯";
        this.rulesThis = this.rules;
      } else {
        this.title = "成本阶梯详情";
        this.rulesThis = {};
      }
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.resetFormData();
        }
      });
    },
    resetFormData() {
      const { id, s1, s2, s3, s4, s5 } = this.sitem;
      this.ruleForm = {
        id: id || "",
        s1: s1 || "",
        s2: s2 || "",
        s3: s3 || "",
        s4: s4 || "",
        s5: s5 || "0",
      };
    },
    s1searchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.s1 = code;
      } else {
        this.ruleForm.s1 = "";
      }
      this.$refs.ruleForm.validateField("s1");
      this.getlist();
    },
    async getlist() {
      const { s1 } = this.ruleForm;
      const { code, data, message } = await asyncRequest.list({
        s1: s1,
      });
      if (code === 0) {
        this.options = data;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "003") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功!" : "修改成功!";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async submitForm1() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "003") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功!" : "修改成功!";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

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