<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" size="small" label-width="90px">
      <el-form-item label="退款日期">
        <el-date-picker
          v-model="queryParams.refDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择"
          style="width: 220px"
        />
      </el-form-item>
      <el-form-item label="退款编号">
        <el-input v-model="queryParams.refNo" placeholder="请输入退款编号" clearable style="width: 220px" />
      </el-form-item>
      <el-form-item label="客户编号">
        <el-input v-model="queryParams.custNo" placeholder="请输入客户编号" clearable style="width: 220px" />
      </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="['erp:arap:refund: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="['erp:arap:refund: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="['erp:arap:refund:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="80" align="center" />
      <el-table-column label="退款日期" prop="refDate" min-width="120" align="center" />
      <el-table-column label="退款编号" prop="refNo" min-width="180" align="center" />
      <el-table-column label="客户编号" prop="custNo" min-width="140" align="center" />
      <el-table-column label="客户名称" prop="custName" min-width="180" align="center" />
      <el-table-column label="退款人" prop="refBy" min-width="120" align="center" />
      <el-table-column label="银行卡号" prop="bankNo" min-width="180" align="center" />
      <el-table-column label="退款类别" prop="refType" min-width="120" align="center" />
      <el-table-column label="退款金额" prop="refAmt" min-width="140" align="right" />
      <el-table-column label="是否已扣款" min-width="120" align="center">
        <template slot-scope="scope">
          {{ String(scope.row.deducted || "0") === "1" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" min-width="200" align="center" />
      <el-table-column label="操作" align="center" width="230">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleView(scope.row)" v-hasPermi="['erp:arap:refund:query']">查看</el-button>
          <el-button type="text" size="mini" @click="handleUpdate(scope.row)" v-hasPermi="['erp:arap:refund:edit']">修改</el-button>
          <el-button type="text" size="mini" @click="handleDelete(scope.row)" v-hasPermi="['erp:arap:refund:remove']">删除</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="820px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="12">
          <el-col :span="6">
            <el-form-item label="退款日期" prop="refDate">
              <el-date-picker
                v-model="form.refDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择"
                style="width: 100%"
                :disabled="refDateDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="退款编号">
              <el-input v-model="form.refNo" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12">
          <el-col :span="6">
            <el-form-item label="客户编号" prop="custNo">
              <el-input v-model="form.custNo" placeholder="请输入客户编号" :disabled="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户名称" prop="custName">
              <el-input v-model="form.custName" placeholder="请输入客户名称" :disabled="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="退款人" prop="refBy">
              <el-input v-model="form.refBy" placeholder="请输入退款人" :disabled="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="银行卡号" prop="bankNo">
              <el-input v-model="form.bankNo" placeholder="请输入银行卡号" :disabled="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="退款类别" prop="refType">
              <el-select v-model="form.refType" placeholder="请选择" :disabled="readonly" clearable style="width: 100%">
                <el-option label="现金" value="CASH" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="退款金额" prop="refAmt">
              <el-input-number v-model="form.refAmt" :precision="4" :min="0" :step="1" :disabled="readonly" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注" :disabled="readonly" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!readonly" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, getRefund, addRefund, editRefund, delRefund } from "@/api/erp/arap";

export default {
  name: "ArapRefund",
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      list: [],
      ids: [],
      single: true,
      multiple: true,
      open: false,
      title: "",
      readonly: false,
      refDateDisabled: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refDate: undefined,
        refNo: undefined,
        custNo: undefined
      },
      form: {},
      rules: {
        refDate: [{ required: true, message: "退款日期不能为空", trigger: "change" }],
        custNo: [{ required: true, message: "客户编号不能为空", trigger: "blur" }],
        custName: [{ required: true, message: "客户名称不能为空", trigger: "blur" }],
        refBy: [{ required: true, message: "退款人不能为空", trigger: "blur" }],
        bankNo: [{ required: true, message: "银行卡号不能为空", trigger: "blur" }],
        refType: [{ required: true, message: "退款类别不能为空", trigger: "change" }],
        refAmt: [{ required: true, message: "退款金额不能为空", trigger: "change" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then((res) => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
        refDate: this.parseTime(new Date(), "{y}-{m}-{d}"),
        refNo: "",
        custNo: "",
        custName: "",
        refBy: "",
        bankNo: "",
        refType: "CASH",
        refAmt: 0,
        deducted: "0",
        remark: ""
      };
      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.readonly = false;
      this.refDateDisabled = false;
      this.reset();
      this.open = true;
      this.title = "新增客户退款登记";
    },
    handleUpdate(row) {
      const id = row && row.id ? row.id : this.ids[0];
      if (!id) return;
      this.readonly = false;
      this.refDateDisabled = true;
      this.reset();
      getRefund(id).then((res) => {
        this.form = Object.assign({}, res.data || {});
        this.open = true;
        this.title = "修改客户退款登记";
      });
    },
    handleView(row) {
      if (!row || !row.id) return;
      this.readonly = true;
      this.refDateDisabled = true;
      getRefund(row.id).then((res) => {
        this.form = Object.assign({}, res.data || {});
        this.open = true;
        this.title = "查看客户退款登记";
      });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        const payload = {
          id: this.form.id,
          refDate: this.form.refDate,
          refNo: this.form.refNo,
          custNo: this.form.custNo,
          custName: this.form.custName,
          refBy: this.form.refBy,
          bankNo: this.form.bankNo,
          refType: this.form.refType,
          refAmt: Number(this.form.refAmt || 0),
          deducted: this.form.deducted || "0",
          remark: this.form.remark
        };
        const req = payload.id ? editRefund(payload) : addRefund(payload);
        this.$modal.confirm("确认提交该客户退款登记吗？").then(() => {
          req.then(() => {
            this.$modal.msgSuccess(payload.id ? "修改成功" : "新增成功");
            this.open = false;
            this.getList();
          });
        }).catch(() => {});
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    handleDelete(row) {
      const ids = row && row.id ? row.id : this.ids.join(",");
      if (!ids) return;
      this.$modal.confirm("确认删除选中的客户退款登记单吗？").then(() => delRefund(ids)).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => {});
    }
  }
};
</script>

