<!-- 编辑流程页面 -->
<script lang="ts" setup name="EditProcess">
import { onMounted, 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 } from './process'
import WorkFlow from '@/components/workFlow/workFlow.vue'
const emits = defineEmits(['close'])
const processSetting = ref()
const selectFormId = ref('') // formid
const $route = useRoute()
const getRowData = ref({})
const ruleFormRef = ref<FormInstance>()
const form: Ref<Iform> = ref({
  number: '', // 编号
  name: '', // 名称
  business: '', // 关联业务
  desc: '', // 流程描述
  selectDecision: '', // 选择的决策
  rejectTo: '', // 可驳回至
  changeData: '', // 驳回修改数据
  isMessage: '', // 审批结果通知发起人
})
// 可选决策项
const decision = [
  {
    id: 'reject-refuse',
    title: '同意 驳回 拒绝',
  },
  {
    id: 'reject',
    title: '同意 驳回',
  },
  {
    id: 'refuse',
    title: '同意 拒绝',
  },
]
// 可驳回至
const reject = [
  {
    id: 'change',
    title: '单据修改',
  },
]
// 驳回修改数据
const rejectChangeData = [
  {
    id: 'all',
    title: '重新按流程走',
  },
  {
    id: 'part',
    title: '送至驳回节点',
  },
]
// 审批结果通知发起人
const messageData = [
  {
    id: 'yes',
    title: '是',
  },
  {
    id: 'no',
    title: '否',
  },
]
const rules = reactive<FormRules>({
  selectDecision: [
    {
      required: true,
      message: '请选择',
      trigger: 'change',
    },
  ],
  rejectTo: [
    {
      required: true,
      message: '请选择',
      trigger: 'change',
    },
  ],
  changeData: [
    {
      required: true,
      message: '请选择',
      trigger: 'change',
    },
  ],
  isMessage: [
    {
      required: true,
      message: '请选择',
      trigger: 'change',
    },
  ],
})

const $router = useRouter()
// 关闭
const close = () => {
  $router.push('/process/list')
}
// 保存
const save = async (formEl: FormInstance | undefined) => {
  if (!formEl) { return }
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 调保存接口
      processSetting.value.saveSet()
    }
    else {
      console.log('error submit!', fields)
    }
  })
}

// 选择决策项
const changeDecision = (value: string) => {
  form.value.selectDecision = value
}
const changeRejectTo = (value: string) => {
  form.value.rejectTo = value
}
const changeData = (value: string) => {
  form.value.changeData = value
}
const changeMessage = (value: string) => {
  form.value.isMessage = value
}

onMounted(() => {
  const rowData: any = $route.query.rowData
  console.log('111111', JSON.parse(rowData))
  getRowData.value = JSON.parse(rowData)
  selectFormId.value = getRowData.value.formId
  form.value.number = getRowData.value.number
  form.value.name = getRowData.value.name
  form.value.business = getRowData.value.business
  form.value.desc = getRowData.value.describe
})
</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="150px" :rules="rules">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="流程编号:">
                {{ form.number }}
              </el-form-item>
              <el-form-item label="可选决策项" prop="selectDecision">
                <el-select v-model="form.selectDecision" placeholder="请选择" @change="changeDecision">
                  <el-option
                    v-for="item in decision"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="流程描述">
                <el-input v-model="form.desc" type="textarea" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="流程名称:">
                {{ form.name }}
              </el-form-item>

              <el-form-item v-if="form.selectDecision !== 'refuse'" label="可驳回至" prop="rejectTo">
                <el-select v-model="form.rejectTo" placeholder="请选择" @change="changeRejectTo">
                  <el-option
                    v-for="item in reject"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="审批结果通知发起人" prop="isMessage">
                <el-select v-model="form.isMessage" placeholder="请选择" @change="changeMessage">
                  <el-option
                    v-for="item in messageData"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关联业务:">
                {{ form.business }}
              </el-form-item>
              <el-form-item v-if="form.selectDecision !== 'refuse'" label="驳回修改数据" prop="changeData">
                <el-select v-model="form.changeData" placeholder="请选择" @change="changeData">
                  <el-option
                    v-for="item in rejectChangeData"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </detail-page>

      <detail-block title="流程配置" />
      <setting ref="processSetting" :get-row-data="getRowData" :select-form-id="selectFormId" />
    </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>
