<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>
      <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" prop="tenantName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.tenantId" type="success" size="small">{{ scope.row.tenantName || scope.row.tenantId }}</el-tag>
          <el-tag v-else type="info" size="small">未分配</el-tag>
        </template>
      </el-table-column>
      <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-item label="分配租户" prop="tenantId">
          <el-select v-model="form.tenantId" placeholder="请选择要分配的租户（同步到租户库）" clearable style="width: 100%;">
            <el-option
              v-for="item in tenantList"
              :key="item.id"
              :label="item.tenantName + ' (' + item.tenantCode + ')'"
              :value="item.id"
            />
          </el-select>
          <div class="form-tip">选择租户后，该商户配置将同步到指定租户的数据库中</div>
        </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>

  </div>
</template>

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

export default {
  name: "redPacketConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 多商户配置表格数据
      moreList: [],
      // 租户列表
      tenantList: [],
      // 弹出层标题
      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: {},
    };
  },
  created() {
    this.getList();
    this.loadTenantList();
  },
  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;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    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,
        tenantId: null
      };
      this.resetForm("form");
    },
    // 加载租户列表
    loadTenantList() {
      getTenantList().then(res => {
        this.tenantList = res.data || [];
      });
    },
    /** 搜索按钮操作 */
    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 = "修改多商户配置";
      });
    },
    /** 提交按钮 */
    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();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    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>

<style scoped>
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
  line-height: 1.4;
}
</style>
