<template>
  <div class="page-container row-container">

    <section class="list-part-container" style="flex: 2">
      <!-- 搜索区域 -->
      <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item class="section-title" label="辅助工序管理" />

        <el-form-item label="辅助工序名称：" prop="code">
          <el-input v-model="queryParams.name" placeholder="请输入辅助工序名称" clearable @keyup.enter="handleQuery"
            style="width: 160px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="info" icon="Search" @click="handleQuery">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Plus" @click="handleAdd(null)"
            v-hasPermi="['business:auxiliaryProcess:add']">新增</el-button>
        </el-form-item>

      </el-form>
      <!-- 列表区域 -->
      <div class="el-table-container">
        <div class="el-table-inner-container">
          <el-table v-loading="loading" :data="processList" border height="100%"
            @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column type="index" label="行号" width="50" align="center" />
            <el-table-column label="辅助工序名称" align="center" prop="name">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="辅助工序名称" v-model="scope.row.name"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.name }}</div>
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark">
              <template #default="scope">
                <el-input v-if="scope.row.editStatus" placeholder="备注" v-model="scope.row.remark"
                  style="width: 100%;" />
                <div v-else>{{ scope.row.remark }}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button v-if="!scope.row.editStatus" link type="warning" size="small" icon="Edit"
                  @click="handleUpdate(scope.row)" v-hasPermi="['business:auxiliaryProcess:edit']">修改</el-button>
                <el-button v-else link type="success" size="small" icon="Check"
                  @click="handleSave(scope.row)">保存</el-button>
                <el-button v-if="!scope.row.editStatus" link type="danger" size="small" icon="Delete"
                  @click="handleDelete(scope.row, scope.$index)"
                  v-hasPermi="['business:auxiliaryProcess:remove']">删除</el-button>
                <el-button v-else link type="info" size="small" icon="Close" @click="handleCancel(scope.row)"
                  v-hasPermi="['business:auxiliaryProcess:remove']">取消</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
    </section>
  </div>
</template>

<script setup name="Equipment">
import { listProcess, delProcess, addProcess, updateProcess } from "@/api/business/auxiliaryProcess";
const { proxy } = getCurrentInstance();
const processList = 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: 10,
  code: null,
  name: null,
  status: null,
})

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

/** 查询检查类别列表 */
function getList() {
  loading.value = true;
  listProcess(queryParams.value).then(response => {
    processList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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


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

/** 新增按钮操作 */
function handleAdd() {
  processList.value.push({ editStatus: true })
}

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


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

function handleSave(row) {
  if (row.id == null) {
    addProcess(row).then(afterSave)
  } else {
    updateProcess(row).then(afterSave)
  }
}

function afterSave(res) {
  if (res.code === 200) {
    getList()
    proxy.$modal.msgSuccess("保存成功！")
  } else {
    proxy.$modal.msgError("保存失败！")
  }
}

function handleCancel(row) {
  row.editStatus = false
}

getList();
</script>
<style scoped>
:deep(.el-tree-node__label) {
  font-size: 14px !important;
  display: inline-block;
  width: 100%;
}

:deep(.el-tree-node__content) {
  height: 40px;
  border-bottom: 1px solid #ebeef5;
  padding: 8px 0;
  line-height: 23px;
}

.move-handle {
  cursor: move;
  background-color: #eee;
}
</style>
