<style>
  .wexinappconfig .mimitBody {
    padding: 20px;
    background-color: #fff;
    margin: 10px 0 20px 0;
    display: table;
    width: 100%;
  }

  .wexinappconfig .mimitList {
    margin-bottom: 15px;
    display: flex;
    flex-direction: row;
  }

  .wexinappconfig .mimitBody .el-tag {
    min-width: 120px;
    padding-right: 50px;
  }

  .wexinappconfig .el-button--small {
    padding: 9px 25px;
  }

</style>
<template>
  <div class="wexinappconfig">
    <div class="CommonHeader">支付宝小程序配置</div>
    <div class="mimitBody">
      <div class="el-col el-col-12">
        <el-form :model="appConfig" :rules="rules" ref="appConfig" label-width="150px">
          <el-form-item label="小程序AppId" prop="AliPayAppId" size="small">
            <el-input type="text" size="small" maxlength="200" v-model="appConfig.AliPayAppId">
            </el-input>
          </el-form-item>
          <el-form-item label="支付宝公钥" prop="AliPayApiSecret" size="small">
            <el-input v-if="isShowAppSecret" type="text" @focus="isShowAppSecret=false" size="small"
              placeholder="已隐藏内容，点击查看或编辑">
            </el-input>
            <el-input v-else type="text" size="small" maxlength="5000" v-model="appConfig.AliPayApiSecret">
            </el-input>
          </el-form-item>
          <el-form-item label="开发者私钥" prop="AliPayPrivateKey" size="small">
            <el-input type="text" size="small" maxlength="5000" v-model="appConfig.AliPayPrivateKey">
            </el-input>
          </el-form-item>

          <el-form-item label="支付宝支付证书" prop="AliPayCertificateUrl" size="small">
            <template>
              <el-upload class="upload-demo" accept='doc,.docx,.xlsx,.xls,.pdf,.pem,.p12' :action="importFileUrl"
                :multiple="false" :on-success="successUpload" :file-list="fileList" :on-remove="handleRemove">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </template>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-button size="small" type="primary" @click="submitForm('appConfig')">保存</el-button>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        //是否显示小程序appSecret
        isShowAppSecret: true,
        //是否显示微信支付Api密钥
        isShowApi: true,
        //是否显示微信支付
        isShowClientOne: true,
        isShowClientTwo: true,
        //修改配置MSG
        appConfig: {
          MallBaseId: 0, //小程序Id
          AliPayAppId: "", //支付宝小程序AppId
          AliPayApiSecret: "", //支付宝公钥，由支付宝生成
          AliPayPrivateKey: "", //开发者私钥
          AliPayCertificate: "", //支付宝支付证书
          AliPayCertificateUrl: "", //支付宝支付证书路径
        },
        fileList: [],
        importFileUrl: this.domainManager().UploadBlob,

        rules: {
          AliPayAppId: [{
            required: true,
            message: "请输入appid",
            trigger: "blur"
          }],
          AliPayApiSecret: [{
            required: true,
            message: "请输入appsecret",
            trigger: "blur"
          }],
          AliPayPrivateKey: [{
            required: true,
            message: "请输入mchid",
            trigger: "blur"
          }],
          WeChatApiSecret: [{
            required: true,
            message: "请输入key",
            trigger: "blur"
          }],
        //   AliPayCertificateUrl: [{
        //     required: true,
        //     message: "请上传微信支付证书",
        //     trigger: "blur"
        //   }]
        }
      };
    },
    created() {

    },
    methods: {
      //版权信息
      GetALiPayAppConfig() {
        this.fileList = [];
        this.appConfig.MallBaseId = this.getLocalStorage().MallBaseId;
        this.apipost("/api/AliPayConfig/GetALiAppConfig", {
          MallBaseId: this.appConfig.MallBaseId,
        }, res => {
          if (res.data.resultCode == 1) {
            this.appConfig.AliPayAppId = res.data.data.AliPayAppId;
            this.appConfig.AliPayApiSecret = res.data.data.AliPayApiSecret;
            this.appConfig.AliPayPrivateKey = res.data.data.AliPayPrivateKey;
            this.appConfig.AliPayCertificate = res.data.data.AliPayCertificate;
            this.appConfig.AliPayCertificateUrl = res.data.data.AliPayCertificateUrl;
          
          } else {
            this.Info(res.data.message);
          }
        })
      },
      submitForm(msg) {
        //提交创建、修改表单
        this.$refs[msg].validate(valid => {
          if (valid) {
            this.UpdateAppConfig();
          } else {
            return false;
          }
        });
      },
      //修改支付宝小程序配置
      UpdateAppConfig() {
        this.apipost("/api/AliPayConfig/SetAliAppConfig", this.appConfig, res => {
          if (res.data.resultCode == 1) {
            this.GetALiPayAppConfig();
            this.Success("操作成功!");
          } else {
            this.Info(res.data.message);
          }
        })
      },
      successUpload(file) {
        let list = [];
        list.push({
          name: file.data.Path.split('WeChatApp')[1].replace(/[\\]/g, ''),
          url: file.data.Path.split('WeChatApp')[1].replace(/[\\]/g, ''),
        })
        this.fileList = list;
        //this.appConfig.WeChatPayCertificateUrl = file.data.Path.split('WeChatApp')[1].replace(/[\\]/g, '')
      },
      handleRemove(file, fileList) {
        this.fileList = fileList;
        if (this.fileList.length == 0) {
         // this.appConfig.WeChatPayCertificateUrl = '';
        }
      },
    },
    mounted() {
      this.GetALiPayAppConfig();
    },
  };

</script>
