<template>
  <!-- 添加承运商信息 -->
  <div class="addCarrier">
    <PageTitle>返回</PageTitle>
    <div class="user_code">
      <span class="text">账号</span>
      <el-input
        class="input"
        placeholder="请输入账号(必填项)"
        v-model="input"
        clearable
      >
      </el-input>
    </div>
    <div class="user_code">
      <span class="text">用户名</span>
      <el-input
        class="input"
        placeholder="请输入用户名(必填项)"
        v-model="user_Name"
        clearable
      >
      </el-input>
    </div>
    <div class="form_box" style="margin-right: 10rem">
      <dil-form :formId="330" v-model="form1"></dil-form>
    </div>
    <el-upload
      class="upload-demo"
      ref="upload1"
      action="https://wl.dasteel.cn:32322/api/v1/rms/rmscarrier/uploadCarrier1"
      :before-upload="beforeUpload"
      :multiple="false"
      list-type="picture"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :on-error="onError(1)"
    >
      <span class="span"></span>
      <el-input
        class="shippingCertificate"
        placeholder="请选择运输证(必填项)"
        v-model="shippingCertificate"
        disabled
      >
      </el-input>
      <el-button size="small" type="primary" @click="upCLick(1)"
        >点击上传运输证</el-button
      >
    </el-upload>
    <el-upload
      class="upload-demo"
      ref="upload2"
      action="https://wl.dasteel.cn:32322/api/v1/rms/rmscarrier/uploadCarrier2"
      :before-upload="beforeUpload"
      :multiple="false"
      list-type="picture"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :on-error="onError(2)"
    >
      <span class="span"></span>
      <el-input
        class="businessLicense"
        placeholder="请选择经营许可证(必填项)"
        v-model="businessLicense"
        disabled
      >
      </el-input>
      <el-button size="small" type="primary" @click="upCLick(2)"
        >点击上传经营许可证</el-button
      >
    </el-upload>
    <el-upload
      class="upload-demo"
      ref="upload3"
      action="https://wl.dasteel.cn:32322/api/v1/rms/rmscarrier/uploadCarrier3"
      :before-upload="beforeUpload"
      :multiple="false"
      list-type="picture"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :on-error="onError(3)"
    >
      <span class="span"></span>
      <el-input
        class="businessLicense1"
        placeholder="请选择营业执照(必填项)"
        v-model="businessLicense1"
        disabled
      >
      </el-input>
      <el-button size="small" type="primary" @click="upCLick(3)"
        >点击上传营业执照</el-button
      >
    </el-upload>
    <div class="form-box f1">
      <el-button
        type="primary"
        v-if="this.form1.carrierType == '销售承运商'"
        @click="drawer = true"
        >添加中标区域信息</el-button
      >
    </div>
    <el-drawer :visible.sync="drawer" :direction="direction" size="40%">
      <el-input placeholder="请输入内容" v-model="inputText" clearable>
      </el-input>
      <el-button type="primary" class="btn" @click="onClick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <div>
        <dilTable
          v-bind.sync="options"
          @radio-change="currentRadioChange"
        ></dilTable>
      </div>
    </el-drawer>
    <div class="button_box">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="makeSure">确定</el-button>
    </div>
  </div>
</template>
<script>
import PageTitle from "@/components/Page/Title";
import { sjTime } from "@/utils/sharedJsFile.js";
export default {
  components: { PageTitle },
  data() {
    return {
      user_Name:'',
      //营业执照
      businessLicense1: null,
      //营业执照
      businessLicense: null,
      //运输证
      shippingCertificate: null,
      inputText: "",
      direction: "rtl",
      input: "",
      form1: {},
      drawer: false,
      options: {
        // first请求数据的地址
        requestUrl: "/api/v1/rms/getCarrierBidAreaList?apiId=406",
        selectionType: "radio",
        mapList: [],
      },
      imageUrl1: "",
      imageUrl2: "",
      imageUrl3: "",
      num: null,
      upBool: false,
      bidAreaId: null,
    };
  },
  created() {
    this.form1 = {
      carrierType: null,
    };
  },
  methods: {
    VerifyPhoneNumber(Phone) {
      var myreg=/^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
      if (!myreg.test(Phone)) {
          return false;
      } else {
          return true;
      }
    },
    upCLick(val) {
      this.num = val;
    },
    beforeUpload(file) {
      this.upBool = true;
      const isLt2M = file.size < 1024 * 1024 * 0.5;
      if (!isLt2M) {
        this.$message.error("上传文件大小不能超过500kb!");
      } else {
        let size = file.size / 1024;
        let _URL = window.URL || window.webkitURL;
        let img = new Image();
        img.src = _URL.createObjectURL(file);
      }
      return isLt2M;
    },
    onError(err) {
      if (this.upBool) {
        if (this.num == 1) {
          this.shippingCertificate = null;
          this.imageUrl1 = null;
          this.$message.error("上传失败");
        } else if (this.num == 2) {
          this.businessLicense = null;
          this.imageUrl2 = null;
          this.$message.error("上传失败");
        } else if (this.num == 3) {
          this.businessLicense1 = null;
          this.imageUrl3 = null;
          this.$message.error("上传失败");
        }
      }
    },
    handleAvatarSuccess(res, file) {
      if (res.code) {
        this.upBool = false;
        if (this.num == 1) {
          this.imageUrl1 = URL.createObjectURL(file.raw);
          this.shippingCertificate = file.name;
        } else if (this.num == 2) {
          this.imageUrl2 = URL.createObjectURL(file.raw);
          this.businessLicense = file.name;
        } else if (this.num == 3) {
          this.imageUrl3 = URL.createObjectURL(file.raw);
          this.businessLicense1 = file.name;
        }
        this.$message.success("上传成功");
      }
    },
    onClick() {
      this.options.requestUrl =
        "/api/v1/rms/getCarrierBidAreaList?apiId=406&con=" + this.inputText;
    },
    currentRadioChange(selection) {
      this.bidAreaId = selection.bidAreaId;
    },
    makeSure() {
      let status = false;
      this.form1.carrierContactNumber = parseInt(this.form1.carrierContactNumber);
      if (!this.input) {
        this.$message.error("账号不能为空");
      } else if(!this.user_Name){
        this.$message.error("用户名不能为空");
      }else if (!this.form1.carrierName) {
        this.$message.error("承运商名称不能为空");
      } else if (!this.form1.carrierType) {
        this.$message.error("承运商类型不能为空");
      }
      // else if (!this.imageUrl1) {
      //   this.$message.error("请上传运输证");
      // } else if (!this.imageUrl2) {
      //   this.$message.error("请上传经营许可证");
      // } else if (!this.imageUrl3) {
      //   this.$message.error("请上传营业执照");
      // }
      else {
        status = true;
      }
      if (this.form1.registerDate) {
        this.form1.registerDate = sjTime(this.form1.registerDate);
      }
      if (status) {
        let rand = "";
        for (let i = 0; i < 8; i++) {
          rand += Math.floor(Math.random() * 10);
        }
        let fromData = new FormData();
        fromData.append("userCode", this.input);
        fromData.append("userName", this.user_Name);
        fromData.append("orgCode", "chengyunshang");
        fromData.append("mobile", "133" + rand);
        fromData.append("email", "133" + rand + "@163.com");
        fromData.append("orgName", "承运商");
        fromData.append("groupId", "506514577756917769");
        fromData.append("companyId", "713710108567277568");
        fromData.append("orgId", "924126339513061376");

        this.$store
          .dispatch("system/usersManage/addUser", fromData)
          .then((res) => {
            console.log(1)
            if (res.code === "0") {
              let userId = res.data.userId;
              this.axios
                .post(
                  "pass/v1/sysuserroles/addUserroles?userId=" +
                    userId +
                    "&userCode=" +
                    this.input +
                    "&roleId=924449091658846208"
                )
                .then((res) => {
                  console.log(2)
                  if (res.code == "0") {
                    //图片地址
                    let imageUrls = {
                      //运输证
                      imageUrl1: this.imageUrl1,
                      //经营许可证
                      imageUrl2: this.imageUrl2,
                      //营业执照
                      imageUrl3: this.imageUrl3,
                    };
                    //传给后端的数据
                    let mapValue = {
                      //承运商账号
                      carrierSsoCode: this.input,
                      //表单数据
                      from: this.form1,
                      //图片数据
                      imageUrls: imageUrls,
                      //sso的用户Id
                      userId: userId,
                    };
                    this.axios
                      .post("/api/v1/rms/insertCarrier", mapValue)
                      .then((res) => {
                        if (res.data.code == "200") {
                          this.cancel();
                          this.$message.success("添加成功");
                        } else {
                          this.axios
                          .delete("pass/v1/sysusers/" + userId)
                          .then((res) => {
                            this.$message.error("添加失败");
                          });
                        }
                      })
                      .catch(() => {
                        this.axios
                          .delete("pass/v1/sysusers/" + userId)
                          .then((res) => {
                            this.$message.error("添加失败");
                          });
                      });
                  }
                });
            }
          });
      }
    },
    // 取消   
    cancel() {
      this.$router.push("/carrier");
    },
  },
};
</script>
<style lang='scss' >
.addCarrier {
  .user_code {
    display: flex;
    justify-content: center;
    margin: 10px;
    .text {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 10px;
    }
    .input {
      width: 250px;
    }
  }
  .form_box {
    display: flex;
    justify-content: center;
    margin-left: 170px;
    margin-top: 15px;
    .el-form-item {
      display: flex;
      justify-content: center;
      .el-form-item__label {
        display: flex;
        align-items: center;
      }
      .el-form-item__content {
        width: 250px;
        .el-radio-group {
          width: 500px;
        }
        .el-date-editor {
          width: 250px;
        }
        .el-input__inner {
          width: 250px;
        }
      }
    }
  }
  .upload-demo {
    display: flex;
    justify-content: center;
    margin: 10px;
    .span {
      display: inline-block;
      width: 200px;
      height: 2px;
    }
    .el-button {
      width: 150px;
      margin-right: 10px;
    }
    .shippingCertificate,
    .businessLicense,
    .businessLicense1 {
      width: 250px;
    }
  }
  .form-box {
    display: flex;
    justify-content: center;
  }
  .button_box {
    display: flex;
    justify-content: center;
    margin: 20px;
    .el-button {
      width: 100px;
    }
  }
}
</style>
