<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    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-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="流程名称" prop="process_name">
              <el-input
                v-model="ruleForm.process_name"
                placeholder="请输入流程名称"
                maxlength="50"
                :disabled="isDetail"
              />
            </el-form-item>
            <el-form-item label="流程值" prop="process_type">
              <el-input
                v-model="ruleForm.process_type"
                placeholder="请输入流程值"
                maxlength="50"
                :disabled="isDetail"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="!isDetail" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/serviceParam/setProcess";
import resToken from "@/mixins/resToken";
export default {
  name: "setProcess",
  props: ["showModel", "id", "isDetail"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加流程",
      showModelThis: this.showModel,
      ruleForm: {
        id: this.id,
        process_name: "",
        process_type: "",
        remark: "",
      },
      rulesThis: this.rules,
      rules: {
        process_name: [
          {
            required: true,
            message: "请输入流程名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        process_type: [
          {
            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;
      if (this.id === "add") {
        this.title = "添加流程";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail) {
          this.title = "流程详情";
          this.rulesThis = {};
        } else {
          this.title = "修改流程";
          this.rulesThis = this.rules;
        }
        await this.resetForm();
        await this.initData(this.id);
      }
      this.loading = false;
    },
    async initData(id) {
      const { code, data, message } = await asyncRequest.detail({ id });
      if (code === 0) {
        const { id, process_name, process_type } = data;
        this.ruleForm = {
          id,
          process_name,
          process_type,
        };
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            id: this.id,
            process_name: "",
            process_type: "",
            remark: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) return;
          this.loading = true;
          const obj = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "add") {
            delete obj["id"];
            res = await asyncRequest.add(obj);
          } else {
            res = await asyncRequest.update(obj);
          }
          this.loading = false;
          const { code, data, message } = res;
          if (code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.setProcess {
  .setWidth {
    width: 100% !important;
    // display: flex;
    // display: block !important;
    // .el-input__inner {
    //   display: flex;
    //   width: 1000px !important;
    //   display: block !important;
    // }
  }
}
</style>
