<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['groupTaskLine:groupTaskLine:add']"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['groupTaskLine:groupTaskLine:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="groupTaskLineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="动作名称" align="center" prop="actionName" />
      <el-table-column label="上级动作" align="center" prop="parentId" />
      <el-table-column label="设备名称" align="center" prop="equipment" />
      <el-table-column label="序号" align="center" prop="actionNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['groupTaskLine:groupTaskLine:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup name="GroupTaskLine">
import { listGroupTaskLine, getGroupTaskLine, delGroupTaskLine, addGroupTaskLine, updateGroupTaskLine } from "@/api/hnyz/groupTaskLine";
import { defineProps, defineEmits } from 'vue'

const { proxy } = getCurrentInstance();

const groupTaskLineList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    groupId: null,
    actionId: null,
    actionName: null,
    parentId: null,
    actionNum: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

const props = defineProps({
    groupTask: Object
})

/** 查询任务动作组明细列表 */
function getList() {
  loading.value = true;
  queryParams.value.groupId = props.groupTask.groupId;
  listGroupTaskLine(queryParams.value).then(response => {
    groupTaskLineList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    lineId: null,
    groupId: null,
    actionId: null,
    actionName: null,
    parentId: null,
    actionNum: null,
    status: null,
    delFlag: null,
    createBy: null,
    updateBy: null,
    createTime: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("groupTaskLineRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加任务动作组明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _lineId = row.lineId || ids.value
  getGroupTaskLine(_lineId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改任务动作组明细";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["groupTaskLineRef"].validate(valid => {
    if (valid) {
      if (form.value.lineId != null) {
        updateGroupTaskLine(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGroupTaskLine(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _lineIds = row.lineId || ids.value;
  proxy.$modal.confirm('是否确认删除任务动作组明细编号为"' + _lineIds + '"的数据项？').then(function() {
    return delGroupTaskLine(_lineIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('groupTaskLine/groupTaskLine/export', {
    ...queryParams.value
  }, `groupTaskLine_${new Date().getTime()}.xlsx`)
}

getList();
</script>
