<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="800px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select :disabled="form.id" v-model="form.payType" placeholder="支付方式" clearable size="small" style="width: 99%">
          <el-option
              v-for="dict in payConfigTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户号" prop="wxMcid" v-if="form.payType === 2">
        <el-input v-model="form.wxMcid" placeholder="请输入商户号" />
      </el-form-item>
      <el-form-item label="微信key" prop="wxKey" v-if="form.payType === 2">
        <el-input v-model="form.wxKey" placeholder="请输入微信key" />
      </el-form-item>
      <el-form-item label="支付宝AppId" prop="appid" v-if="form.payType === 1">
        <el-input v-model="form.appid" placeholder="请输入AppId" />
      </el-form-item>
      <el-form-item label="私钥" prop="privateKey" v-if="form.payType === 1">
        <el-input v-model="form.privateKey" placeholder="请输入秘钥" type="textarea" :rows="5"/>
      </el-form-item>
      <el-form-item label="公钥" prop="publicKey" v-if="form.payType === 1">
        <el-input v-model="form.publicKey" placeholder="请输入公钥" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item label="回调域名" prop="notifyUrl" >
        <el-input v-model="form.notifyUrl" placeholder="请输入回调域名" />
      </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 {payConfigTypeList} from "@/constant/statusMap";
import {addPayConfig, getPayConfig, updatePayConfig} from "@/api/cai/payConfig";

export default {
  components: {
  },
  data () {
    return {
      payConfigTypeList,
      open: false,
      title: '',
      form:{
        id: undefined,
        name: undefined,
        payType: 1,
        appid: undefined,
        wxMcid: undefined,
        wxKey: undefined,
        privateKey: undefined,
        publicKey: undefined,
        notifyUrl: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        payType: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        appid: [
          { required: true, message: "回调地址不能为空", trigger: "blur" }
        ],
        wxMcid: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        wxKey: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        privateKey: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        publicKey: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "支付配置";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getPayConfig(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePayConfig(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addPayConfig(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
