<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="渠道key" prop="channelKey">
        <el-input
          v-model="queryParams.channelKey"
          placeholder="请输入渠道key"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道费率" prop="rate">
        <el-input
          v-model="queryParams.rate"
          placeholder="请输入渠道费率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="logo" prop="logoUrl">
        <el-input
          v-model="queryParams.logoUrl"
          placeholder="请输入logo"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款公司名" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入付款公司名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付id" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入支付id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付秘钥" prop="appSecret">
        <el-input
          v-model="queryParams.appSecret"
          placeholder="请输入支付秘钥"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第三方支付url" prop="thirdUrl">
        <el-input
          v-model="queryParams.thirdUrl"
          placeholder="请输入第三方支付url"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书地址" prop="certPath">
        <el-input
          v-model="queryParams.certPath"
          placeholder="请输入证书地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阿里公钥证书地址" prop="aliPublicCert">
        <el-input
          v-model="queryParams.aliPublicCert"
          placeholder="请输入阿里公钥证书地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阿里应用公钥证书地址" prop="aliApplyCert">
        <el-input
          v-model="queryParams.aliApplyCert"
          placeholder="请输入阿里应用公钥证书地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阿里ca根证书地址" prop="aliCaCert">
        <el-input
          v-model="queryParams.aliCaCert"
          placeholder="请输入阿里ca根证书地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户号" prop="merchantNo">
        <el-input
          v-model="queryParams.merchantNo"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="api秘钥" prop="apiSecret">
        <el-input
          v-model="queryParams.apiSecret"
          placeholder="请输入api秘钥"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="orderno">
        <el-input
          v-model="queryParams.orderno"
          placeholder="请输入排序"
          clearable
          @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="['business:channel: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="['business:channel: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="['business:channel:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:channel:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="渠道类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_user_cash_account" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="提现渠道(四方产品id)" align="center" prop="channelKey" />
      <el-table-column label="充值渠道(四方产品id)" align="center" prop="channelNo" />
      <el-table-column label="充值手续费" align="center" prop="rate">
        <template slot-scope="scope">
          {{(scope.row.rate * 100).toFixed(2) + "%"}}
        </template>
      </el-table-column>
      <el-table-column label="提现手续费" align="center" prop="withdrawRate">
        <template slot-scope="scope">
          {{(scope.row.withdrawRate * 100).toFixed(2) + "%"}}
        </template>
      </el-table-column>
      <el-table-column label="是否开启" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_channel_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="logo" align="center" prop="logoUrl" />
      <el-table-column label="付款公司名" align="center" prop="company" />
      <el-table-column label="支付id" align="center" prop="appId" />
      <el-table-column label="支付秘钥" align="center" prop="appSecret" />
      <el-table-column label="第三方支付url" align="center" prop="thirdUrl" />
      <el-table-column label="是否开启 0：未开启  1：开启" align="center" prop="status" />
      <el-table-column label="证书地址" align="center" prop="certPath" />
      <el-table-column label="阿里公钥证书地址" align="center" prop="aliPublicCert" />
      <el-table-column label="阿里应用公钥证书地址" align="center" prop="aliApplyCert" />
      <el-table-column label="阿里ca根证书地址" align="center" prop="aliCaCert" />
      <el-table-column label="商户号" align="center" prop="merchantNo" />
      <el-table-column label="api秘钥" align="center" prop="apiSecret" />-->
      <el-table-column label="排序" align="center" prop="orderno" />
      <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="['business:channel:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:channel: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="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="30%" lable-position="left">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="渠道类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.app_user_cash_account"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提现渠道(四方产品id)" prop="channelKey">
          <el-input v-model="form.channelKey" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="充值渠道(四方产品id)" prop="channelNo">
          <el-input v-model="form.channelNo" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="充值手续费（%）" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入充值手续费" />
        </el-form-item>
        <el-form-item label="提现手续费（%）" prop="withdrawRate">
          <el-input v-model="form.withdrawRate" placeholder="请输入提现手续费" />
        </el-form-item>
        <el-form-item label="是否开启" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.app_channel_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="logo" prop="logoUrl">
          <el-input v-model="form.logoUrl" placeholder="请输入logo" />
        </el-form-item>
        <el-form-item label="付款公司名" prop="company">
          <el-input v-model="form.company" placeholder="请输入付款公司名" />
        </el-form-item>
        <el-form-item label="支付id" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入支付id" />
        </el-form-item>
        <el-form-item label="支付秘钥" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入支付秘钥" />
        </el-form-item>
        <el-form-item label="第三方支付url" prop="thirdUrl">
          <el-input v-model="form.thirdUrl" placeholder="请输入第三方支付url" />
        </el-form-item>
        <el-form-item label="证书地址" prop="certPath">
          <el-input v-model="form.certPath" placeholder="请输入证书地址" />
        </el-form-item>
        <el-form-item label="阿里公钥证书地址" prop="aliPublicCert">
          <el-input v-model="form.aliPublicCert" placeholder="请输入阿里公钥证书地址" />
        </el-form-item>
        <el-form-item label="阿里应用公钥证书地址" prop="aliApplyCert">
          <el-input v-model="form.aliApplyCert" placeholder="请输入阿里应用公钥证书地址" />
        </el-form-item>
        <el-form-item label="阿里ca根证书地址" prop="aliCaCert">
          <el-input v-model="form.aliCaCert" placeholder="请输入阿里ca根证书地址" />
        </el-form-item>
        <el-form-item label="商户号" prop="merchantNo">
          <el-input v-model="form.merchantNo" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="api秘钥" prop="apiSecret">
          <el-input v-model="form.apiSecret" placeholder="请输入api秘钥" />
        </el-form-item>-->
        <el-form-item label="排序" prop="orderno">
          <el-input v-model="form.orderno" placeholder="请输入排序" />
        </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 { listChannel, getChannel, delChannel, addChannel, updateChannel } from "@/api/business/channel";

export default {
  name: "Channel",
  dicts: ['app_channel_status','app_user_cash_account'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值渠道表格数据
      channelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        channelKey: null,
        channelNo: null,
        rate: null,
        logoUrl: null,
        company: null,
        appId: null,
        appSecret: null,
        thirdUrl: null,
        status: null,
        certPath: null,
        aliPublicCert: null,
        aliApplyCert: null,
        aliCaCert: null,
        merchantNo: null,
        apiSecret: null,
        orderno: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "请选择渠道类型", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询充值渠道列表 */
    getList() {
      this.loading = true;
      listChannel(this.queryParams).then(response => {
        this.channelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        channelKey: null,
        channelNo: null,
        rate: null,
        withdrawRate: null,
        logoUrl: null,
        company: null,
        appId: null,
        appSecret: null,
        thirdUrl: null,
        status: null,
        certPath: null,
        aliPublicCert: null,
        aliApplyCert: null,
        aliCaCert: null,
        merchantNo: null,
        apiSecret: null,
        orderno: 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
      getChannel(id).then(response => {
        this.form = response.data;
        if(this.form.rate){
          this.form.rate = this.form.rate * 100;
        }
        if(this.form.withdrawRate){
          this.form.withdrawRate = this.form.withdrawRate * 100;
        }
        this.open = true;
        this.title = "修改充值渠道";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChannel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChannel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值渠道编号为"' + ids + '"的数据项？').then(function() {
        return delChannel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/channel/export', {
        ...this.queryParams
      }, `channel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
