<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-row :gutter="10">
              <el-col :span="24">
                <el-form-item label="数据类型" prop="type">
                  <el-select
                    v-model="ruleForm.type"
                    style="width: 100%"
                    :disabled="id == '007'"
                    placeholder="数据类型"
                    @change="typeChange"
                  >
                    <el-option label="面积1~面积2 ㎡" value="1" />
                    <el-option label="面积+ ㎡" value="2" />
                  </el-select> </el-form-item
              ></el-col>
              <el-col :span="ruleForm.type === '1' ? 13 : 24">
                <el-form-item label="会议面积" prop="min">
                  <el-input
                    v-model="ruleForm['min']"
                    :disabled="id == '007'"
                    placeholder="面积1"
                    max="999999"
                    min="0"
                    step="1"
                    type="number"
                  >
                    <template slot="append" v-if="ruleForm.type !== '1'">+</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="tc" v-if="ruleForm.type === '1'">
                <span style="height: 40px; line-height: 40px">~</span>
              </el-col>
              <el-col :span="9" v-if="ruleForm.type === '1'">
                <el-form-item label-width="0" prop="max">
                  <el-input
                    v-model="ruleForm['max']"
                    :disabled="id == '007'"
                    placeholder="面积2"
                    max="999999"
                    min="0"
                    step="1"
                    type="number"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </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/area";
import resToken from "@/mixins/resToken";

/**
 * @props
 * 名称 name
 * */
export default {
  name: "area",
  mixins: [resToken],
  props: ["showModel", "id", "sitem"],
  data() {
    return {
      loading: false,
      title: "添加会场面积",
      showModelThis: this.showModel,
      select: "1",
      activeOptions: [],
      actionList: [],
      ruleForm: {
        id: "",
        type: "1",
        min: "",
        max: "",
      },
      rulesThis: this.rules,
      rules: {
        type: [{ required: true, message: "请选择数据类型", trigger: "change" }],
        min: [{ required: true, message: "最小面积不能为空", trigger: "blur" }],
        max: [{ required: true, message: "最大面积不能为空", trigger: "blur" }],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      const basicTitle = "会场面积";
      let prefix = this.id === "005" ? "修改" : this.id === "002" ? "添加" : "";
      this.title = prefix ? prefix + basicTitle : basicTitle + "详情";
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { id, min, max } = this.sitem;
          const isok = String(max) === "0";
          this.ruleForm = {
            id: id || "",
            type: isok ? "2" : "1",
            min: min || "",
            max: max || "",
          };
        }
      });
    },
    typeChange() {
      const { type } = this.ruleForm;
      this.rulesThis.max[0].required = type === "1";
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            const { min, max } = this.ruleForm;
            let model = JSON.parse(JSON.stringify(this.ruleForm));
            if (model.type === "2") {
              model.max = "";
            } else {
              if (Number(min) >= Number(max)) {
                this.$message.warning("最大面积需大于最小面积！");
                this.loading = false;
                return;
              }
            }
            let res = {};
            if (this.id === "002") {
              delete model["id"];
              res = await asyncRequest.add(model);
            } else {
              res = await asyncRequest.update(model);
            }
            this.loading = false;
            if (res && res.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>
.area {
}
</style>
