/**
* @Author: kk
*
*/

<template>
  <div style="margin-top:50px;margin-left:80px">
    <div align="left">
      <h3>放款渠道为：{{ this.ruleForm.payChannelType }}</h3>
      <br />
      <br />
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="渠道选择：" prop="payChannelApi">
          <el-radio-group v-model="ruleForm.payChannelApi" @change="onRadioChange">
            <el-radio
              v-for="(item,index) in operat"
              :key="index"
              :label="item.payChannelApi"
            >{{ item.channelName }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <br />
        <el-popover placement="top" width="160" v-model="visible2">
          <p>确认操作？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visible2 = false">取消</el-button>
            <el-button type="primary" size="mini" @click="passChannelMarket">确定</el-button>
          </div>
          <el-button slot="reference" type="primary" v-if="passDisable" disabled>确定切换</el-button>
          <el-button slot="reference" type="primary" v-else>确定切换</el-button>
        </el-popover>
      </div>
    </div>
    <el-divider></el-divider>

    <div align="left">
      <h3>扣款渠道为：{{ this.ruleFormProtocol.payChannelType }}</h3>
      <br />
      <br />
      <el-form
        :model="ruleFormProtocol"
        ref="ruleFormProtocol"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="渠道选择：" prop="payChannelApi">
          <el-radio-group v-model="ruleFormProtocol.payChannelApi" @change="onRadioChangeProtocol">
            <el-radio
              v-for="(item,indexp) in operatProtocol"
              :key="indexp"
              :label="item.payChannelApi"
            >{{ item.channelName }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <h2 style="color:red">影响较大，请慎重操作</h2>
        <br />
        <el-popover placement="top" width="160" v-model="visibleProtocol">
          <p>确认操作？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visibleProtocol = false">取消</el-button>
            <el-button type="primary" size="mini" @click="passChannelMarketProtocol">确定</el-button>
          </div>
          <el-button slot="reference" type="primary" v-if="passDisableProtocol" disabled>确定切换</el-button>
          <el-button slot="reference" type="primary" v-else>确定切换</el-button>
        </el-popover>
      </div>
    </div>
  </div>
</template>


<script>
import * as moment from "moment";

export default {
  name: "PassMoneyMarket",
  components: {},
  data() {
    return {
      queryModel: {
        type: Object
      },
      ruleForm: {
        payChannelApi: "",
        payChannelType: ""
      },
      ruleFormProtocol: {
        payChannelApi: "",
        payChannelType: ""
      },
      visible2: false,
      visibleProtocol: false,
      operat: [],
      operatProtocol: [],
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      passDisable: true,
      passDisableProtocol: true,
      rules: {
        loginName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    /** 加载放款渠道有那些 */
    passChannelMarket() {
      this.$$post(`/config/updatePayChannel`, {
        status: "YES",
        payChannelType: "WITHDRAW",
        payChannelApi: this.ruleForm.payChannelApi
      }).then(res => {
        if (res.data.code < 1) {
          this.$message.error(res.data.msg);
          return;
        }
        this.$message({ message: "操作成功", type: "success" });
        this.handleClose();
        window.location.reload();
      });
    },
    loadOperate() {
      this.$$post(`/config/getCnlRelationList`, {
        payChannelType: "WITHDRAW"
      }).then(res => {
        if (res.data.code == 1) {
            var arr = [];
            res.data.data.forEach((item, index) => {
                if (
                item.payChannelApi !== "FUIOU_WD" &&
                item.payChannelApi !== "CHANPAY_WD" 
                ) {
                arr.push(item);
                }
                arr.forEach((item, index) => {
                    if (item.status == "YES") {
                    this.ruleForm.payChannelType = item.payChannelApi;
                    this.ruleForm.payChannelApi = item.payChannelApi;
                    }
                });
                this.operat = arr;
          });
        } else {
          this.$message({ message: res.data.msg, type: "error" });
        }
      });
    },

    onRadioChange(val) {
      // 当radio已被选中时，这个事件不会被触发
      if (val == this.ruleForm.payChannelType) {
        this.passDisable = true;
      } else {
        this.passDisable = false;
      }
    },

    /** 加载扣款渠道有那些 */
    passChannelMarketProtocol() {
      this.$$post(`/config/updatePayChannel`, {
        status: "YES",
        payChannelType: "DEDUCT",
        payChannelApi: this.ruleFormProtocol.payChannelApi
      }).then(res => {
        if (res.data.code < 1) {
          this.$message.error(res.data.msg);
          return;
        }
        this.$message({ message: "操作成功", type: "success" });
        this.handleClose();
        window.location.reload();
      });
    },
    loadOperateProtocol() {
      this.$$post(`/config/getCnlRelationList`, {
        payChannelType: "DEDUCT"
      }).then(res => {
        if (res.data.code == 1) {
          var arr = [];
          res.data.data.forEach((item, index) => {
            if (
              item.payChannelApi !== "FUIOU_PROTOCOL" &&
              item.payChannelApi !== "CHANPAY_PROTOCOL" &&
              item.payChannelApi !== "ZHENTONGCF_PROTOCOL"
            ) {
              arr.push(item);
            }
          });

          arr.forEach((item, index) => {
            if (item.status == "YES") {
              this.ruleFormProtocol.payChannelType = item.payChannelApi;
              this.ruleFormProtocol.payChannelApi = item.payChannelApi;
            }
          });

          this.operatProtocol = arr;
        } else {
          this.$message({ message: res.data.msg, type: "error" });
        }
      });
    },

    onRadioChangeProtocol(val) {
      // 当radio已被选中时，这个事件不会被触发
      if (val == this.ruleFormProtocol.payChannelType) {
        this.passDisableProtocol = true;
      } else {
        this.passDisableProtocol = false;
      }
    },
    dateFormat: function(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    /** 关闭处理 */
    handleClose() {
      this.visible = false;
      setTimeout(() => {
        this.$emit("update:dialogVisible", false);
      }, 300);
    }
  },
  mounted: function() {
    this.loadOperate();
    // 扣款渠道
    this.loadOperateProtocol();
  }
};
</script>

