<template>
  <div class="app-container project-milestone">

    <template v-if="data.currentProId">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            v-hasPermi="['manage:milestone:add']">新增一行</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="Edit" @click="handleUpdate"
            v-hasPermi="['manage:milestone:edit']">保存</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="data.milestoneList" class="data-list">
        <el-table-column label="节点名称" align="center" prop="nodeName">
          <template #default="scope">
            <el-input v-model="scope.row.nodeName" />
          </template>
        </el-table-column>
        <el-table-column label="工期" align="center" prop="days" width="180">
          <template #default="scope">
            <span v-if="scope.row.days">{{ scope.row.days }}天</span>
          </template>
        </el-table-column>
        <el-table-column label="计划开始日期" align="center" prop="scheduledStart" width="180">
          <template #default="scope">
            <el-date-picker v-model="scope.row.scheduledStart" />
          </template>
        </el-table-column>
        <el-table-column label="计划结束日期" align="center" prop="scheduledEnd" width="180">
          <template #default="scope">
            <el-date-picker v-model="scope.row.scheduledEnd" />
          </template>
        </el-table-column>
        <el-table-column label="实际开始日期" align="center" prop="actualStart" width="180">
          <template #default="scope">
            <el-date-picker v-model="scope.row.actualStart" />
          </template>
        </el-table-column>
        <el-table-column label="实际结束日期" align="center" prop="actualEnd" width="180">
          <template #default="scope">
            <el-date-picker v-model="scope.row.actualEnd" />
          </template>
        </el-table-column>

        <el-table-column label="说明" align="center" prop="explainInfo">
          <template #default="scope">
            <el-input v-model="scope.row.explainInfo" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template #default="scope">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['manage:milestone:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div v-else style="margin-top:50px;text-align: center;color: #999;">
      <el-icon color="#999">
        <WarningFilled />
      </el-icon> 请选择项目！
    </div>
  </div>
</template>

<script setup name="Milestone">
import { listMilestone, getMilestone, delMilestone, addBatch } from "@/api/manage/milestone";
import useUserStore from '@/store/modules/user'
const { proxy } = getCurrentInstance();
const userStore = useUserStore()

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: 100,
    comId: null,
    projectId: null,
    nodeName: null,
    scheduledStart: null,
    scheduledEnd: null,
    actualStart: null,
    actualEnd: null,
    days: null,
    explainInfo: null,
    isDel: null,
    state: null,
  },
  rules: {
  },
  currentProId: '',
  milestoneList: []
});

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

/** 查询项目里程碑列表 */
function getList() {
  loading.value = true;
  listMilestone(queryParams.value).then(response => {
    data.milestoneList = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}



/** 新增按钮操作 */
function handleAdd() {
  data.milestoneList.push({
    nodeName: '',
    scheduledStart: null,
    scheduledEnd: null,
    actualStart: null,
    actualEnd: null,
    days: null,
    explainInfo: ''
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  let objs = data.milestoneList.filter(d => d.nodeName).filter(d => d.nodeName.trim());
  objs.forEach(it => {
    it.comId = userStore.currentComId;
    it.projectId = userStore.currentProId;
  })
  if (objs.length == 0) {
    proxy.$modal.msgError("请输入节点名称!");
  } else {
    addBatch(objs).then(d => {
      if (d.code == 200) {
        proxy.$modal.msgSuccess("保存成功!");
        getList();
      } else {
        proxy.$modal.msgError("保存失败!");
      }
    });
  }
}


/** 删除按钮操作 */
function handleDelete(row) {
  let _ids = [row.id];
  if (!(row.nodeName && row.nodeName.trim())) {
    let index = data.milestoneList.indexOf(row);
    if (index >= 0) {
      data.milestoneList.splice(index, 1);
    }
    return;
  }
  proxy.$modal.confirm('是否确认删除项目里程碑【' + row.nodeName + '】的数据项？').then(function () {
    return delMilestone(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

onMounted(() => {
  queryParams.value.projectId = userStore.currentProId;
  queryParams.value.comId = userStore.currentComId;
  data.currentProId = userStore.currentProId;
  getList();
});
</script>
<style lang="scss">
.project-milestone {
  .data-list {
    div.cell {
      .el-date-editor {
        width: 100%;
      }
    }
  }
}
</style>