<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="公众号appId" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入公众号appId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小程序appId" prop="miniappId">
        <el-input
          v-model="queryParams.miniappId"
          placeholder="请输入小程序appId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户号" prop="mchId">
        <el-input
          v-model="queryParams.mchId"
          placeholder="请输入商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['redPacket:more:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['redPacket:more:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['redPacket:more:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-tag
          type="primary"
          size="large"
          style="font-size: 16px;"
        >
          当前使用的商户号：{{this.redPacketMchId}}
        </el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          @click="handleEditRedPacket"
          v-hasPermi="['redPacket:more:editRedPacket']"
        >修改当前发送的红包的商户号</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="moreList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="商户类型" align="center" prop="isNew" >
        <template slot-scope="scope">
          {{ scope.row.isNew === 0 ? '老商户' : '新商户' }}
        </template>
      </el-table-column>
      <el-table-column label="公众号appId" align="center" prop="appId" />
      <el-table-column label="小程序appId" align="center" prop="miniappId" />
      <el-table-column label="商户号" align="center" prop="mchId" />
      <el-table-column label="商户密钥" align="center" prop="mchKey" />
      <el-table-column label="p12证书文件" align="center" prop="keyPath" />
      <el-table-column label="apiclient_key.pem证书" align="center" prop="privateKeyPath" />
      <el-table-column label="apiclient_cert.pem证书" align="center" prop="privateCertPath" />
      <el-table-column label="apiV3" align="center" prop="apiV3Key" />
      <el-table-column label="公钥ID" align="center" prop="publicKeyId" />
      <el-table-column label="pub_key.pem证书" align="center" prop="publicKeyPath" />
      <el-table-column label="回调地址" align="center" prop="notifyUrl" />
      <el-table-column label="回调地址" align="center" prop="notifyUrlScrm" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['redPacket:more:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['redPacket:more:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改多商户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="商户类型" prop="isNew">
          <el-radio-group v-model="form.isNew">
            <el-radio :label="0">商家转账到零钱(旧)</el-radio>
            <el-radio :label="1">商家转账(新)</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="公众号appId" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入公众号appId" />
        </el-form-item>
        <el-form-item label="小程序appId" prop="miniappId">
          <el-input v-model="form.miniappId" placeholder="请输入小程序appId" />
        </el-form-item>
        <el-form-item label="商户号" prop="mchId">
          <el-input v-model="form.mchId" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="商户密钥" prop="mchKey">
          <el-input v-model="form.mchKey" placeholder="请输入商户密钥" />
        </el-form-item>
        <el-form-item label="p12证书文件" prop="keyPath">
          <el-input v-model="form.keyPath" placeholder="请输入p12证书文件的绝对路径或者以classpath:开头的类路径." />
        </el-form-item>
        <el-form-item label="apiclient_key证书地址" prop="privateKeyPath">
          <el-input v-model="form.privateKeyPath" placeholder="请输入apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径." />
        </el-form-item>
        <el-form-item label="apiclient_cert证书地址" prop="privateCertPath">
          <el-input v-model="form.privateCertPath" placeholder="请输入apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径." />
        </el-form-item>
        <el-form-item label="apiV3秘钥" prop="apiV3Key">
          <el-input v-model="form.apiV3Key" placeholder="请输入apiV3 秘钥值." />
        </el-form-item>
        <el-form-item label="公钥ID" prop="publicKeyId">
          <el-input v-model="form.publicKeyId" placeholder="请输入公钥ID" />
        </el-form-item>
        <el-form-item label="pub_key.pem证书地址" prop="publicKeyPath">
          <el-input v-model="form.publicKeyPath" placeholder="请输入pub_key.pem证书文件的绝对路径或者以classpath:开头的类路径." />
        </el-form-item>
        <el-form-item label="回调地址" prop="notifyUrl">
          <el-input v-model="form.notifyUrl" placeholder="请输入回调地址notifyUrl" />
        </el-form-item>
        <el-form-item label="回调地址" prop="notifyUrlScrm">
          <el-input v-model="form.notifyUrlScrm" placeholder="请输入回调地址notifyUrlScrm" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="redPacketOpen.title" :visible.sync="redPacketOpen.open" width="600px" append-to-body>
      <el-form ref="redPacketOpen" :model="redPacketOpen" label-width="110px">
        <el-form-item label="商户号" prop="cateId">
          <el-select v-model="redPacketOpen.newChangeMchId" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in moreList"
              :key="dict.mchId"
              :label="dict.mchId"
              :value="dict.mchId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormChangeMchId">确 定</el-button>
        <el-button @click="cancelChangeMchId">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMore,
  getMore,
  delMore,
  addMore,
  updateMore,
  getRedPacketMchId,
  updateChangeMchId
} from '@/api/his/redPacketConfig'

export default {
  name: "redPacketConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      redPacketMchId: null,
      redPacketOpen:{
        open:false,
        title:null,
        oldChangeMchId:null,
        newChangeMchId:null,
      },
      // 多商户配置表格数据
      moreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isNew: null,
        appId: null,
        miniappId: null,
        mchId: null,
        mchKey: null,
        keyPath: null,
        privateKeyPath: null,
        privateCertPath: null,
        apiV3Key: null,
        publicKeyId: null,
        publicKeyPath: null,
        notifyUrl: null,
        notifyUrlScrm: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      redPacketOpenRule:{
        newChangeMchId:[{ required: true, trigger: "blur", message: "商户号不能为空" }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询多商户配置列表 */
    getList() {
      this.loading = true;
      listMore(this.queryParams).then(response => {
        console.log("response.rows",response.rows)
        this.moreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getRedPacketMchId().then(res=>{
        this.redPacketMchId=res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelChangeMchId() {
      this.redPacketOpen.open=false;
      this.redPacketOpen.title=false;
      this.redPacketOpen.oldChangeMchId=false;
      this.redPacketOpen.newChangeMchId=false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        isNew: null,
        appId: null,
        miniappId: null,
        mchId: null,
        mchKey: null,
        keyPath: null,
        privateKeyPath: null,
        privateCertPath: null,
        apiV3Key: null,
        publicKeyId: null,
        publicKeyPath: null,
        notifyUrl: null,
        notifyUrlScrm: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加多商户配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMore(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改多商户配置";
      });
    },
    handleEditRedPacket(){
      this.redPacketOpen.open= true;
      this.redPacketOpen.title="修改发送红包的商户号";

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMore(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMore(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交修改发送红包的商户号- */
    submitFormChangeMchId() {
      this.$refs["redPacketOpen"].validate(valid => {
        if (valid) {
            this.redPacketOpen.oldChangeMchId=this.redPacketMchId;
            if (this.redPacketOpen.newChangeMchId==null){
              return this.$message.error("修改的商户号不能为空")
            }

            updateChangeMchId(this.redPacketOpen).then(response => {
              this.msgSuccess("修改成功");
              this.cancelChangeMchId();
              this.getList();
            });

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除多商户配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMore(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>
