<template>
  <div>
    <el-form ref="agentForm" :model="agentObj" :rules="rules" label-width="120px">
      <el-form-item :label="$t('代理商Agent')" prop="agentId">
        <supplierOutSelect v-model="agentObj.agentId" :companyType="'1'" :areaType="1" :placeholder="$t('请选择代理商')" :allSupplier="this.$attrs.allSupplier" />
        <el-button v-hasPermi="['ecw:supplier:query']" v-if="agentObj.agentId" type="text" style="font-size: 13px; margin-left: 240px;" @click="SupplierDetail">{{$t('查看代理商详情')}}</el-button>
      </el-form-item>
    </el-form>
    <el-row class="operate-button">
      <el-button type="success" @click="onSubmit">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
    </el-row>
  </div>
</template>

<script>
import { agentSet } from "@/api/ecw/boxSea";
import supplierOutSelect from "./common/supplierOutSelect.vue";
import { formatStringNumber, serviceMsg } from "../utils";
/**
 * agent
 */
export default {
  name: "agent",
  inheritAttrs: false,
  components: { supplierOutSelect },
  data() {
    return {
      // agent对象
      agentObj: {},
      // 校验
      rules: {
        agentId: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
      supplierId: 0
    };
  },
  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);
    },
    SupplierDetail(){
      this.supplierId = this.agentObj.agentId
      this.$router.push('/supplier/detail?id='+this.supplierId)
    },
  },
};
</script>

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