<template>
  <div class="shipping-update-error">
    <!-- 更新报关异常状态 -->
    <div class="message-title">{{headerTitle}}</div>
    <el-form ref="updateErrorForm" :rules="rules" :model="updateObj" label-width="140px">
      <el-form-item :label="$t('更新类型')" prop="type">
        <el-radio-group v-model="updateObj.type">
          <el-radio v-for="item in this.getDictDatas(this.DICT_TYPE.BOX_SHIPPING_UPDATE_TYPE)" :key="item.value" :label="item.value">{{$l(item, 'label')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('更新订单状态')">
        <el-radio-group v-model="updateObj.updateOrder">
          <el-radio v-for="item in types" :key="item.value" :label="item.value">{{$l(item, 'label')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="customize">
        <el-input v-model="updateObj.customize" :placeholder="$t('请输入')" :disabled="!isEnter"></el-input>
      </el-form-item>
    </el-form>
    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
    </el-row>
  </div>
</template>

<script>
import { updateAbnormalOrder } from "@/api/ecw/box";
import { serviceMsg } from "./shippingSea/utils";

export default {
  name: "updateError",
  inheritAttrs: false,
  components: {},
  props: {
    shipmentObj: Object,
    errorInfo: Object,
  },
  data() {
    return {
      updateObj: {},
      rules: {
        type: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
      types: [],
      isEnter: false,
      headerTitle: "",
    };
  },
  methods: {
    onSubmit() {
      this.$refs["updateErrorForm"].validate((valid) => {
        if (valid) {
          let text = "";
          // 自定义
          if (this.updateObj.updateOrder) {
            if (this.updateObj.updateOrder === "0") {
              text = this.updateObj.customize;
            } else {
              const dict = this.types.find(
                (item) => item.value === this.updateObj.updateOrder
              );
              text = this.$l(dict, "label");
            }
          }

          const { orderList } = this.errorInfo;
          updateAbnormalOrder({
            shipmentId: this.shipmentObj.id,
            orderIdList: orderList.map((item) => item.orderId) ?? [],
            type: this.updateObj.type,
            text,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.$emit("closeDialog", "detail");
            });
          });
        }
      });
    },
    cancel() {
      this.$emit("closeDialog");
    },
  },
  watch: {
    errorInfo: {
      handler: function (val) {
        const { errorType, orderList, operate } = val;

        if (operate === "single") {
          this.headerTitle = this.$t(
            "确定给{selfNo}下的{orderNos}更新状态吗?",
            {
              selfNo: this.shipmentObj.selfNo,
              orderNos: orderList.map((item) => item.orderNo).join(",") ?? "",
            }
          );
        } else {
          this.headerTitle = this.$t(
            `确定给{selfNo}下的${
              operate === "all" ? this.$t("所有订单") : this.$t("所选订单")
            }更新状态吗?`,
            {
              selfNo: this.shipmentObj.selfNo,
            }
          );
        }

        switch (errorType) {
          // 报关异常
          case "customs":
            this.types = this.getDictDatas(
              this.DICT_TYPE.BOX_CUSTOMS_ERROR_TYPE
            );
            break;
          case "shipping":
            this.types = this.getDictDatas(
              this.DICT_TYPE.BOX_SHIPPING_ERROR_TYPE
            );
            break;
          case "arrival":
            this.types = this.getDictDatas(
              this.DICT_TYPE.BOX_ARRIVAL_ERROR_TYPE
            );
            break;
          case "flight":
            this.types = this.getDictDatas(
              this.DICT_TYPE.BOX_FLYING_ERROR_TYPE
            );
            break;
        }
      },
      immediate: true,
    },
    "updateObj.updateOrder"(val) {
      if (val === "0") {
        this.isEnter = true;
        this.rules.customize = [
          { required: true, message: this.$t("必填"), trigger: "change" },
        ];
      } else {
        this.isEnter = false;
        this.rules.customize = [];
        this.$refs["updateErrorForm"].clearValidate("customize");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.shipping-update-error {
  .message-title {
    text-align: center;
    font-size: 20px;
    margin: 0 20px 10px;
  }
}
</style>