<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align: left"
    @close="closeDialog"
    width="1000px"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'160px'"
        inline
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属公司" prop="companyId">
              <el-select-tree
                :props="props"
                :options="treeData"
                v-model="formModel.companyId"
                height="200"
                style="width: 300px"
              ></el-select-tree>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户名称" prop="name">
              <el-input
                v-model="formModel.name"
                placeholder="请输入账户名称"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信商户号" prop="mchId">
              <el-input
                v-model="formModel.mchId"
                placeholder="请输入微信mchId"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="app_id" prop="appId">
              <el-input
                v-model="formModel.appId"
                placeholder="请输入app_id"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="微信支付" prop="isOpenWechat">
              <el-switch
                v-model="formModel.isOpenWechat"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
            <el-form-item label="支付宝支付" prop="isOpenAlipay">
              <el-switch
                v-model="formModel.isOpenAlipay"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信子商户号" prop="subMchId">
              <el-input
                v-model="formModel.subMchId"
                placeholder="请输入微信subMchId"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信子公众号app_id" prop="subAppId">
              <el-input
                v-model="formModel.subAppId"
                placeholder="请输入服务商模式下子公众号APPID"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="微信子公众号sub_app_secret"
              prop="subAppSecret"
            >
              <el-input
                v-model="formModel.subAppSecret"
                placeholder="请输入sub_app_secret"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="微信的回调地址" prop="wechatNotifyUrl">
              <el-input
                v-model="formModel.wechatNotifyUrl"
                placeholder="请输入微信的回调地址"
                style="width: 450px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="支付宝令牌" prop="appAuthToken">
              <el-input
                v-model="formModel.appAuthToken"
                placeholder="请输入支付宝appAuthToken"
                style="width: 450px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="支付宝的回调地址" prop="alipayNotifyUrl">
              <el-input
                v-model="formModel.alipayNotifyUrl"
                placeholder="请输入支付宝的回调地址"
                style="width: 450px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </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 paymentInfoApi from "@/api/base/paymentInfo";
import companyInfoApi from "@/api/bus/companyInfo";
import pageUtil from "@/utils/page";
import SelectTree from "@/components/SelectTree";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
export default {
  props: ["businessKey", "title"],
  data() {
    return {
      ruleValidate: {
        companyId: [
          { required: true, message: "物业公司不能为空", trigger: "change" },
        ],
        name: [
          { required: true, message: "账户名称不能为空", trigger: "blur" },
        ],
        cycle: [
          { required: true, message: "结算周期不能为空", trigger: "blur" },
        ],
      },
      formModel: {
        companyId: "",
        name: "",
        mchId: "",
        subMchId: "",
        appAuthToken: "",
        financePersonId: "",
        financeUserId: "",
      },
      treeData: [],
      personList: [],
      userList: [],
      companyList: [],
      showDialog: true,
      loading: false,
      submitting: false,
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
    };
  },
  created() {
    this.loadTree();
  },
  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 paymentInfoApi.add(self.formModel);
            } else {
              return paymentInfoApi.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);
            }
          });
        }
      });
    },
    loadTree() {
      companyInfoApi.treeList().then((resp) => {
        var jsonData = resp.data;

        if (jsonData.result) {
          this.treeData = jsonData.data;
        } else {
          this.$message.error(jsonData.message + "");
        }
      });
    },
  },
  mounted: function () {
    var self = this;

    companyInfoApi.treeList().then((resp) => {
      var jsonData = resp.data;
      this.companyList = jsonData.data;
    });

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

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>