<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="15vh"
    width="600px"
    @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="80px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="24">
                <el-form-item label="异常数量" prop="error_num">
                  <el-input
                    v-model="ruleForm.error_num"
                    placeholder="请输入异常数量"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="异常原因" prop="error_code">
                  <abnormal-cause
                    :value="ruleForm.error_code"
                    :placeholder="'请选择异常原因'"
                    :disabled="false"
                    @searchChange="select_change"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="异常备注" prop="error_remark">
                  <el-input
                    type="textarea"
                    :rows="3"
                    placeholder="请输入异常备注"
                    v-model="ruleForm.error_remark"
                    maxlength="250"
                    show-word-limit
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" @click="submitForm">保 存 </el-button>
          <el-button @click="showModelThis = false">关 闭</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
   <script>
import resToken from "@/mixins/resToken";
import { isnumber } from "@/utils/validate";
export default {
  name: "returnOrder",
  props: ["showModel", "index", "sitem"],
  mixins: [resToken],
  data() {
    const validateerror_num = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("异常数量不能为空！"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("异常数量必须为数字"));
        } else {
          callback();
        }
      }
    };
    return {
      loading: false,
      title: "",
      showModelThis: this.showModel,
      ruleForm: {
        error_num: "", //异常数量
        error_code: "", //异常原因
        error_remark: "", //异常备注
        error_code_name: "",
        id: "",
      },
      rulesThis: this.rules,
      rules: {
        error_num: [
          {
            required: true,
            validator: validateerror_num,
            trigger: "blur",
          },
        ],
        error_code: [
          {
            required: true,
            message: "请输入异常原因",
            trigger: "change",
          },
        ],
        error_remark: [
          {
            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;
      this.rulesThis = this.rules;
      if (this.index === -1) {
        this.title = "添加异常情况";
      } else {
        this.title = "编辑异常情况";
      }
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { error_num, error_code, error_remark, error_code_name, id } =
            this.sitem;
          this.ruleForm = {
            id: id || "",
            error_num: error_num || "", //异常数量
            error_code: error_code || "", //异常原因
            error_remark: error_remark || "", //异常备注
            error_code_name: error_code_name || "",
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.showModelThis = false;
          this.$emit("refresh", {
            index: this.index,
            item: JSON.parse(JSON.stringify(this.ruleForm)),
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    select_change(e) {
      this.ruleForm.error_code = e && e.code ? e.code : "";
      this.ruleForm.error_code_name = e && e.code ? e.label : "";
      this.$refs.ruleForm.validateField("error_code_name");
      this.$refs.ruleForm.validateField("error_code");
    },
  },
};
</script>

   <style lang="scss" scoped>
</style>
   