<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="25vh"
    width="600px"
    :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)"
    append-to-body
    @close="closeModel"
  >
    <el-card style="margin-top: -20px">
      <el-row>
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            is_show-icon
            :rules="rulesThis"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="分类名称：" prop="className">
              <el-input
                v-model="ruleForm.className"
                :disabled="isDetail"
                placeholder="请输入分类名称"
              />
            </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/purchaseIn/goodsClass";
import btnList from "@/assets/js/btnList";
import resToken from "@/mixins/resToken";
export default {
  mixins: [resToken],
  props: [
    "showModel",
    "id",
    "isDetail",
    "pid",
    "level",
    "isAdd",
    "type",
    "formData",
    "goodsName",
  ],
  data() {
    return {
      loading: false,
      showIconModel: false,
      title: "",
      btnList,
      showModelThis: this.showModel,
      ruleForm: {
        className: "", //分类名称
      },
      rulesThis: this.rules,
      rules: {
        className: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      if (this.isAdd) {
        this.title = "添加商品分类";
        this.loading = false;
        this.rulesThis = this.rules;
      } else {
        if (this.isDetail) {
          this.title = "商品分类详情";
          this.rulesThis = {};
        } else {
          this.title = "修改商品分类";
          this.rulesThis = this.rules;
        }
      }
      await this.resetForm();
    },

    async resetForm() {
      this.ruleForm = {
        id: this.id,
        pid: this.pid,
        className: this.goodsName || "", // 名称
      };
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          // 去掉验证信息
          this.$refs.ruleForm.clearValidate();
        }
      });
    },
    async submitForm() {
      if (this.loading) {
        return false;
      }
      this.loading = true;

      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = {};
          let res = {};
          const { className } = this.ruleForm;

          model = {
            id: this.id,
            pid: this.pid,
            cat_name: className,
          };
          if (this.level === 1) {
            // delete model["pid"];
            // delete model["menuid"];
            // delete model["url"];
            // delete model["action_code"];
            // delete model["status"];
          } else if (this.level === 2) {
            delete model["menuid"];
            delete model["action_code"];
            delete model["status"];
          } else {
            delete model["pid"];
            delete model["img"];
            delete model["is_show"];
            delete model["weight"];
            delete model["route"];
            delete model["is_display"];
            delete model["url"];
          }
          if (this.isAdd) {
            delete model["id"];
          }
          if (this.isAdd) {
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
            // if (this.isAdd) {
            //   res = await asyncRequest.actionadd(model);
            // } else {
            //   res = await asyncRequest.actionedit(model);
            // }
          }
          this.loading = false;
          if (res && res.code === 0) {
            this.ruleForm = {};
            const title = this.isAdd ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            let pids = this.pid;
            if (this.id === 0) {
              // 添加组织
              pids = "0";
            }
            this.$emit("refresh", this.id, pids, this.level);
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          this.loading = false;
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.g-block {
  display: inline-block;
  padding: 10px 5px;
  text-align: center;
  border: 1px #ccc dashed;
  margin: 1px;
  font-size: 0px;
  cursor: pointer;
  i {
    font-size: 30px;
    line-height: 30px;
    display: block;
    margin: 0 auto;
    width: 30px;
  }
}
</style>
