<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="转账单号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入转账单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="bstType" v-if="notHasView(views.withdraw)">
        <el-select v-model="queryParams.bstType" placeholder="请选择业务类型" clearable>
          <el-option
            v-for="dict in dict.type.transfer_bst_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.transfer_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="批次名称" prop="batchName">
        <el-input
          v-model="queryParams.batchName"
          placeholder="请输入批次名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次备注" prop="batchRemark">
        <el-input
          v-model="queryParams.batchRemark"
          placeholder="请输入批次备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关闭原因" prop="closeReason">
        <el-input
          v-model="queryParams.closeReason"
          placeholder="请输入关闭原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ss:transfer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'batchId'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'bstType'">
              <dict-tag :options="dict.type.transfer_bst_type" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'status'">
              <dict-tag :options="dict.type.transfer_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'accountType'">
              <dict-tag :options="dict.type.account_type" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'totalAmount'">
              {{d.row.totalAmount | money}} 元
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['ss:transfer:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleRefresh(scope.row)"
            v-show="canRefresh(scope.row)"
            v-hasPermi="['ss:transfer:refresh']"
          >刷新结果</el-button>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import {
  listTransfer,
  getTransfer,
  delTransfer,
  addTransfer,
  updateTransfer,
  refreshTransferStatus
} from '@/api/ss/transfer'
import { $showColumns, $view } from '@/utils/mixins'
import { TransferStatus } from '@/utils/constants'

// 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "Transfer",
  mixins: [$showColumns, $view],
  dicts: ['transfer_status', 'transfer_bst_type', 'account_type'],
  props: {
    query: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    // 允许刷新结果的状态
    canRefresh() {
      return (row) => {
        return row.status === TransferStatus.TRANSFER_ING;
      }
    }
  },
  data() {
    return {
      // 字段列表
      columns: [
        {key: 'batchId', visible: false, label: '转账ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'batchNo', visible: true, label: '转账单号', minWidth: null, sortable: true, overflow: false, align: 'center', width: "180"},
        {key: 'accountType', visible: true, label: '转账账户类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'bstType', visible: true, label: '业务类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'bstId', visible: false, label: '业务编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'status', visible: true, label: '状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'batchName', visible: true, label: '名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'batchRemark', visible: true, label: '备注', minWidth: "200", sortable: true, overflow: true, align: 'center', width: null},
        {key: 'totalAmount', visible: true, label: '总金额', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'closeReason', visible: true, label: '关闭原因', minWidth: "100", sortable: true, overflow: true, align: 'center', width: null},
        {key: 'createTime', visible: true, label: '创建时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "180"},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 转账表格数据
      transferList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        batchId: null,
        batchNo: null,
        accountType: null,
        bstType: null,
        bstId: null,
        status: null,
        batchName: null,
        batchRemark: null,
        totalAmount: null,
        closeReason: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        batchNo: [
          { required: true, message: "批次单号不能为空", trigger: "blur" }
        ],
        accountType: [
          { required: true, message: "转账账户类型不能为空", trigger: "change" }
        ],
        bstType: [
          { required: true, message: "业务类型不能为空", trigger: "change" }
        ],
        bstId: [
          { required: true, message: "业务ID不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        batchName: [
          { required: true, message: "批次名称不能为空", trigger: "blur" }
        ],
        batchRemark: [
          { required: true, message: "批次备注不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "批次总金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query
    }
    this.getList();
  },
  methods: {
    handleRefresh(row) {
      this.loading = true;
      refreshTransferStatus(row.batchId).then(res => {
        if (res.code === 200) {
          this.$message.success("刷新成功");
        }
      }).finally(() => {
        this.getList();
      })
    },
    handleView(row) {
      this.$router.push(`/money/transfer/${row.batchId}`);
    },
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询转账列表 */
    getList() {
      this.loading = true;
      listTransfer(this.queryParams).then(response => {
        this.transferList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        batchId: null,
        batchNo: null,
        accountType: null,
        bstType: null,
        bstId: null,
        status: null,
        batchName: null,
        batchRemark: null,
        createTime: null,
        totalAmount: null,
        closeReason: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.batchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加转账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const batchId = row.batchId || this.ids
      getTransfer(batchId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改转账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.batchId != null) {
            updateTransfer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransfer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const batchIds = row.batchId || this.ids;
      this.$modal.confirm('是否确认删除转账编号为"' + batchIds + '"的数据项？').then(function() {
        return delTransfer(batchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/transfer/export', {
        ...this.queryParams
      }, `transfer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
