<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item :label="$t('business.deal.feeRecord.userId')" prop="userId" label-width="auto">
        <el-input v-model="queryParams.userId" :placeholder="$t('business.deal.feeRecord.userIdPlaceholder')" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('business.deal.feeRecord.relatedOrderId')" prop="relatedOrderId" label-width="auto">
        <el-input v-model="queryParams.relatedOrderId"
          :placeholder="$t('business.deal.feeRecord.relatedOrderIdPlaceholder')" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item :label="$t('business.deal.feeRecord.feeStandardId')" prop="feeStandardId">
        <el-input v-model="queryParams.feeStandardId"
          :placeholder="$t('business.deal.feeRecord.feeStandardIdPlaceholder')" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item :label="$t('business.deal.feeRecord.businessType')" prop="businessType" label-width="auto">
        <el-select v-model="queryParams.businessType"
          :placeholder="$t('business.deal.feeRecord.businessTypePlaceholder')" clearable>
          <el-option v-for="dict in dict.businessTypeOptions" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('business.deal.feeRecord.status')" prop="status" label-width="auto">
        <el-select v-model="queryParams.status" :placeholder="$t('business.deal.feeRecord.statusPlaceholder')"
          clearable>
          <el-option :label="$t('common.dict.enabled')" value="1" />
          <el-option :label="$t('common.dict.disabled')" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('business.deal.feeRecord.feeCollectionTime')" prop="feeCollectionTime" label-width="auto">
        <el-date-picker clearable v-model="queryParams.feeCollectionTime" type="date" value-format="yyyy-MM-dd"
          :placeholder="$t('business.deal.feeRecord.feeCollectionTimePlaceholder')">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('common.button.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset') }}</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="['business:feeRecord:add']">{{ $t('common.button.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="['business:feeRecord:edit']">{{ $t('common.button.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="['business:feeRecord:remove']">{{ $t('common.button.delete') }}
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:feeRecord:export']">{{ $t('common.button.export') }}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feeRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column :label="$t('business.deal.feeRecord.id')" align="center" prop="id" v-if="true" /> -->
      <el-table-column :label="$t('business.deal.feeRecord.relatedOrderId')" align="center" prop="relatedOrderId" />
      <el-table-column :label="$t('business.deal.feeRecord.businessType')" align="center" prop="businessType">
        <template slot-scope="scope">
          <span>{{ getBusinessTypeLabel(scope.row.businessType) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('business.deal.feeRecord.userId')" align="center" prop="userId" />
      <el-table-column :label="$t('business.deal.feeRecord.feeStandardId')" align="center" prop="feeStandardId" />
      <el-table-column :label="$t('business.deal.feeRecord.discountRuleId')" align="center" prop="discountRuleId" />
      <el-table-column :label="$t('business.deal.feeRecord.calculatedFeeAmount')" align="center"
        prop="calculatedFeeAmount" />
      <el-table-column :label="$t('business.deal.feeRecord.discountedFeeAmount')" align="center"
        prop="discountedFeeAmount" />
      <el-table-column :label="$t('business.deal.feeRecord.actualFeeAmount')" align="center" prop="actualFeeAmount" />
      <el-table-column :label="$t('business.deal.feeRecord.feeCollectionTime')" align="center" prop="feeCollectionTime"
        width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.feeCollectionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('business.deal.feeRecord.status')" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == '1' ? $t('common.dict.enabled') : $t('common.dict.disabled') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.tableMsg.operation')" align="center" width="200"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view"
            @click="handleDetail(scope.row)">{{ $t('common.button.detail') }}
          </el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:feeRecord:edit']">{{ $t('common.button.edit') }}
          </el-button> -->
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:feeRecord:remove']">{{ $t('common.button.delete') }}
          </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" />

    <!-- 添加或修改手续费记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('business.deal.feeRecord.relatedOrderId')" prop="relatedOrderId">
          <el-input v-model="form.relatedOrderId"
            :placeholder="$t('business.deal.feeRecord.relatedOrderIdPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.userId')" prop="userId">
          <el-input v-model="form.userId" :placeholder="$t('business.deal.feeRecord.userIdPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.feeStandardId')" prop="feeStandardId">
          <el-input v-model="form.feeStandardId"
            :placeholder="$t('business.deal.feeRecord.feeStandardIdPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.discountRuleId')" prop="discountRuleId">
          <el-input v-model="form.discountRuleId"
            :placeholder="$t('business.deal.feeRecord.discountRuleIdPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.calculatedFeeAmount')" prop="calculatedFeeAmount">
          <el-input v-model="form.calculatedFeeAmount"
            :placeholder="$t('business.deal.feeRecord.calculatedFeeAmountPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.discountedFeeAmount')" prop="discountedFeeAmount">
          <el-input v-model="form.discountedFeeAmount"
            :placeholder="$t('business.deal.feeRecord.discountedFeeAmountPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.actualFeeAmount')" prop="actualFeeAmount">
          <el-input v-model="form.actualFeeAmount"
            :placeholder="$t('business.deal.feeRecord.actualFeeAmountPlaceholder')" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('business.deal.feeRecord.businessType')" prop="businessType">
              <el-select v-model="form.businessType"
                :placeholder="$t('business.deal.feeRecord.businessTypePlaceholder')" clearable>
                <el-option v-for="dict in dict.businessTypeOptions" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('business.deal.feeRecord.status')" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="1">{{ $t('common.dict.enabled') }}</el-radio>
                <el-radio label="0">{{ $t('common.dict.disabled') }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item :label="$t('business.deal.feeRecord.feeCollectionTime')" prop="feeCollectionTime">
          <el-date-picker clearable v-model="form.feeCollectionTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            :placeholder="$t('business.deal.feeRecord.feeCollectionTimePlaceholder')">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{
            $t('common.button.confirm')
          }}
        </el-button>
        <el-button @click="cancel">{{ $t('common.button.cancel') }}</el-button>
      </div>
    </el-dialog>

    <!-- 手续费记录详情 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="auto">
        <el-form-item :label="$t('business.deal.feeRecord.businessType') + '：'">
          {{ getBusinessTypeLabel(form.businessType) }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.relatedOrderId') + '：'">
          {{ form.relatedOrderId }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.userId') + '：'">
          {{ form.userId }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.feeStandardId') + '：'">
          {{ form.feeStandardId }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.discountRuleId') + '：'">
          {{ form.discountRuleId }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.calculatedFeeAmount') + '：'">
          {{ form.calculatedFeeAmount }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.discountedFeeAmount') + '：'">
          {{ form.discountedFeeAmount }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.actualFeeAmount') + '：'">
          {{ form.actualFeeAmount }}
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.feeCollectionTime')" prop="feeCollectionTime">
          <el-date-picker clearable v-model="form.feeCollectionTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            :placeholder="$t('business.deal.feeRecord.feeCollectionTimePlaceholder')">
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('business.deal.feeRecord.status') + '：'">
          {{ form.status === '1' ? $t('common.dict.enabled') : $t('common.dict.disabled') }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t('common.button.close') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listFeeRecord,
    getFeeRecord,
    delFeeRecord,
    addFeeRecord,
    updateFeeRecord
  } from "@/api/business/feeRecord";

  export default {
    name: "FeeRecord",
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 字典数据
        dict: {
          businessTypeOptions: [{
              value: '1',
              label: this.$t('business.deal.feeRecord.businessTypeOption.transfer')
            },
            {
              value: '2',
              label: this.$t('business.deal.feeRecord.businessTypeOption.payment')
            },
            {
              value: '3',
              label: this.$t('business.deal.feeRecord.businessTypeOption.recharge')
            },
            {
              value: '4',
              label: this.$t('business.deal.feeRecord.businessTypeOption.withdraw')
            },
            {
              value: '5',
              label: this.$t('business.deal.feeRecord.businessTypeOption.collection')
            },
            {
              value: '6',
              label: this.$t('business.deal.feeRecord.businessTypeOption.paymentOut')
            }
          ]
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 手续费记录表格数据
        feeRecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示详情弹出层
        detailOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          relatedOrderId: undefined,
          businessType: undefined,
          userId: undefined,
          feeStandardId: undefined,
          discountRuleId: undefined,
          calculatedFeeAmount: undefined,
          discountedFeeAmount: undefined,
          actualFeeAmount: undefined,
          feeCollectionTime: undefined,
          status: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          relatedOrderId: [{
            required: true,
            message: this.$t('business.deal.feeRecord.relatedOrderIdRequired'),
            trigger: "blur"
          }],
          userId: [{
              required: true,
              message: this.$t('business.deal.feeRecord.userIdRequired'),
              trigger: "blur"
            },
            {
              pattern: /^\d+$/,
              message: this.$t('business.deal.feeRecord.userIdPattern'),
              trigger: "blur"
            }
          ],
          feeStandardId: [{
              required: true,
              message: this.$t('business.deal.feeRecord.feeStandardIdRequired'),
              trigger: "blur"
            },
            {
              pattern: /^\d+$/,
              message: this.$t('business.deal.feeRecord.feeStandardIdPattern'),
              trigger: "blur"
            }
          ],
          discountRuleId: [{
              required: true,
              message: this.$t('business.deal.feeRecord.discountRuleIdRequired'),
              trigger: "blur"
            },
            {
              pattern: /^\d+$/,
              message: this.$t('business.deal.feeRecord.discountRuleIdPattern'),
              trigger: "blur"
            }
          ],
          calculatedFeeAmount: [{
              required: true,
              message: this.$t('business.deal.feeRecord.calculatedFeeAmountRequired'),
              trigger: "blur"
            },
            {
              pattern: /^\d+(\.\d+)?$/,
              message: this.$t('business.deal.feeRecord.calculatedFeeAmountPattern'),
              trigger: "blur"
            }
          ],
          discountedFeeAmount: [{
              required: true,
              message: this.$t('business.deal.feeRecord.discountedFeeAmountRequired'),
              trigger: "blur"
            },
            {
              pattern: /^\d+(\.\d+)?$/,
              message: this.$t('business.deal.feeRecord.discountedFeeAmountPattern'),
              trigger: "blur"
            }
          ],
          actualFeeAmount: [{
              required: true,
              message: this.$t('business.deal.feeRecord.actualFeeAmountRequired'),
              trigger: "blur"
            },
            {
              pattern: /^\d+(\.\d+)?$/,
              message: this.$t('business.deal.feeRecord.actualFeeAmountPattern'),
              trigger: "blur"
            }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 转换业务类型 */
      getBusinessTypeLabel(type) {
        const map = {
          '1': this.$t('business.deal.feeRecord.businessTypeOption.transfer'),
          '2': this.$t('business.deal.feeRecord.businessTypeOption.payment'),
          '3': this.$t('business.deal.feeRecord.businessTypeOption.recharge'),
          '4': this.$t('business.deal.feeRecord.businessTypeOption.withdraw'),
          '5': this.$t('business.deal.feeRecord.businessTypeOption.collection'),
          '6': this.$t('business.deal.feeRecord.businessTypeOption.paymentOut')
        };
        return map[type] || type;
      },
      /** 查询手续费记录列表 */
      getList() {
        this.loading = true;
        listFeeRecord(this.queryParams).then(response => {
          this.feeRecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.detailOpen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          relatedOrderId: undefined,
          businessType: undefined,
          userId: undefined,
          feeStandardId: undefined,
          discountRuleId: undefined,
          calculatedFeeAmount: undefined,
          discountedFeeAmount: undefined,
          actualFeeAmount: undefined,
          feeCollectionTime: undefined,
          status: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = this.$t('business.deal.feeRecord.addTitle');
      },
      /** 详情按钮操作 */
      handleDetail(row) {
        this.reset();
        this.form = row;
        this.detailOpen = true;
        this.title = this.$t('business.deal.feeRecord.detailTitle');
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getFeeRecord(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = this.$t('business.deal.feeRecord.editTitle');
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateFeeRecord(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('business.deal.feeRecord.updateSuccess'));
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addFeeRecord(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('business.deal.feeRecord.addSuccess'));
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm(this.$t('business.deal.feeRecord.deleteConfirm', {
          ids: ids
        })).then(() => {
          this.loading = true;
          return delFeeRecord(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess(this.$t('business.deal.feeRecord.deleteSuccess'));
        }).catch(() => {}).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/feeRecord/export', {
          ...this.queryParams
        }, `feeRecord_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
