<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="detailNo">
        <el-input
          v-model="queryParams.detailNo"
          placeholder="请输入转账明细单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转账编号" prop="transferBatchNo" v-if="notHasView(views.transfer)">
        <el-input
          v-model="queryParams.transferBatchNo"
          placeholder="请输入转账批次ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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_detail_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="转账备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入转账备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="accountNo">
        <el-input
          v-model="queryParams.accountNo"
          placeholder="请输入收款用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入收款用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失败原因" prop="failReason">
        <el-input
          v-model="queryParams.failReason"
          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:transferDetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferDetailList" @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 === 'detailId'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'status'">
              <dict-tag :options="dict.type.transfer_detail_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'transferBatchNo'">
              <transfer-link :id="d.row.transferId" :text="d.row.transferBatchNo"/>
            </template>
            <template v-else-if="column.key === 'amount'">
              {{d.row.amount | money}} 元
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>

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

<script>
import { listTransferDetail, getTransferDetail, delTransferDetail, addTransferDetail, updateTransferDetail } from "@/api/ss/transferDetail";
import { $showColumns, $view } from '@/utils/mixins'
import TransferLink from '@/components/Business/Transfer/TransferLink.vue'

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

export default {
  name: "TransferDetail",
  components: { TransferLink },
  mixins: [$showColumns, $view],
  dicts: ['transfer_detail_status'],
  props: {
    query: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      // 字段列表
      columns: [
        {key: 'detailId', visible: false, label: '明细ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'detailNo', visible: true, label: '明细单号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'transferBatchNo', visible: true, 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: 'amount', visible: true, label: '转账金额', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'remark', visible: true, label: '转账备注', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'accountNo', visible: true, label: '收款用户账号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'userName', visible: true, label: '收款用户姓名', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'failReason', visible: true, label: '失败原因', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 转账明细表格数据
      transferDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        detailId: null,
        detailNo: null,
        transferId: null,
        status: null,
        amount: null,
        remark: null,
        accountNo: null,
        userName: null,
        failReason: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        detailNo: [
          { required: true, message: "转账明细单号不能为空", trigger: "blur" }
        ],
        transferId: [
          { required: true, message: "转账批次ID不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "明细状态不能为空", trigger: "change" }
        ],
        amount: [
          { required: true, message: "转账金额不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "转账备注不能为空", trigger: "blur" }
        ],
        accountNo: [
          { required: true, message: "收款用户账号不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query
    }
    this.getList();
  },
  methods: {
    /** 当排序按钮被点击时触发 **/
    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;
      listTransferDetail(this.queryParams).then(response => {
        this.transferDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        detailId: null,
        detailNo: null,
        transferId: null,
        status: null,
        amount: null,
        remark: null,
        accountNo: null,
        userName: null,
        createTime: null,
        failReason: 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.detailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加转账明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const detailId = row.detailId || this.ids
      getTransferDetail(detailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改转账明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.detailId != null) {
            updateTransferDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransferDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const detailIds = row.detailId || this.ids;
      this.$modal.confirm('是否确认删除转账明细编号为"' + detailIds + '"的数据项？').then(function() {
        return delTransferDetail(detailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/transferDetail/export', {
        ...this.queryParams
      }, `transferDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
