<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button
        type="primary"
        size="small"
        icon="Plus"
        @click="handleAdd"
        v-hasPermi="['business:account:add']"
        >新增</el-button
      >
      <el-button
        type="danger"
        size="small"
        icon="Delete"
        :disabled="multiple"
        @click="handleDelete"
        v-hasPermi="['business:account:remove']"
        >删除</el-button
      >
      <el-dropdown trigger="click">
        <el-button type="primary" size="small">
          其它<el-icon class="el-icon--right"><arrow-down /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item
              icon="Download"
              @click="handleExport"
              v-hasPermi="['business:account:export']"
            >
              导出</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <!-- 搜索区 -->
    <el-form
      class="list-search-container"
      size="small"
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="账户名称：" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入账户名称"
          style="width: 150px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号：" prop="accountNum">
        <el-input
          v-model="queryParams.accountNum"
          placeholder="请输入账号"
          style="width: 150px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行：" prop="accountOpen">
        <el-input
          v-model="queryParams.accountOpen"
          placeholder="请输入开户行"
          style="width: 150px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表区 -->
    <el-table
      v-loading="loading"
      :data="accountList"
      size="small"
      border
      height="100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账户名称" align="center" prop="name">
        <template #default="scope">
          <el-input
            v-if="scope.row.isAdd || scope.row.isEdit"
            v-model="scope.row.name"
            size="small"
            placeholder="请输入内容"
          ></el-input>
          <span v-else>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center" prop="accountNum">
        <template #default="scope">
          <el-input
            v-if="scope.row.isAdd || scope.row.isEdit"
            size="small"
            v-model="scope.row.accountNum"
            placeholder="请输入内容"
          ></el-input>
          <span v-else>{{ scope.row.accountNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开户行" align="center" prop="accountOpen">
        <template #default="scope">
          <el-input
            v-if="scope.row.isAdd || scope.row.isEdit"
            size="small"
            v-model="scope.row.accountOpen"
            placeholder="请输入内容"
          ></el-input>
          <span v-else>{{ scope.row.accountOpen }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <template
            v-if="
              !scope.row.isAdd &&
              !scope.row.isEdit &&
              scope.row.referrerDataSource == isReferrerDataSource
            "
          >
            <el-button
              v-show="
                !scope.row.isAdd &&
                !scope.row.isEdit &&
                scope.row.referrerDataSource == isReferrerDataSource
              "
              link
              type="warning"
              size="small"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['business:account:edit']"
              >修改</el-button
            >
            <el-button
              v-show="
                !scope.row.isAdd &&
                !scope.row.isEdit &&
                scope.row.referrerDataSource == isReferrerDataSource
              "
              link
              type="danger"
              size="small"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:account:remove']"
              >删除</el-button
            >
          </template>
          <template
            v-if="
              scope.row.isAdd ||
              (scope.row.isEdit &&
                scope.row.referrerDataSource == isReferrerDataSource)
            "
          >
            <el-button
              v-show="
                scope.row.isAdd ||
                (scope.row.isEdit &&
                  scope.row.referrerDataSource == isReferrerDataSource)
              "
              link
              type="primary"
              size="small"
              v-hasPermi="['business:account:edit', 'business:account:add']"
              @click="handleSave(scope.row)"
              >保存</el-button
            >
            <el-button
              v-show="scope.row.isEdit || scope.row.isAdd"
              link
              size="small"
              @click="handCancel(scope.row)"
              >取消</el-button
            >
          </template>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 表单 -->
    <account-form ref="accountRef" :get-list="getList"></account-form>
  </div>
</template>

<script setup name="Account">
import {
  listAccount,
  delAccount,
  addAccount,
  updateAccount,
  exportAccount,
} from "@/api/business/account";
import accountForm from "./form";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
/** 查询 对象 */

const accountList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  tenantId: null,
  orderByColumn: "create_time",
  name: null,
  accountNum: null,
  accountOpen: null,
  createTime: null,
  isEdit: false,
  isAdd: false,
});

/***********************  方法区  ****************************/

/** 查询银行账户列表 */
function getList() {
  loading.value = true;
  listAccount(queryParams.value).then((response) => {
    accountList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 保存按钮操作 */
function handleSave(row) {
  if (row.name == null || row.name == "") {
    proxy.$modal.msgError("账户名称不能为空！");
    return;
  }

  if (row.accountNum == null || row.accountNum == "") {
    proxy.$modal.msgError("账号不能为空！");
    return;
  }
  if (row.isAdd) {
    addAccount(row).then((response) => {
      proxy.$modal.msgSuccess("添加成功");
      visible.value = false;
      getList.value();
    });
    row.isAdd = false;
    getList();
    getList();
  }

  if (row.isEdit) {
    updateAccount(row).then((response) => {
      proxy.$modal.msgSuccess("修改成功");
      visible.value = false;
      getList.value();
    });
    queryParams.isEdit = false;
    getList();
  }

  getList();
}

/** 新增按钮操作 */
function handleAdd() {
  // 此处必须进行深拷贝，否则添加的明细，将都指向了一个对象
  const _newSource = JSON.parse(JSON.stringify(queryParams.value));
  accountList.value.push(_newSource);
  handleSourceCurrentChange(_newSource);
}

/** 取消按钮操作 */
function handCancel(row) {
  if (row.isEdit) row.isEdit = false;
  else accountList.value.length = accountList.value.length - 1;
}

/** 修改按钮操作 */
function handleSourceCurrentChange(row) {
  row.isAdd = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  row.isEdit = true;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除选中的数据项？")
    .then(function () {
      return delAccount(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功！");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  exportAccount(queryParams.value);
}

getList();
</script>
