<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="15vh"
    width="700px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card style="margin:-20px 0 0 0;">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            ref="ruleForm"
            label-width="140px"
            class="demo-ruleForm"
          >
            <el-form-item
              label="退货验收异常原因"
              prop="result"
              v-if="id === 'add' || (isDetail ? true : true)"
            >
              <el-input
                v-model="ruleForm.result"
                :disabled="isDetail"
              ></el-input>
            </el-form-item>

            <el-form-item
              label="异常备注"
              prop="result_desc"
              v-if="id === 'add' || (isDetail ? true : true)"
            >
              <el-input
                :autosize="{ minRows: 4, maxRows: 10 }"
                type="textarea"
                v-model="ruleForm.result_desc"
                :disabled="isDetail"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" @click="submitForm" v-if="!isDetail"
            >保 存
          </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/goodsAnomaly";
import resToken from "@/mixins/resToken";
export default {
  name: "goodsAnomaly",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "",
      showModelThis: this.showModel,
      ruleForm: {
        result: "", //入库验收异常原因
        result_desc: "", //入库异常备注
      },
      rulesThis: this.rules,
      rules: {
        result: [
          {
            required: true,
            trigger: "blur",
            message: "退货验收异常原因不能为空",
          },
        ],

        result_desc: [
          {
            required: true,
            trigger: "blur",
            message: "备注不能为空",
          },
        ],
      },
    };
  },
  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.loading = false;
    },
    // 获取详情列表
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        this.ruleForm = res.data;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            result: "", //入库验收异常原因
            result_desc: "", //入库异常备注
          };
        }
      });
    },

    // 提交功能
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { result, result_desc } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            result: result || "", //入库验收异常原因
            result_desc: result_desc || "", //入库异常备注
            type:"3",
            id: this.id,
          };
          let res = {};
          if (this.id === "add") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            delete model["type"];
            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>
.storeAnomaly {
}
</style>
   