<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="支付类型" prop="type">
        <el-select v-model="form.type" placeholder="支付类型" size="small" >
          <el-option
              v-for="dict in selectOps"
              :key="dict.value"
              :label="dict.value"
              :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="轮训比重" prop="forNum">
        <el-input-number :min="1" v-model="form.forNum" placeholder="请输入轮训比重" />
      </el-form-item>
<!--      <el-form-item label="最小金额" prop="minAmount">
        <el-input v-model="form.minAmount" placeholder="请输入最小金额" />
      </el-form-item>
      <el-form-item label="最大金额" prop="maxAmount">
        <el-input v-model="form.maxAmount" placeholder="请输入最大金额" />
      </el-form-item>-->
      <el-form-item label="排序" prop="sortBy">
        <el-input v-model="form.sortBy" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="支付名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入支付名称" />
      </el-form-item>
      <el-form-item label="商户ID" prop="mchId">
        <el-input v-model="form.mchId" placeholder="请输入商户ID" />
      </el-form-item>
      <el-form-item label="支付宝产品ID" prop="aliProductId">
        <el-input v-model="form.aliProductId" placeholder="请输入支付宝产品ID" />
      </el-form-item>
      <el-form-item label="微信产品ID" prop="wxProductId">
        <el-input v-model="form.wxProductId" placeholder="请输入微信产品ID" />
      </el-form-item>
      <el-form-item label="请求域名" prop="gatewayUrl">
        <el-input v-model="form.gatewayUrl" placeholder="请输入请求域名" />
      </el-form-item>
      <el-form-item label="回调域名" prop="notifyUrl">
        <el-input v-model="form.notifyUrl" placeholder="请输入回调域名" />
      </el-form-item>
      <el-form-item label="秘钥" prop="sign">
        <el-input v-model="form.sign" placeholder="请输入秘钥" type="textarea"/>
      </el-form-item>

      <el-form-item v-if="form.type == 'V14'" label="密钥文件地址" prop="privateKeyPath">
        <el-input v-model="form.extendMap.privateKeyPath" />
      </el-form-item>
      <el-form-item v-if="form.type == 'V14'" label="密钥密码" prop="privateKeyPassword">
        <el-input v-model="form.extendMap.privateKeyPassword" />
      </el-form-item>
      <el-form-item v-if="form.type == 'V14'" label="小程序AppId" prop="minAppId">
        <el-input v-model="form.extendMap.minAppId" />
      </el-form-item>
      <el-form-item v-if="form.type == 'V14'" label="小程序密钥" prop="minSecret">
        <el-input v-model="form.extendMap.minSecret" />
      </el-form-item>
      <el-form-item v-if="form.type == 'V14'" label="上级商户号" prop="lastMchId">
        <el-input v-model="form.extendMap.lastMchId" />
      </el-form-item>
      <el-form-item v-if="form.type == 'V14'" label="签名证书序列号" prop="signNo">
        <el-input v-model="form.extendMap.signNo" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addPayTrdConfig, getPayTrdConfig, updatePayTrdConfig} from "@/api/cai/payTrdConfig";

export default {
  components: {
  },
  data () {
    return {
      open: false,
      title: '',
      form:{
        id: undefined,
        type: undefined,
        name: undefined,
        mchId: undefined,
        minAmount: 0,
        maxAmount: 0,
        sortBy: 0,
        aliProductId: undefined,
        wxProductId: undefined,
        gatewayUrl: undefined,
        notifyUrl: undefined,
        sign: undefined,
        forNum: 1,
        extendMap: {
          minAppId: undefined,
          minSecret: undefined,
          signNo: undefined,
          lastMchId: undefined,
          privateKeyPath: undefined,
          privateKeyPassword: undefined,
        }
      },
      selectOps:[],
      // 表单校验
      rules: {
        minAmount: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        maxAmount: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        sortBy: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "支付类型不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "支付名称不能为空", trigger: "blur" }
        ],
        notifyUrl: [
          { required: true, message: "回调域名不能为空", trigger: "blur" }
        ],
        mchId: [
          { required: true, message: "商户ID不能为空", trigger: "blur" }
        ],
        aliProductId: [
          { required: true, message: "支付宝产品ID不能为空", trigger: "blur" }
        ],
        sign: [
          { required: true, message: "秘钥不能为空", trigger: "blur" }
        ]
      },
      buttonLoading: false,
    }
  },
  created() {
    for (let i = 0; i < 20; i++) {
      this.selectOps.push({"value": "V"+i})
    }
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "四方支付配置";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getPayTrdConfig(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePayTrdConfig(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addPayTrdConfig(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      })
    },
  }
}
</script>
