<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="900px"
    :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
            size="small"
            inline
            label-width="80px"
            class="demo-table-expand"
          >
            <el-col :span="12">
              <el-form-item label="订单编号" style="margin: 0">
                <span>{{ id.order_sn }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="订单数量" style="margin: 0">
                <span>{{ id.order_num }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收货人" style="margin: 0">
                <span>{{ id.contector }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收货电话" style="margin: 0">
                <span>{{ id.contector_mobile }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="isDetail !== '009'">
              <el-form-item label="快递公司" style="margin: 0">
                <span>{{ id.post_name }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="isDetail !== '009'">
              <el-form-item label="快递编号" style="margin: 0">
                <span>{{ id.post_code }}</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
            size="small"
            :rules="rules"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-col :span="12">
              <el-form-item label="物流公司" prop="express_code">
                <el-select
                  v-model="ruleForm.express_code"
                  filterable
                  clearable
                  placeholder="物流公司"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options"
                    :key="'express_code' + item.express_code"
                    :label="item.express_name"
                    :value="item.express_code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物流单号" prop="post_code">
                <el-input
                  v-model="ruleForm.post_code"
                  placeholder="物流单号"
                  maxlength="100"
                />
              </el-form-item>
            </el-col>
          </el-form>
        </el-col>
        <el-col :span="isDetail === '009' ? 20 : 24" v-if="id && id.order_sn">
          <span
            style="
              width: 120px;
              padding: 0 12px;
              line-height: 32px;
              height: 32px;
              font-size: 14px;
              color: #606266;
              text-align: right;
              vertical-align: middle;
              font-weight: 700;
              box-sizing: border-box;
            "
            >收货地址</span
          >
          <span
            >{{ id.provice_name }}{{ id.city_name }}{{ id.area_name
            }}{{ id.addr }}</span
          >
        </el-col>
        <el-col
          :span="4"
          style="text-align: right"
          class="fr"
          v-if="isDetail === '009'"
        >
          <el-button type="primary" size="small" @click="submitForm"
            >保 存
          </el-button>
        </el-col>
        <div
          :span="24"
          v-if="isDetail !== '009' && id && id.status === '2' && express"
        >
          <span
            style="
              width: 120px;
              padding: 0 12px;
              line-height: 32px;
              height: 32px;
              font-size: 14px;
              color: #606266;
              text-align: right;
              vertical-align: middle;
              font-weight: 700;
              box-sizing: border-box;
            "
            >最新物流</span
          >
          <span>{{ express.status_cn }}</span>
          <div v-if="express.post_content&&express.post_content.length>0" class="post_content">
            <el-timeline style="padding:20px 0 0 0">
              <el-timeline-item
                v-for="(activity, index) in express.post_content"
                :key="activity.time + index"
                :size="'normal'"
                :color="index === 0 ? '#1890ff' : '#e4e7ed'"
                :timestamp="activity.time"
              >
                {{ activity.context }}
              </el-timeline-item>
            </el-timeline>
          </div>
        </div>
      </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 {
      options: [],
      loading: false,
      newTime: 0,
      showModelThis: this.showModel,
      express: null,
      ruleForm: {
        express_code: "",
        code: "",
      },
      rules: {
        express_code: [
          {
            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();
      await this.getExquery();
      if (this.id && this.id.status === "2") {
        await this.getOrderexpress(this.id.order_sn);
      }
      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 = {
            express_code: "",
            post_code: "",
          };
        }
      });
    },
    async getExquery() {
      this.options = [];
      let model = {
        status: "1",
        code: "",
        express_name: "",
      };
      const { code, data, message } = await asyncRequest.exquery(model);
      if (code === 0) {
        this.options = data;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    async getOrderexpress(order_sn) {
      this.express = null;
      const { code, data, message } = await asyncRequest.orderexpress({
        order_sn: order_sn,
      });
      if (code === 0) {
        this.express = data;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    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,
            express_code: obj.express_code,
            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 {
  .post_content{
    padding: 0  0 0 20px;
    max-height: 300px;
    overflow-y:scroll;
  }
}
</style>
