<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="76px" class="flex form"
      style="padding: 0 20px; position: relative">
      <el-form-item>
        <div class="searchTop flexBet" style="align-items: center">
          <el-row :gutter="10" class="mb8" style="margin-left: 20px">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="" size="medium" @click="handleAdd()">绑定通道</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form-item>
    </el-form>
    <el-table   v-loading="loading" :data="infoList">
      <el-table-column label="通道名称" align="center" prop="dataStatus">
        <template slot-scope="scope">
          {{ scope.row.platformChannel.channelName }}
        </template>
      </el-table-column>

      <el-table-column label="类型" align="center" prop="channelTypeDescribe" />

      <el-table-column label="费率" align="center" prop="dataStatus">
        <template slot-scope="scope">
          {{ scope.row.rate + "%" + '+' + NumberDiv(scope.row.singleFee, 100),
                    }}
        </template>
      </el-table-column>

      <el-table-column label="扣费方式" align="center" prop="channelType">
        <template slot-scope="scope">
          {{ scope.row.platformChannel.channelType == 1 ? '内扣' : '外扣' }}
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status" min-width="140">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-text="开" inactive-text="关" :active-value="1" :inactive-value="2"
            @change="switchStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="200">
        <template slot-scope="scope">
          <el-button size="small" type="text"  @click="handleUpdate(scope.row)" style="  ">费率</el-button>
          <el-button size="small" type="text" @click="handleDelete(scope.row)" style="color: red">删除</el-button>
        </template>
      </el-table-column>

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

    <!-- 添加或修改对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="650px" append-to-body :close-on-click-modal="false"
      :before-close="cancel">
      <el-form ref="formChannel" :model="form" :rules="rules" label-width="108px">
        <el-form-item label="通道" prop="cid">
          <el-select v-model="form.cid" placeholder="请选择通道" clearable size="medium">
            <el-option v-for="(item, i) in channelAll" :key="i" :label="item.channelName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formChannel')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 费率 -->
    <el-dialog :title="title" :visible.sync="openRate" width="500px" append-to-body>
      <el-form ref="formRate" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="规则" prop="rateRuleType">
          <el-select v-model="form.rateRuleType" placeholder="请选择规则" clearable size="medium" @change="getValue">
            <el-option v-for="(item, i) in withdrawRule" :key="i" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单笔比例" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入单笔提款比例" style="width:300px" @input="change"
            :disabled="form.rateRuleType == 2" /> %
        </el-form-item>
        <el-form-item label="单笔收取" prop="singleFee">
          <el-input v-model="form.singleFee" placeholder="请输入单笔提款收取" style="width:300px" @input="change"
            :disabled="form.rateRuleType == 2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formRate')">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { payMerchant, common_api } from "@/api/form";
export default {
  name: "pkCouponScopeRangeStoreList",
  props: {
    pkCouponId: {
      type: Number,
      default() {
        return null;
      },
    },
    pkCouponStore: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      // 规则
      withdrawRule: [
        {
          label: "个人规则",
          value: 1,
        },
        {
          label: "系统规则",
          value: 2,
        },
      ],
      // 所有通道
      channelAll: [],
      dateRange: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openRate: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        pkCouponId: null,
      },
      // 表单参数
      form: {
      },
      detailData: {},
      // 表单校验
      rules: {
        rate: [{ required: true, message: "请输入单笔提款比例", trigger: "blur" }],
        singleFee: [{ required: true, message: "请输入单笔提款收取", trigger: "blur" }],
        cid: [
          { required: true, message: "请选择通道", trigger: "blur" },
        ]
      },
    };
  },
  created() {
    this.pkCouponId && (this.queryParams.pkCouponId = this.pkCouponId);
    this.getList();
    this.init();
  },
  methods: {
    change() {
      this.$forceUpdate()
    },
    getValue(e) {
      if (e == 2) {
        this.form.rate = this.detailData.platformChannel.channelRate
        this.form.singleFee = this.NumberDiv(this.detailData.platformChannel.channelSingleFee, 100)
      }
      if (e == 1) {
        this.form.rate = null
        this.form.singleFee = null
      }
      this.getList()
      this.$forceUpdate()
    },
    // 状态有效无效
    switchStatusChange(row) {
      const id = row.id || this.ids;
      this.form = row;
      this.form.status = row.status;
      payMerchant.upChannel(this.form).then((res) => {
        if (res.code === 200) {
          this.$message.success("编辑成功");
        }
      });
    },
    /** 查询 */
    getList() {
      this.loading = true;
      payMerchant.getChannelList(this.queryParams.pkCouponId)
        .then((response) => {
          this.infoList = response.data;
          this.loading = false;
        });
    },
    init() {
      // 所有通道
      common_api
        .platformChannelAll()
        .then((response) => {
          this.channelAll = response.data
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openRate = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "绑定通道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getList()
      this.detailData = row
      this.reset();
      this.form = row;
      this.form.rate = row.rate
      for (var i = 0; i < this.infoList.length; i++) {
        if (this.infoList[i].id == row.id) {
          this.form.singleFee = this.NumberDiv(this.infoList[i].singleFee, 100)
        }
      }
      // 系统规则赋值不同
      if (this.form.rateRuleType == 2) {
        this.form.rate = row.platformChannel.channelRate
        this.form.singleFee = this.NumberDiv(row.platformChannel.channelSingleFee, 100)
        for (var i = 0; i < this.infoList.length; i++) {
          if (this.infoList[i].id == row.id) {
            this.form.singleFee = this.NumberDiv(this.infoList[i].platformChannel.channelSingleFee, 100)
          }
        }
      }

      this.title = "费率";
      this.openRate = true;

    },
    /** 提交按钮 */
    submitForm(type) {
      this.$refs[type].validate((valid) => {
        if (valid) {
          let singleFee = 0;
          if (this.form.singleFee) {
            singleFee = this.NumberMul(this.form.singleFee, 100)
          }
          const loading = this.$loading({
            lock: true,
            text: `正在操作`,
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });

          if (type == 'formChannel') {
            payMerchant.bindChannel({
              cid: this.form.cid,
              mid: this.pkCouponId,
            }).then((response) => {
              loading.close();
              this.open = false;
              this.msgSuccess("绑定成功");
              this.getList();
            }).catch(() => {
              loading.close();
            });
          }
          if (type == 'formRate') {
            payMerchant.upChannel({
              platformChannelId: this.form.platformChannelId,
              id: this.form.id,
              rate: this.form.rate,
              singleFee: singleFee,
              merchantId: this.form.merchantId,
              rateRuleType: this.form.rateRuleType
            }).then((response) => {
              loading.close();
              this.openRate = false;
              this.msgSuccess("修改成功");
              this.getList();
            }).catch(() => {
              loading.close();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除通道名称为"' + row.platformChannel.channelName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return payMerchant.delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-radio {
  line-height: 2;
}
</style>
