<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    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="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="原因类型" prop="result">
              <el-select
                v-model="ruleForm.type"
                placeholder="原因类型"
                style="width: 100%"
                :disabled="!(id === 'add')"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="原因标题" prop="result">
              <el-input
                v-model="ruleForm.result"
                :disabled="isDetail"
                placeholder="原因标题"
                maxlength="50"
                show-word-limit
              ></el-input>
            </el-form-item>

            <el-form-item label="原因备注" prop="result_desc">
              <el-input
                :autosize="{ minRows: 4, maxRows: 10 }"
                type="textarea"
                placeholder="原因备注"
                maxlength="250"
                show-word-limit
                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/unusualReason";
import resToken from "@/mixins/resToken";
export default {
  name: "unusualReason",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "",
      showModelThis: this.showModel,
      ruleForm: {
        type: "1",
        result: "", //入库验收异常原因
        result_desc: "", //入库异常备注
      },
      rulesThis: this.rules,
      options: [
        { id: "1", label: "入库验收异常" },
        { id: "2", label: "退货验收异常" },
        { id: "3", label: "售后已收到货异常" },
        { id: "4", label: "采购退货" },
        { id: "5", label: "销售退换货" },
        { id: "6", label: "售后未收到货异常" },
        { id: "7", label: "议价申请原因" },
        { id: "8", label: "商品下线原因" },
      ],

      rules: {
        type: [
          {
            required: true,
            trigger: "change",
            message: "请选择原因类型",
          },
        ],
        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: "", //入库异常备注
            type: "1",
          };
        }
      });
    },

    // 提交功能
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid);
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const { result, result_desc, type } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            result: result || "", //入库验收异常原因
            result_desc: result_desc || "", //入库异常备注
            type: type || "1",
            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);
            console.log(res);
          }
          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>
.unusualReason {
}
</style>
   