<!-- 新建流程页面 -->
<script lang="ts" setup name="EditProcess">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import type { Ref } from 'vue'
import Setting from '../../setting.vue'
import type { Iform, dictType } from './process'
import { getDictByCode } from '@/api/system/dict'

const emits = defineEmits(['close'])
const processSetting = ref()
const selectFormId = ref('') // formid
const ruleFormRef = ref<FormInstance>()
const form: Ref<Iform> = ref({
  number: '', // 编号
  name: '', // 名称
  business: '', // 关联业务
  formID: '', // formID
  desc: '', // 流程描述
  decisionItem: '', // 可选决策项 1同意驳回拒绝2同意驳回3同意拒绝
  noticeStarter: '', // 审批结果是否通知发起人
})
const decisionItemList = ref<dictType[]>([]) // 可选决策项
const noticeStarterList = ref<dictType[]>([]) // 可选决策项

// 表单校验
const rules = reactive<FormRules>({
  name: [
    {
      required: true,
      message: '流程名称必填',
      trigger: 'blur',
    },
  ],
  business: [
    {
      required: true,
      message: '关联业务必填',
      trigger: 'blur',
    },
  ],
  formID: [
    {
      required: true,
      message: 'formID必填',
      trigger: 'blur',
    },
  ],
  desc: [
    {
      required: true,
      message: '流程描述必填',
      trigger: 'blur',
    },
  ],
  decisionItem: [
    {
      required: true,
      message: '可选决策项必填',
      trigger: 'change',
    },
  ],
  noticeStarter: [
    {
      required: true,
      message: '审批结果是否通知发起人必填',
      trigger: 'change',
    },
  ],
})

// 获取字典值
async function getDict() {
  // 可选决策项
  const response = await getDictByCode('decisionItem')
  decisionItemList.value = response.data

  // 审批结果是否通知发起人
  const res = await getDictByCode('noticeStarter')
  noticeStarterList.value = res.data
}

const $router = useRouter()
// 关闭
const close = () => {
  $router.back()
}

// 保存
const save = async (formEl: FormInstance | undefined) => {
  if (!formEl) { return }
  await formEl.validate((valid, fields) => {
    if (valid) {
      window.sessionStorage.setItem('decisionItem', `${form.value.decisionItem}`)
      window.sessionStorage.setItem('noticeStarter', `${form.value.noticeStarter}`)
      // 调setting组件的保存
      processSetting.value.saveSet()
    }
  })
}

// 监听流程组件--新建流程成功
const createProcessSuccess = () => {
  close()
}
onMounted(async () => {
  await getDict()
})
</script>

<template>
  <div class="edit-process">
    <app-container>
      <detail-page title="流程管理-新建">
        <template #btns>
          <el-button type="primary" @click="save(ruleFormRef)">
            保存
          </el-button>
          <el-button type="info" @click="close">
            关闭
          </el-button>
        </template>
        <el-form ref="ruleFormRef" :model="form" label-width="180px" :rules="rules">
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="流程编号:">
                <el-input v-model="form.number" disabled placeholder="系统自动生成" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="流程名称:" prop="name">
                <el-input v-model="form.name" placeholder="请输入流程名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="formID:" prop="formID">
                <el-input v-model="form.formID" placeholder="请输入formID" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关联业务:" prop="business">
                <el-input v-model="form.business" placeholder="请输入关联业务" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="可选决策项:" prop="decisionItem">
                <el-select
                  v-model="form.decisionItem"
                  clearable
                  placeholder="请选择可选决策项"
                  size="default"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="item in decisionItemList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审批结果是否通知发起人:" prop="noticeStarter">
                <el-select
                  v-model="form.noticeStarter"
                  clearable
                  placeholder="请选择是否通知发起人"
                  size="default"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="item in noticeStarterList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="流程描述" prop="desc">
                <el-input v-model="form.desc" type="textarea" autosize placeholder="请输入流程描述" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </detail-page>

      <detail-block title="流程配置" />
      <setting
        ref="processSetting"
        :is-add-process="true"
        :get-row-data="form"
        :select-form-id="selectFormId"
        @createProcessSuccess="createProcessSuccess"
      />
    </app-container>
  </div>
</template>

<style lang="scss" scoped>
  .edit-process {
    width: 100%;

    .title {
      margin: 0 0 20px;
      font-weight: 600;
    }

    .edit {
      width: 100%;
      padding: 12px;
      background-color: #fff;
      border-radius: 7px;
      margin: 0 0 20px;

      .button-area {
        width: 100%;
        display: flex;
        justify-content: flex-end;
        margin-bottom: 20px;
      }
    }

    .set {
      width: 100%;
      background-color: #fff;
      border-radius: 7px;
      margin: 20px 0;
    }
  }
</style>
