<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleForm"
    :size="'mini'"
    label-width="90px"
  >
    <el-row>
      <el-col :span="12"
        ><el-form-item label="货物情况" prop="good_receive_type">
          <el-select placeholder="货物情况" style="width:100%" v-model="ruleForm.good_receive_type">
            <el-option v-for="opt in statusOptions" :key="opt.value" :value="opt.value" :label="opt.label" />
          </el-select>
        </el-form-item>

        <el-form-item label="丢失数量" prop="loss_num">
          <digital-input
            :values="ruleForm.loss_num"
            :placeholder="'丢失数量'"
            :min="0"
            :max="100000000000"
            :position="'right'"
            :precision="0"
            :size="'mini'"
            :controls="false"
            :append="''"
            @reschange="number_change($event, 'loss_num')"
          />
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="备注" prop="remark">
          <el-input
            placeholder="请输入备注"
            type="textarea"
            maxlength="100"
            :size="'mini'"
            v-model="ruleForm.remark"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="fr"
            :size="'mini'"
            @click="submitForm"
            :loading="loading"
            >保 存
          </el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>

import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/deliveryWorkOrder";
import { isAlphanumeric } from "@/utils/validate";
export default {
  name: "allotFlow",
  mixins: [resToken],
  props: ["id", "sitem"],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find((i) => i.menu_route == "allotDetail") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "DBD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    const validateExpressSn = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("物流单号不能为空！"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("请输入正确的物流单号"));
        } else {
          callback();
        }
      }
    };
    const validate_loss_num = (rule, value, callback) => {
      const { required } = rule;
      if (required && value === "") {
        callback(new Error("不能为空！"));
      } else {
        callback();
      }
    };

    return {
      status: "",
      ruleForm: {
        good_receive_type: "0", //发货物流公司
        remark: "", //物流单号
        loss_num: "",
      },
      statusOptions:[
        { value:'0', label:'库房未发货无需追回'},
        { value:'1', label:'库房已发货无全部追回'},
        { value:'2', label:'库房已发货已无法追回'},
        { value:'3', label:'库房已发货已部分追回'},
      ],
      rules: {
        good_receive_type: [
          {
            type: "array",
            required: true,
            message: "请选择发货公司",
            trigger: "change",
          },
        ],
        loss_num: [
          {
            required: true,
            validator: validate_loss_num,
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            trigger: "blur",
            validator: validateExpressSn,
          },
        ],
      },
    };
  },
  methods: {
    async number_change(e, key) {
      this.ruleForm[key] = e + "" || "0";
      this.$refs.ruleForm.validateField(key);
    },
    // 商品保存提交
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const model = JSON.parse(JSON.stringify(this.ruleForm));
          model.outChildCode = this.sitem.outChildCode;
          model.good_receive_type = model.good_receive_type.toString();
          
          let res = await asyncRequest.express({list:[model]});
          this.loading = false;
          
          if (res && res.code === 0) {
            //
            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;
        }
      });
    },
    handleCompany(e) {
      console.log(e);
      this.ruleForm.good_receive_type = e && e.code ? [e.label] : [];
      this.$refs.ruleForm.validateField("good_receive_type");
    },
  },
};
</script>
