<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="warning"
                   size="small"
                   v-if="permission.paybill_payStat && data.length > 0"
                   @click="handleStat">代付统计
        </el-button>
        <span span style="color: red; font-weight:bold">{{payStat}}</span>
      </template>
      <template slot="payStatus" slot-scope="{row}">
        <el-tag type="success" v-if="row.payStatus === 'SUCCESS'">成功</el-tag>
        <el-tag type="warning" v-if="row.payStatus === 'REQUEST_RECEIVE'">请求已接收</el-tag>
        <el-tag type="warning" v-if="row.payStatus === 'REQUEST_ACCEPT'">请求已受理</el-tag>
        <el-tag type="warning" v-if="row.payStatus === 'REMITING'">银行处理中</el-tag>
        <el-tag type="danger" v-if="row.payStatus === 'FAIL'">失败</el-tag>
        <el-tag type="danger" v-if="row.payStatus === 'ERROR'">系统异常</el-tag>
        <el-tag type="primary" v-if="row.payStatus === 'WAITING'">待发起</el-tag>
      </template>
      <template slot="bizType" slot-scope="{row}">
        <el-tag type="primary" v-if="row.bizType === 1">商户代付</el-tag>
        <el-tag type="success" v-if="row.bizType === 2">平台分润</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, payStat} from "@/api/bussiness/bussinesspayorder";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        payStat: "",
        selectionList: [],
        option: {
          height:'auto',
          menuWidth: 120,
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "商户请求号",
              prop: "requestNo",
              hide: true
            },
            {
              label: "采购商订单号",
              prop: "bussinessOrderId",
              width: 200,
              labelWidth: 100,
            },
            {
              label: "采购商",
              prop: "bussinessId",
              type: "select",
              width: 200,
              searchFilterable: true,
              dicUrl: "/api/cyzh-guosen/bussiness/listBussiness?size=500",
              search: true,
              props:{
                label: "name",
                value: "id"
              },
            },
            {
              label: "付款金额",
              prop: "orderAmount",
            },
            // {
            //   label: "渠道手续费",
            //   prop: "channelFee",
            // },
            {
              label: "付款状态",
              prop: "payStatus",
              type: "select",
              dicData:[
                {
                  label:"成功",
                  value:"SUCCESS"
                },
                {
                  label:"失败",
                  value:"FAIL"
                },
                {
                  label:"待发起",
                  value:"WAITING"
                },
                {
                  label:"系统异常",
                  value:"ERROR"
                },
                {
                  label:"银行处理中",
                  value: "REMITING"
                },
                {
                  label:"请求已受理",
                  value: "REQUEST_ACCEPT"
                },
                {
                  label:"请求已接收",
                  value: "REQUEST_RECEIVE"
                }
              ],
              search:true,
            },
            {
              label: "业务类型",
              prop: "bizType",
              slot: true,
              width: 100
            },
            {
              label: "收款方开户名",
              prop: "receiverAccountName",
              labelWidth: 100
            },
            {
              label: "收款方帐户号",
              prop: "receiverAccountNo",
              labelWidth: 100,
              width: 150
            },
            {
              label: "收款方开户行",
              prop: "receiverBankCode",
              labelWidth: 100,
              hide: true
            },
            {
              label: "账户类型",
              prop: "bankAccountType",
              hide:true
            },
            {
              label: "支行编号",
              prop: "branchBankCode",
              hide:true
            },
            {
              label: "失败原因",
              prop: "failReason",
            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "date",
              search: true,
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              width: 140
            },
            {
              label: "更新时间",
              prop: "updateTime",
              type: "date",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              width: 140
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.bussinesspayorder_add, false),
          viewBtn: this.vaildData(this.permission.paybill_view, false),
          delBtn: this.vaildData(this.permission.bussinesspayorder_delete, false),
          editBtn: this.vaildData(this.permission.bussinesspayorder_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleStat(){
        this.query.bizType = 1;
        payStat(this.query).then(res =>{
          this.payStat = "¥" + res.data.data;
        })
      },
      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.payStat = "";
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.payStat = "";
        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.bizType = 1;
        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>
