<template>
  <el-dialog
    v-loading="loading"
    :title="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" v-if="id && id.order_sn">
          <el-form
            label-position="left"
            size="mini"
            inline
            class="demo-table-expand"
          >
            <el-col :span="12">
              <el-form-item label="订单编号">
                <span>{{ id.order_sn }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="订单数量">
                <span
                  >{{ id.order_num }}{{ id.unit }}/{{ id.unit_weight }}kg</span
                >
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收货人">
                <span>{{ id.contector }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收货电话">
                <span>{{ id.contector_mobile }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="收货地址">
                <span
                  >{{ id.provice_name }}{{ id.city_name }}{{ id.area_name
                  }}{{ id.addr }}</span
                >
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="商品名称">
                <span>{{ id.good_name }}</span>
              </el-form-item>
            </el-col>
          </el-form>
        </el-col>
        <el-col :span="24" v-if="isDetail === '009'">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="物流公司" prop="post_company">
              <el-input v-model="ruleForm.post_company" />
            </el-form-item>
            <el-form-item label="物流单号" prop="post_code">
              <el-input v-model="ruleForm.post_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 asyncRequest from "@/apis/service/mobile/order";
import { isChinese, isEmoticon } from "@/utils/validate";
import resToken from "@/mixins/resToken";
export default {
  name: "order",
  props: ["showModel", "id", "isDetail"],
  mixins: [resToken],
  data() {
    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: {
        post_company: "",
        code: "",
      },
      rules: {
        post_company: [
          {
            required: true,
            message: "物流公司不能为空！",
            trigger: "blur",
          },
        ],
        post_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;
      if (this.isDetail == "007") {
        this.title = "订单详情";
      } else {
        this.title = "填写物流";
      }
      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 = {
            post_company: "",
            post_code: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const obj = JSON.parse(JSON.stringify(this.ruleForm));
          const model = {
            ordersn: this.id.order_sn,
            post_company: obj.post_company,
            post_code: obj.post_code,
          };
          const res = await asyncRequest.post(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: `${this.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>
.order {
}
</style>
