<template>
  <div>
    <headerTitle title="计划信息"></headerTitle>
    <plan-form ref="planForm" :dialog-type="dialogType" :dialogForm="dialogForm" :deptList="deptList"
               :deptTreeList="deptTreeList"></plan-form>
    <headerTitle title="计划节点"></headerTitle>
    <plan-info-table ref="planInfoTable" :dialog-type="dialogType" :dialogForm="dialogForm" :deptList="deptList"
                     :deptTreeList="deptTreeList"></plan-info-table>
  </div>
</template>

<script>

import PlanForm from "@/views/bpm/handleTask/components/project-manage/plan-manage/plan-form.vue";
import planInfoTable from "@/views/bpm/handleTask/components/project-manage/plan-manage/planInfoTable.vue";
import {projectsPlanGetByIdAPI} from "@/api/bpm/components/project-manage/plan";
import {deepClone} from "ele-admin/lib/utils/core";
import {listOrganizations} from "@/api/system/organization";

export default {
  name: "project-form",
  components: {PlanForm, planInfoTable},
  props: {
    dialogType: {
      type: String,
      default: 'view',
    },
    businessId: {
      default: ''
    },
    taskId: {
      default: ''
    },
    taskDefinitionKey: {
      default: ''
    }
  },
  data() {
    return {
      dialogForm: {},
      deptList: [],
      deptTreeList: [],
    }
  },
  async created() {
    await this.getProjectPlanInfo(this.businessId)
    await this.getDeptList()
  },

  methods: {

    async getProjectPlanInfo(id = '') {
      let res = await projectsPlanGetByIdAPI(id)
      this.dialogForm = deepClone(res)
      this.dialogForm.responsibleUserIds = this.dialogForm.responsibleUserList.map(i => i.userName) || []
      this.dialogForm.planStageList.forEach((item, index) => {
        let userNames = item.responsibleUserList.map(i => i.userName) || []
        this.$set(item, 'responsibleUserNames', userNames.join(','))
      })
    },
    // 获取部门数据
    getDeptList() {
      listOrganizations().then((list) => {
        this.deptList = list;
        this.deptTreeList = this.$util.toTreeData({
          data: list,
          idField: 'id',
          parentIdField: 'parentId'
        });
      });
    },

    getTableValue() {
      return new Promise((resolve, reject) => {
        this.$refs.planForm.validate(valid => {
          if (!valid) {
            this.$message.warning('有必填项未填，请检查')
            reject('有必填项未填，请检查')
          } else {
            resolve(this.form)
          }
        })
      })
    }
  }
}
</script>


<style scoped lang="scss">

</style>
