<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="对账日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 220px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="支付渠道" prop="payChannel" >
        <el-select v-model="queryParams.payChannel" filterable placeholder="请选择支付渠道" clearable>
          <el-option
            v-for="item in payOptions"
            :key="item.channelId"
            :label="item.name"
            :value="item.channelId"
          ></el-option>
        </el-select>
      </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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:reconciliation2:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:reconciliation2:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:reconciliation2:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:reconciliation2:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reconciliation2List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="day" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.day, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" align="center" prop="payChannel" :formatter="formatPayChannel"  />
      <el-table-column label="总收入" align="center" prop="totalAmount" />
      <el-table-column label="订单支付" align="center" prop="orderPaid" />
      <el-table-column label="订单退款" align="center" prop="orderRefund" />
      <el-table-column label="押金抵扣" align="center" prop="deductionAmount" />
      <el-table-column label="订单盈余" align="center" prop="orderSurplus" />

      <el-table-column label="押金支付" align="center" prop="depositPaid" />
      <el-table-column label="押金退款" align="center" prop="depositRefund" />
      <el-table-column label="押金抵扣" align="center" prop="deductionAmount" />
      <el-table-column label="押金盈余" align="center" prop="depositSurplus" />
      <el-table-column label="手续费" align="center" prop="handlingCharge" />
      <el-table-column label="平台服务费" align="center" prop="platformServiceFee" />
      <el-table-column label="用户账变" align="center" prop="userReceipts" />
      <el-table-column label="结算金额" align="center">
        <template slot-scope="scope">
          <span class="bold-red">{{ scope.row.settlementAmount }}</span>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改平台对账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单支付" prop="orderPaid">
          <el-input v-model="form.orderPaid" placeholder="请输入订单支付" />
        </el-form-item>
        <el-form-item label="押金支付" prop="depositPaid">
          <el-input v-model="form.depositPaid" placeholder="请输入押金支付" />
        </el-form-item>
        <el-form-item label="支付渠道id" prop="payChannel">
          <el-input v-model="form.payChannel" placeholder="请输入支付渠道id" />
        </el-form-item>
        <el-form-item label="对账日期" prop="day">
          <el-date-picker clearable
            v-model="form.day"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择对账日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="押金退款" prop="depositRefund">
          <el-input v-model="form.depositRefund" placeholder="请输入押金退款" />
        </el-form-item>
        <el-form-item label="押金抵扣金额" prop="deductionAmount">
          <el-input v-model="form.deductionAmount" placeholder="请输入押金抵扣金额" />
        </el-form-item>
        <el-form-item label="手续费" prop="handlingCharge">
          <el-input v-model="form.handlingCharge" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="用户账变" prop="userReceipts">
          <el-input v-model="form.userReceipts" placeholder="请输入用户账变" />
        </el-form-item>
        <el-form-item label="结算金额" prop="settlementAmount">
          <el-input v-model="form.settlementAmount" placeholder="请输入结算金额" />
        </el-form-item>
        <el-form-item label="平台服务费" prop="platformServiceFee">
          <el-input v-model="form.platformServiceFee" placeholder="请输入平台服务费" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReconciliation2, getReconciliation2, delReconciliation2, addReconciliation2, updateReconciliation2 } from "@/api/system/reconciliation2";
import { listChannel } from '@/api/system/channel'

export default {
  name: "Reconciliation2",
  dicts: ['et_pay_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 日期范围
      dateRange: [],
      payOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台对账表格数据
      reconciliation2List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderPaid: null,
        depositPaid: null,
        payChannel: null,
        day: null,
        depositRefund: null,
        deductionAmount: null,
        handlingCharge: null,
        userReceipts: null,
        settlementAmount: null,
        platformServiceFee: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getPayChannelList();
    this.getList();

  },
  methods: {
    formatPayChannel(row, column, cellValue) {
      const payOption = this.payOptions.find(option => option.channelId === cellValue);
      return payOption ? payOption.name : '未知';
    },
    getPayChannelList(){
      listChannel({}).then(response => {
        this.payOptions = response.rows;
      });
    },
    /** 查询平台对账列表 */
    getList() {
      this.loading = true;
      listReconciliation2(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.reconciliation2List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reconciliationId: null,
        orderPaid: null,
        depositPaid: null,
        payChannel: null,
        createTime: null,
        day: null,
        depositRefund: null,
        deductionAmount: null,
        handlingCharge: null,
        userReceipts: null,
        settlementAmount: null,
        platformServiceFee: 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.reconciliationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台对账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reconciliationId = row.reconciliationId || this.ids
      getReconciliation2(reconciliationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台对账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reconciliationId != null) {
            updateReconciliation2(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReconciliation2(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reconciliationIds = row.reconciliationId || this.ids;
      this.$modal.confirm('是否确认删除平台对账编号为"' + reconciliationIds + '"的数据项？').then(function() {
        return delReconciliation2(reconciliationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reconciliation2/export', {
        ...this.queryParams
      }, `reconciliation2_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
  .bold-red {
    font-weight: bold;
    color: red;
  }
</style>
