<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="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入渠道名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户类型" prop="accountType">
        <el-select v-model="queryParams.accountType" placeholder="请选择对应账户类型" clearable>
          <el-option
            v-for="dict in dict.type.account_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务费收取类型" prop="serviceType" label-width="8em">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务费收取类型" clearable>
          <el-option
            v-for="dict in dict.type.withdraw_service_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-radio-group v-model="queryParams.enabled">
          <el-radio-button :label="true">是</el-radio-button>
          <el-radio-button :label="false">否</el-radio-button>
        </el-radio-group>
      </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:channelWithdraw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="channelWithdrawList" @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 === 'channelId'">
              {{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 === 'enabled'">
              <el-switch v-model="d.row.enabled" @change="(nv ) =>{onChangeEnabled(d.row, nv)}"/>
            </template>
            <template v-else-if="column.key === 'serviceType'">
              <dict-tag :options="dict.type.withdraw_service_type" :value="d.row.serviceType"/>
            </template>
            <template v-else-if="column.key === 'serviceRate'">
              {{d.row.serviceRate | money}} {{withdrawServiceUnit(d.row.serviceType)}}
            </template>
            <template v-else-if="column.key === 'costRate'">
              {{d.row.costRate | money}} %
            </template>
            <template v-else-if="column.key === 'picture'">
              <image-preview :src="d.row.picture" :width="32" :height="32"/>
            </template>
            <template v-else-if="column.key === 'minAmount' || column.key === 'maxAmount'">
              {{d.row[column.key] | 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">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ss:channelWithdraw:edit']"
          >修改</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <form-col :span="span * 2" label="图片" prop="picture">
          <image-upload v-model="form.picture" :limit="1"/>
        </form-col>
        <form-col :span="span" label="渠道名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入渠道名称" />
        </form-col>
        <form-col :span="span" label="账户类型" prop="accountType">
          <el-select v-model="form.accountType" style="width: 100%">
            <el-option
              v-for="dict in dict.type.account_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </form-col>
<!--        <form-col :span="span * 2" label="服务费收取方式" label-width="9em" prop="serviceType">-->
<!--          <el-radio-group v-model="form.serviceType">-->
<!--            <el-radio v-for="dict in dict.type.withdraw_service_type" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </form-col>-->
        <form-col :span="span" label="服务费" prop="serviceRate">
          <el-input v-model.number="form.serviceRate" :min="0" type="number">
            <template #suffix>
              {{withdrawServiceUnit(form.withdrawType)}}
            </template>
          </el-input>
        </form-col>
        <form-col :span="span" label="渠道成本" prop="costRate">
          <el-input v-model.number="form.costRate"  placeholder="请输入充值成本率" :min="0" type="number">
            <template #suffix>%</template>
          </el-input>
        </form-col>
        <form-col :span="span" label="单笔最低" prop="minAmount">
          <el-input v-model.number="form.minAmount"  placeholder="请输入单笔最低提现金额" :min="0" type="number">
            <template #suffix>元</template>
          </el-input>
        </form-col>
        <form-col :span="span" label="单笔最高" prop="maxAmount">
          <el-input v-model.number="form.maxAmount"  placeholder="请输入单笔最高提现金额" :min="0" type="number">
            <template #suffix>元</template>
          </el-input>
        </form-col>
      </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 { listChannelWithdraw, getChannelWithdraw, delChannelWithdraw, addChannelWithdraw, updateChannelWithdraw } from "@/api/system/channelWithdraw";
import { $showColumns, $withdrawServiceType } from '@/utils/mixins'
import { updateBill } from '@/api/system/withdraw'

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

export default {
  name: "ChannelWithdraw",
  mixins: [$showColumns, $withdrawServiceType],
  dicts: ['account_type', 'withdraw_service_type'],
  data() {
    return {
      span: 12,
      // 字段列表
      columns: [
        {key: 'channelId', visible: true, label: 'ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: "80"},
        {key: 'name', visible: true, label: '渠道名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'picture', visible: true, label: '图片', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'enabled', visible: true, label: '是否启用', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'accountType', visible: true, label: '对应账户类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'serviceType', visible: true, label: '服务费收取类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'serviceRate', visible: true, label: '服务费', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'costRate', visible: true, label: '渠道成本', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'minAmount', visible: true, label: '单笔最低', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'maxAmount', 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,
      // 提现渠道表格数据
      channelWithdrawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        channelId: null,
        name: null,
        accountType: null,
        serviceType: null,
        enabled: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        accountType: [
          { required: true, message: "对应账户类型不能为空", trigger: "change" }
        ],
        serviceType: [
          { required: true, message: "服务费收取类型不能为空", trigger: "change" }
        ],
        serviceRate: [
          { required: true, message: "服务费不能为空", trigger: "blur" }
        ],
        enabled: [
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],
        costRate: [
          { required: true, message: "渠道成本不允许为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onChangeEnabled(row, enabled) {
      updateChannelWithdraw({channelId: row.channelId, enabled: enabled}).catch(() => {
        row.enabled = !enabled;
      })
    },
    /** 当排序按钮被点击时触发 **/
    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;
      listChannelWithdraw(this.queryParams).then(response => {
        this.channelWithdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        channelId: null,
        name: null,
        accountType: null,
        serviceType: null,
        serviceRate: null,
        enabled: null,
        costRate: 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.channelId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现渠道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const channelId = row.channelId || this.ids
      getChannelWithdraw(channelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改提现渠道";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.channelId != null) {
            updateChannelWithdraw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChannelWithdraw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const channelIds = row.channelId || this.ids;
      this.$modal.confirm('是否确认删除提现渠道编号为"' + channelIds + '"的数据项？').then(function() {
        return delChannelWithdraw(channelIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/channelWithdraw/export', {
        ...this.queryParams
      }, `channelWithdraw_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
