<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.bonusgroupbill_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button type="text" size="small" icon="el-icon-zoom-in" @click="showPayOrder(row)">发起记录</el-button>
      </template>
      <template slot="bonusStatus" slot-scope="{row}">
        <el-tag v-if="row.bonusStatus === 'REQUEST_RECEIVE'" type="warning">请求已接收</el-tag>
        <el-tag v-if="row.bonusStatus === 'REQUEST_ACCEPT'" type="warning">请求已受理</el-tag>
        <el-tag v-if="row.bonusStatus === 'REMITING'" type="warning">银行处理中</el-tag>
        <el-tag v-if="row.bonusStatus === 'FAIL'" type="danger">失败</el-tag>
        <el-tag v-if="row.bonusStatus === 'SUCCESS'" type="success">成功</el-tag>
        <el-tag v-if="row.bonusStatus === 'ERROR'" type="danger">系统业务异常</el-tag>
        <el-tag v-if="row.bonusStatus === 'WAITING'" type="primary">待发起</el-tag>
      </template>
      <template slot="accountIsDefault" slot-scope="{row}">
        <el-tag v-if="row.accountIsDefault === 1" type="success">是</el-tag>
        <el-tag v-if="row.accountIsDefault === 0" type="warning">否</el-tag>
      </template>
    </avue-crud>
    <el-dialog :modal-append-to-body="false" :visible.sync="bonusPayOrderVisible" title="发起记录" width="80%" center top="2vh" modal="true" @close="closeAccountDialog">
      <bonusbill :biz-id="billId" v-if="bonusPayOrderVisible"></bonusbill>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/bonus/bonusgroupbill";
  import {mapGetters} from "vuex";
  import bonusbill from "../billmng/bonusbill.vue";

  export default {
    components: {
      bonusbill
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        bonusPayOrderVisible: false,
        billId: null,
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "分润日期",
              prop: "bonusDate",
              type: "date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              search: true,
            },
            {
              label: "分润金额（元）",
              prop: "bonusAmt",
              labelWidth: 120
            },
            {
              label: "业务场景",
              prop: "bonusSceneName",
            },
            {
              label: "所属分润商户",
              prop: "bonusGroupName",
              labelWidth: 110
            },
            {
              label: "所属分润策略",
              prop: "bonusStrategyName",
              labelWidth: 110
            },
            {
              label: "收款账户名",
              prop: "accountReceiverName",
              search: true,
              searchLabelWidth: 110,
            },
            {
              label: "收款账户号码",
              prop: "receiverAccountNo",
              hide: true,
              labelWidth: 110
            },
            {
              label: "开户行编号",
              prop: "receiverBankCode",
              hide: true,
            },
            {
              label: "账户类型",
              prop: "bankAccountType",
              hide: true,
              type: "select",
              dicData:[
                {
                  label:"借记卡",
                  value: "DEBIT_CARD"
                },
                {
                  label:"贷记卡",
                  value: "CREDIT_CARD"
                },
                {
                  label:"准贷卡",
                  value: "QUASI_CREDIT_CARD"
                },
                {
                  label:"存折",
                  value: "PASSBOOK"
                },
                {
                  label:"单位结算卡",
                  value: "UNIT_SETTLE_CARD"
                },
                {
                  label:"对公卡",
                  value: "PUBLIC_CARD"
                },
              ]
            },
            {
              label: "分润比例",
              prop: "rate",
              hide: true,
            },
            {
              label: "分润状态",
              prop: "bonusStatus",
              type: "select",
              search: true,
              dicData:[
                {
                  label:"成功",
                  value: "SUCCESS"
                },
                {
                  label:"失败",
                  value: "FAIL"
                },
                {
                  label:"待发起",
                  value: "WAITING"
                },
                {
                  label:"请求已接收",
                  value:"REQUEST_RECEIVE"
                },
                {
                  label:"请求已受理",
                  value: "REQUEST_ACCEPT"
                },
                {
                  label:"银行处理中",
                  value: "REMITING"
                },
                {
                  label:"系统业务异常",
                  value: "ERROR"
                },
              ]
            },
            {
              label: "是否平台账户",
              prop: "accountIsDefault",
              type: "select",
              labelWidth: 110,
              dicData:[
                {
                  label:"否",
                  value: 0
                },
                {
                  label:"是",
                  value: 1
                }
              ]
            },
            {
              label: "更新时间",
              prop: "updateTime",
            }
          ],
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.bonusgroupbill_add, false),
          viewBtn: this.vaildData(this.permission.bonusgroupbill_view, false),
          delBtn: this.vaildData(this.permission.bonusgroupbill_delete, false),
          editBtn: this.vaildData(this.permission.bonusgroupbill_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      showPayOrder(row){
        this.bonusPayOrderVisible = true;
        this.billId = row.id;
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.bonusSceneId = this.bonusSceneId;
        params.bonusGroupId = this.bonusGroupId;
        params.date = this.date;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
