<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-form-item label="退货数量" prop="num">
              <el-input
                maxlength="250"
                placeholder="退货数量"
                v-model="ruleForm.num"
              />
            </el-form-item>
            <el-form-item label="退货原因" prop="errorCode">
              <el-select
                style="width: 100%"
                v-model="ruleForm.errorCode"
                placeholder="退换货原因"
              >
                <el-option
                  v-for="item in reason_options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="退货备注" prop="remark">
              <el-input
                type="textarea"
                maxlength="250"
                show-word-limit
                placeholder="请填写备注"
                v-model="ruleForm.remark"
              />
            </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/sellOut/zxReturn";
import resToken from "@/mixins/resToken";
import { isnumber } from "@/utils/validate";
export default {
  name: "zxReturn",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  data() {
    const validateWeight = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("退货数量不能为空!"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("退货数量仅支持整数!"));
        } else {
          callback();
        }
      }
    };
    return {
      noSendNumbers: "", //收货-未发货数量
      noSendNum: "", //销售-未发货数量
      reason_options: [], //退换货原因备选项
      loading: false,
      title: "添加销售退货单",
      showModelThis: this.showModel,
      ruleForm: {
        num: "",
        outCode: "", //发货申请单编号
        remark: "", //退货备注
        num: "", //退货数量
        errorCode: "", //退货原因
      },
      rulesThis: this.rules,
      rules: {
        errorCode: [
          {
            required: true,
            message: "请选择退货原因",
            trigger: "change",
          },
        ],
        num: [
          {
            required: true,
            validator: validateWeight,
            trigger: "blur",
          },
        ],
        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.title = "添加销售退货单";
      this.getReason(); //获取退还货原因
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.outCode = this.sitem.orderCode;
          this.ruleForm = {
            orderCode: this.outCode,
            remark: "", //退货备注
            num: "", //退货数量
            errorCode: "", //退货原因
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const model = JSON.parse(JSON.stringify(this.ruleForm));
          const res = await asyncRequest.add(model);
          this.loading = false;
          if (res && res.code === 0) {
            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;
        }
      });
    },
    // 获取退换货原因
    async getReason() {
      this.loading = true;
      let model = {
        page: 1,
        size: 100,
        type: "5",
      };
      const res = await asyncRequest.getReason(model);
      if (res && res.code === 0 && res.data) {
        const { list } = res.data;
        list.forEach((element) => {
          let obj = {
            value: element.result_code,
            label: element.result,
            id: element.id,
          };
          this.reason_options.push(obj);
        });
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

   <style lang="scss" scoped>
.zxReturn {
}
</style>
   