<template>
  <el-dialog
    v-loading="loading"
    title="填写物流"
    :center="true"
    align="left"
    top="20vh"
    width="700px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="closeModel"
  >
    <el-card style="margin-top: -25px">
      <el-row>
        <el-col :span="24" style="padding-right: 10px">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="物流公司" prop="company">
              <el-input v-model="ruleForm.company" />
            </el-form-item>
            <el-form-item label="物流单号" prop="code">
              <el-input v-model="ruleForm.code" />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="padding-right: 10px; text-align: right">
          <el-button type="primary" @click="submitForm">保 存 </el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import appProcess from "./appProcess";
import asyncRequest from "@/apis/service/InvoiceSales/invoiceApply";
import { isChinese, isEmoticon } from "@/utils/validate";
import resToken from "@/mixins/resToken";
export default {
  name: "invoiceApply",
  props: ["showModel", "id", "isDetail", "status", "invNo"],
  mixins: [resToken],
  components: {
    appProcess,
  },
  data() {
    const validatecompany = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("物流公司不能为空！"));
      } else {
        if (!isChinese(value))
          if (isEmoticon(value)) {
            callback(new Error("物流公司名字格式不正确！"));
          } else {
            callback();
          }
      }
    };

    const validatecode = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        if (isChinese(value)) {
          callback(new Error("物流单号格式不正确！"));
        } else if (isEmoticon(value)) {
          callback(new Error("物流单号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    return {
      loading: false,
      newTime: 0,
      showModelThis: this.showModel,
      ruleForm: {
        company: "",
        code: "",
      },
      rules: {
        company: [
          {
            required: true,
            // validator: validatecompany,
            trigger: "blur",
          },
        ],
        code: [
          {
            required: true,
            // validator: validatecode,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      this.loading = true;
      await this.resetForm();
      this.loading = false;
    },
    refreshAll() {
      this.showModelThis = false;
      this.$emit("refresh");
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            company: "",
            code: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { company, code } = JSON.parse(JSON.stringify(this.ruleForm));
          const model = {
            invNo: this.invNo,
            company: company,
            code: code,
          };
          const res = await asyncRequest.post(model);
          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;
        }
      });
    },
  },
};
</script>

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