<template>
  <div>
    <el-form ref="agentForm" :model="agentObj" :rules="rules" label-width="120px">
      <el-form-item label="代理商Agent" prop="agentId">
        <supplierSelect v-model="agentObj.agentId" :companyType="'1'" placeholder="请选择代理商" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
    </el-form>
    <el-row class="operate-button">
      <el-button type="success" @click="onSubmit">提交</el-button>
      <el-button @click="cancel">关闭</el-button>
    </el-row>
  </div>
</template>

<script>
import { agentSet } from "@/api/ecw/boxSea";
import supplierSelect from "./common/supplierSelect.vue";
import { formatStringNumber, serviceMsg } from "../utils";
/**
 * agent
 */
export default {
  name: "agent",
  inheritAttrs: false,
  components: { supplierSelect },
  data() {
    return {
      // agent对象
      agentObj: {},
      // 校验
      rules: {
        agentId: [{ required: true, message: "必填", trigger: "change" }],
      },
    };
  },
  created() {
    const { currNode, shipmentObj } = this.$attrs;
    const voName = currNode.voName;
    let oldData = { ...shipmentObj[voName] };
    oldData = formatStringNumber(oldData, ["agentId"]);
    this.agentObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit() {
      this.$refs["agentForm"].validate((valid) => {
        if (valid) {
          agentSet({
            shipmentId: this.$attrs.shipmentObj.id,
            ...this.agentObj,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
