<template>
  <basic-container>
    <el-dialog  :modal-append-to-body="false" :visible.sync="debitDetailVisible" title="分佣明细" width="70%">
      <debit ref="debit" :requestId="selectRequestId"></debit>
    </el-dialog>
    <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"
               :upload-after="uploadAfter">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-goods"
                   plain

                   @click="toBin">卡bin查询
        </el-button>
      </template>
      <template slot="menu" slot-scope="scope">
        <el-button type="text"
                   size="small"
                   icon="el-icon-zoom-in"
                   @click="handleDetail(scope.row)">分佣明细
        </el-button>
      </template>
      <template slot="demoDownloadForm">

        <el-button type="primary" @click="handleTemplate">
          模板下载
        </el-button>
      </template>
      <template slot="attachUrl" slot-scope="scope">

        <el-link :href="scope.row.attachUrl" type="primary">
          {{scope.row.attachName}}
        </el-link>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/guosen/bussinessdebitrequest";
  import {mapGetters} from "vuex";
  import {getToken} from '@/util/auth';
  import debit from "@/views/guosen/debit"

  export default {
    components:{
      debit
    },
    data() {
      return {
        debitDetailVisible:false,
        selectRequestId : 0,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          saveBtn: false,
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          delBtn: false,
          editBtn: false,
          viewBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "采购商id",
              prop: "bussinessId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入采购商id",
                trigger: "blur"
              }]
            },
            {
              label: "附件",
              prop: "attachUrl",
              slot: true,
              span: 24,
              drag: true,
              type: "upload",
              tip: '只能上传指定模板的excel表格文件,请先下载模板并填充数据后上传',
              action: "/api/cyzh-guosen/bussinessdebitrequest/import-excel",
              rules: [{
                required: true,
                message: "请输入提交附件url",
                trigger: "blur"
              }]
            },
            {
              label: "",
              prop: "demoDownload",
              span: 24,
              slot: true,
              hide: true,
            },
            {
              label: "订单总笔数",
              prop: "orderCount",
              display: false,
              rules: [{
                required: true,
                message: "请输入订单总笔数",
                trigger: "blur"
              }]
            },
            {
              label: "成功的订单笔数",
              prop: "successOrderCount",
              display: false,
              rules: [{
                required: true,
                message: "请输入成功的订单笔数",
                trigger: "blur"
              }]
            },
            {
              label: "失败的订单笔数",
              prop: "failOrderCount",
              display: false,
              rules: [{
                required: true,
                message: "请输入失败的订单笔数",
                trigger: "blur"
              }]
            },
            {
              label: "手续费",
              prop: "fee",
              display: false,
              rules: [{
                required: true,
                message: "请输入手续费",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createTime",
              display: false,
              rules: [{
                required: true,
                message: "请输入手续费",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.bussinessdebitrequest_add, false),
          viewBtn: this.vaildData(this.permission.bussinessdebitrequest_view, false),
          delBtn: this.vaildData(this.permission.bussinessdebitrequest_delete, false),
          editBtn: this.vaildData(this.permission.bussinessdebitrequest_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      toBin() {
        this.$router.push('/guosen/bankcode');
      },
      uploadAfter(res, done, loading,column){
        done()
         this.$refs.crud.closeDialog()
         this.onLoad(this.page,{})
       // this.refreshChange()


      },
      handleDetail(row){
        this.debitDetailVisible = true;
        this.selectRequestId = row.id;
        this.$nextTick(()=>{
          this.$refs.debit.onLoad({current:1,size:10},{requestId:row.id})
        })

      },
      handleTemplate(){
         window.open(`/api/cyzh-guosen/bussinessdebitrequest/export-template?${this.website.tokenHeader}=${getToken()}`);
      },
      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;
        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>
