<template>
  <el-drawer
    v-model="drawerData.drawerShow"
    title="编辑计划"
    direction="rtl"
    :before-close="drawerData.handleClose"
    size="70%"
  >
    <el-button type="primary" size="small" @click="drawerData.visible = true">新增计划</el-button>
    <el-tabs class="demo-tabs">
      <el-tab-pane v-for="(item, index) in drawerData.list" :label="item.name" :key="index">
        <CustomInputTable
          :tableTitle="drawerData.tableTitle"
          :tableData="drawerData.tableData"
          @onAdd="drawerData.onAdd"
        >
          <template #default="scope">
            <el-button type="danger" size="small" @click="drawerData.delete(scope.$index)">删除</el-button>
          </template>
        </CustomInputTable>
      </el-tab-pane>
    </el-tabs>
  </el-drawer>
  <el-dialog v-model="drawerData.visible" title="新增计划">
    <el-form :model="drawerData.formData" :rules="drawerData.rules" label-width="100px">
      <el-form-item label="计划名称" prop="name">
        <el-input v-model="drawerData.formData.name" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="drawerData.visible = false">取消</el-button>
        <el-button type="primary" @click="drawerData.addPlan"
          >确定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { reactive } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
export default {
  name: 'Plan',
  setup() {
    const tableData: {
      id: number;
      task: string;
      count: string;
      time: string;
      beforeTask: string;
      pharse: string;
      folder: string;
      role: string;
      more: string;
    }[] = []
    const drawerData = reactive({
      drawerShow: false,
      list: [
        {
          name: '主计划'
        }
      ],
      tableTitle: [
        {
          lable: '序号',
          prop: 'id'
        },
        {
          lable: '任务',
          prop: 'task'
        },
        {
          lable: '工期',
          prop: 'count'
        },
        {
          lable: '标准工时',
          prop: 'time'
        },
        {
          lable: '前置任务',
          prop: 'beforeTask'
        },
        {
          lable: '关联阶段',
          prop: 'pharse'
        },
        {
          lable: '预期交付文档',
          prop: 'folder'
        },
        {
          lable: '责任角色',
          prop: 'role'
        },
        {
          lable: '更多属性',
          prop: 'more'
        },
      ],
      tableData: tableData,
      visible: false,
      formData: {
        name: ''
      },
      rules: {
        name: [{
          required: true, message: '请输入计划名称', trigger: 'blur'
        }]
      },
      show: () => {
        drawerData.drawerShow = true
      },
      addPlan: ()=> {
        drawerData.list.push({
          name: drawerData.formData.name
        })
        drawerData.visible = false
      },
      onAdd: (value: string) => {
        if (!value) {
          return false
        }
        drawerData.tableData.push({
          id: Date.now(),
          task: value,
          count: '',
          time: '',
          beforeTask: '',
          pharse: '',
          folder: '',
          role: '',
          more: '更多详情',
        })
      },
      delete: (index: number) => {
        ElMessageBox.confirm('确定删除吗？', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          drawerData.tableData.splice(index, 1)
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
        })
      }
    })
    return { drawerData }
  }
}
</script>

<style>

</style>
