<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" size="small" icon="Plus" @click="handleAdd"
        v-hasPermi="['settings:source:add']">新增</el-button>

      <el-button type="danger" size="small" icon="Delete" :disabled="multiple" @click="handleDelete"
        v-hasPermi="['settings:source:remove']">删除</el-button>
    </div>
    <!-- 搜索区 -->
    <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true"
      label-width="68px">
      <el-form-item label="来源：" prop="referrerDataSource">
        <el-input v-model="queryParams.title" 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(queryParams)">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表区 -->
    <el-table v-loading="loading" :data="sourceList" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" type="index" /> -->
      <el-table-column label="来源" align="center" prop="title">
        <template #default="scope">
          <el-input v-if="scope.row.isAdd" v-model="scope.row.title" size="small" placeholder="请输入内容"></el-input>
          <el-input v-else-if="scope.row.isEdit" size="small" v-model="scope.row.title" placeholder="请输入内容"></el-input>
          <span v-else>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <template v-if="!scope.row.isAdd &&
            !scope.row.isEdit &&
            scope.row.referrerDataSource == isReferrerDataSource
            ">
            <el-button link type="warning" size="small" @click="handleUpdate(scope.row)"
              v-hasPermi="['settings:source:edit']">修改</el-button>
            <el-button link type="danger" size="small" @click="handleDelete(scope.row)"
              v-hasPermi="['settings:source: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" @click="handleSave(scope.row)">保存</el-button>
            <el-button v-show="scope.row.isEdit || scope.row.isAdd" link type="infeo" 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" />

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

<script setup name="Source">
import {
  listSource,
  delSource,
  updateSource,
  addSource,
} from "@/api/settings/source";
import sourceForm from "./form";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
/** 查询 对象 */

const sourceList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const datarow = ref(0);
const isReferrerDataSource = ref("");

/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  title: null,
  referrerDataSource: null,
  orderByColumn: "create_time",
  createTime: null,
  isEdit: false,
  isAdd: false,
});

const sourceEmptyData = {
  id: null,
  title: "",
  remark: "",
  editStatus: true,
};

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

/** 查询来源列表 */
function getList() {
  loading.value = true;
  listSource(queryParams.value).then((response) => {
    sourceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

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

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

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

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

/** 保存按钮操作 */
function handleSave(row) {
  if (row.title !== null && row.title !== "") {
    if (row.isAdd) {
      addSource(row).then((response) => {
        proxy.$modal.msgSuccess("添加成功");
        visible.value = false;
        getList.value();
      });
      row.isAdd = false;
      getList();
    }

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

    getList();
  } else {
    proxy.$modal.msgError("来源不能为空！");
  }
}

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

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

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "settings/source/export",
    {
      ...queryParams.value,
    },
    `source_${new Date().getTime()}.xlsx`
  );
}

/** 修改新增属性判断 */
function handleSourceCurrentChange(row) {
  row.isAdd = true;
}
getList();
</script>
