
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align: left"
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'100px'"
      >
        <el-form-item label="公司车队" prop="companyId">
          <el-select-tree
            :props="props"
            :options="companyResult"
            v-model="formModel.companyId"
            size="mediumn"
            width="300px"
          ></el-select-tree>
        </el-form-item>
        <el-form-item label="商户名称" prop="name">
          <el-input
            v-model="formModel.name"
            placeholder="请输入商户名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="imageUrl">
          <el-upload
            class="avatar-uploader"
            name="photoFile"
            :action="uploadUrl"
            :show-file-list="false"
            :headers="headers"
            :data="uploadData"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="fileUrl" :src="fileUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="formModel.phone"
            placeholder="请输入手机号"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input
            v-model="formModel.bankName"
            placeholder="请输入银行名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="银行卡账号" prop="bankNumber">
          <el-input
            v-model="formModel.bankNumber"
            placeholder="请输入银行卡账号"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="提现许可" prop="withdrawAllow">
          <el-radio-group v-model="formModel.withdrawAllow">
            <el-radio :label="true">可提现</el-radio>
            <el-radio :label="false">不可提现</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商户状态" prop="frozen">
          <el-radio-group v-model="formModel.frozen">
            <el-radio :label="true">已冻结</el-radio>
            <el-radio :label="false">未冻结</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import accountInfoApi from "@/api/merchant/accountInfo";
import companyInfoApi from "@/api/bus/companyInfo";
import SelectTree from "@/components/SelectTree";

import { getToken } from "@/utils/auth"; // get token from cookie
export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [
          { required: true, message: "商户名称不能为空", trigger: "blur" },
        ],
        companyId: [
          {
            required: true,
            message: "关联的公司车队不能为空",
            trigger: "blur",
          },
        ],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        bankName: [{ required: true, message: "不能为空", trigger: "blur" }],
        bankNumber: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      //上传地址
      uploadUrl: Constant.serverUrl + "/uploadPicture",
      uploadData: {
        subFolder: "accountInfo",
      },
      fileUrl: "",
      headers: {
        Authorization: getToken(),
      },
      companyResult: [],
      treeData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
    };
  },
  created() {
    var self = this;

    companyInfoApi.treeList().then(function (response) {
      var jsonData = response.data;
      if (jsonData.result) {
        self.companyResult = jsonData.data;
      }
    });
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return accountInfoApi.add(self.formModel);
            } else {
              return accountInfoApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
    handleAvatarSuccess(res, file) {
      var self = this;
      self.formModel.imageUrl = res.data;
      self.fileUrl =
        res.data + "?x-oss-process=image/resize,m_lfit,h_200,w_200";
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  mounted: function () {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return accountInfoApi.create();
      } else {
        return accountInfoApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
          let imageUrl = self.formModel.imageUrl;
          if (imageUrl != null) {
            self.fileUrl =
              imageUrl + "?x-oss-process=image/resize,m_lfit,h_200,w_200";
          }

          let withdrawAllow = self.formModel.withdrawAllow;
          if (withdrawAllow == null) {
            self.formModel.withdrawAllow = false;
          }
          let frozen = self.formModel.frozen;
          if (frozen == null) {
            self.formModel.frozen = false;
          }
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>