<template>
  <div class="page">
    <ContentWrap>
      <div class="top-header">
        <span class="title">发起任务</span>
        <div>
          <el-button :icon="RefreshRight" @click="resetFormData">重置</el-button>
          <el-button type="primary" :icon="Position" @click="submit" :disabled="formLoading"
            >确认</el-button
          >
        </div>
      </div>
    </ContentWrap>

    <el-form :model="formData" label-width="94" ref="formRef">
      <el-card shadow="never">
        <template #header>
          <div class="card-header">
            <span class="line"></span>
            <span>任务基本信息</span>
          </div>
        </template>
        <div>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item required label="任务号" prop="taskNo" label-width="146">
                <el-input v-model="formData.taskNo" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-row>
          <div
            v-if="
              formData.taskDetailList[0].taskType !== 2 && formData.taskDetailList[0].taskType !== 3
            "
          >
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item required label="是否拼接任务" prop="montageTask" label-width="146">
                  <el-select
                    v-model="formData.montageTask"
                    placeholder="请选择"
                    @change="montageTaskChange"
                  >
                    <el-option label="不拼接" :value="0" />
                    <el-option label="拼接" :value="1" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12" v-if="formData.montageTask === 1">
                <el-form-item required label="任务拼接数量" prop="montageNumber" label-width="146">
                  <el-input-number
                    class="!w-220px"
                    v-model="formData.montageNumber"
                    :min="1"
                    :max="10"
                    @change="montageNumberChange"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <div
              v-if="
                formData.taskDetailList[0].taskType === 1 ||
                formData.taskDetailList[0].taskType === 5 ||
                formData.taskDetailList[0].taskType === 6
              "
            >
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="搬空所选线库/区域"
                    prop="doMoveAll"
                    label-width="146"
                  >
                    <el-switch
                      v-model="formData.doMoveAll"
                      :active-value="1"
                      :inactive-value="0"
                      @change="doMoveAllChange"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item required label="任务循环" prop="doCycle" label-width="146">
                    <el-switch
                      v-model="formData.doCycle"
                      :active-value="1"
                      :inactive-value="0"
                      @change="doCycleChange"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12" v-if="formData.doCycle === 1">
                  <el-form-item required label="循环次数" prop="cycleNumber" label-width="146">
                    <el-input-number class="!w-220px" v-model="formData.cycleNumber" :min="0" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="物料信息" prop="skuInfo" label-width="146">
                    <el-input v-model="formData.skuInfo" placeholder="请输入物料信息" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="优先级" prop="priority" label-width="146">
                    <el-input-number
                      placeholder="优先级范围为1-100"
                      v-model="formData.priority"
                      :min="1"
                      :max="100"
                      class="!w-220px"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="其他信息" prop="otherMsg" label-width="146">
                    <el-input
                      v-model="formData.otherMsg"
                      placeholder="请输入其他信息"
                      :rows="2"
                      type="textarea"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </el-card>

      <el-card shadow="never" class="mt-4">
        <template #header>
          <div class="card-header">
            <span class="line"></span>
            <span>任务详情</span>
          </div>
        </template>
        <div class="task-list">
          <div
            v-for="(detailItem, index) in formData.taskDetailList"
            :key="index"
            class="task-item"
          >
            <div class="index-logo">{{ index + 1 }}</div>

            <el-row :gutter="24">
              <el-col :span="24">
                <el-form-item
                  required
                  label="任务类型"
                  :prop="`taskDetailList[${index}].taskType`"
                  :rules="{ required: true, message: '任务类型不能为空', trigger: 'change' }"
                >
                  <el-select
                    :disabled="formData.doCycle == 1 || formData.doMoveAll == 1"
                    v-model="detailItem.taskType"
                    placeholder="请选择"
                    @change="taskTypeChange(detailItem, index)"
                  >
                    <el-option label="取放货" :value="1" />
                    <el-option label="停车" :value="2" />
                    <el-option label="充电" :value="3" />
                    <el-option label="移动" :value="4" />
                    <el-option label="仅取货" :value="5" />
                    <el-option label="仅放货" :value="6" />
                    <el-option label="扫描码" :value="7" />
                    <el-option label="检测托盘类型" :value="8" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 取放货 -->
            <div v-if="detailItem.taskType === 1">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="取货类型"
                    :prop="`taskDetailList[${index}].takeType`"
                    :rules="{ required: true, message: '取货类型不能为空', trigger: 'change' }"
                  >
                    <el-select
                      v-model="detailItem.takeType"
                      placeholder="请选择取货类型"
                      @change="takeTypeChange(detailItem)"
                    >
                      <el-option label="库位" :value="1" />
                      <el-option label="线库" :value="2" />
                      <el-option label="区域" :value="3" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="取货位置"
                    :prop="`taskDetailList[${index}].takeId`"
                    :rules="{ required: true, message: '取货位置不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.takeType"
                      v-model="detailItem.takeId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入取货位置"
                      :remote-method="
                        (query) => {
                          takeRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.takeList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="放货类型"
                    :prop="`taskDetailList[${index}].releaseType`"
                    :rules="{ required: true, message: '放货类型不能为空', trigger: 'change' }"
                  >
                    <el-select
                      v-model="detailItem.releaseType"
                      placeholder="请选择放货类型"
                      @change="releaseTypeChange(detailItem)"
                    >
                      <el-option label="库位" :value="1" />
                      <el-option label="线库" :value="2" />
                      <el-option label="区域" :value="3" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="放货位置"
                    :prop="`taskDetailList[${index}].releaseId`"
                    :rules="{ required: true, message: '放货位置不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.releaseType"
                      v-model="detailItem.releaseId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入放货位置"
                      :remote-method="
                        (query) => {
                          releaseRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.releaseList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="指定车辆">
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotNo"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 停车 -->
            <div v-if="detailItem.taskType === 2">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="车辆编号"
                    :prop="`taskDetailList[${index}].robotNo`"
                    :rules="{ required: true, message: '车辆不能为空', trigger: 'change' }"
                  >
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="停车后锁定"
                    :prop="`taskDetailList[${index}].needLock`"
                    :rules="{ required: true, message: '请选择停车后是否锁定', trigger: 'change' }"
                  >
                    <el-select v-model="detailItem.needLock" placeholder="停车后锁定">
                      <el-option label="是" :value="0" />
                      <el-option label="否" :value="1" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 充电 -->
            <div v-if="detailItem.taskType === 3">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="车辆编号"
                    :prop="`taskDetailList[${index}].robotNo`"
                    :rules="{ required: true, message: '车辆不能为空', trigger: 'change' }"
                  >
                    <el-select
                      v-model="detailItem.robotNo"
                      placeholder="请选择车辆"
                      @change="
                        (e) => {
                          robotNoChange(e, detailItem)
                        }
                      "
                    >
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="所选车辆电量"
                    :prop="`taskDetailList[${index}].electricity`"
                    :rules="{ required: true, message: '所选车辆电量不能为空', trigger: 'change' }"
                  >
                    <el-input
                      v-model="detailItem.electricity"
                      placeholder="请输入车辆电量"
                      :disabled="true"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 移动 -->
            <div v-if="detailItem.taskType === 4">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="车辆编号"
                    :prop="`taskDetailList[${index}].robotNo`"
                    :rules="{ required: true, message: '车辆不能为空', trigger: 'change' }"
                  >
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="终点库位"
                    :prop="`taskDetailList[${index}].releaseId`"
                    :rules="{ required: true, message: '终点库位不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.takeType"
                      v-model="detailItem.releaseId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入终点库位"
                      :remote-method="
                        (query) => {
                          releaseRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.releaseList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 仅取货 -->
            <div v-if="detailItem.taskType === 5">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="取货类型"
                    :prop="`taskDetailList[${index}].takeType`"
                    :rules="{ required: true, message: '取货类型不能为空', trigger: 'change' }"
                  >
                    <el-select
                      v-model="detailItem.takeType"
                      placeholder="请选择取货类型"
                      @change="takeTypeChange(detailItem)"
                    >
                      <el-option label="库位" :value="1" />
                      <el-option label="线库" :value="2" />
                      <el-option label="区域" :value="3" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="取货位置"
                    :prop="`taskDetailList[${index}].takeId`"
                    :rules="{ required: true, message: '取货位置不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.takeType"
                      v-model="detailItem.takeId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入取货位置"
                      :remote-method="
                        (query) => {
                          takeRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.takeList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="车辆编号"
                    :prop="`taskDetailList[${index}].robotNo`"
                    :rules="{ required: true, message: '车辆不能为空', trigger: 'change' }"
                  >
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 仅放货 -->
            <div v-if="detailItem.taskType === 6">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="放货类型"
                    :prop="`taskDetailList[${index}].releaseType`"
                    :rules="{ required: true, message: '放货类型不能为空', trigger: 'change' }"
                  >
                    <el-select
                      v-model="detailItem.releaseType"
                      placeholder="请选择放货类型"
                      @change="releaseTypeChange(detailItem)"
                    >
                      <el-option label="库位" :value="1" />
                      <el-option label="线库" :value="2" />
                      <el-option label="区域" :value="3" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="放货位置"
                    :prop="`taskDetailList[${index}].releaseId`"
                    :rules="{ required: true, message: '放货位置不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.releaseType"
                      v-model="detailItem.releaseId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入放货位置"
                      :remote-method="
                        (query) => {
                          releaseRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.releaseList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item required label="指定车辆" prop="montageTask">
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 扫描码 -->
            <div v-if="detailItem.taskType === 7">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="车辆编号"
                    :prop="`taskDetailList[${index}].robotNo`"
                    :rules="{ required: true, message: '车辆不能为空', trigger: 'change' }"
                  >
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="放货位置"
                    :prop="`taskDetailList[${index}].releaseId`"
                    :rules="{ required: true, message: '放货位置不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.releaseType"
                      v-model="detailItem.releaseId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入放货位置"
                      :remote-method="
                        (query) => {
                          releaseRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.releaseList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <!-- 检测托盘类型 -->
            <div v-if="detailItem.taskType === 8">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item
                    required
                    label="车辆编号"
                    :prop="`taskDetailList[${index}].robotNo`"
                    :rules="{ required: true, message: '车辆不能为空', trigger: 'change' }"
                  >
                    <el-select v-model="detailItem.robotNo" placeholder="请选择车辆">
                      <el-option
                        v-for="car in robotList"
                        :key="car.id"
                        :label="car.robotList"
                        :value="car.robotNo"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    required
                    label="检测位置"
                    :prop="`taskDetailList[${index}].releaseId`"
                    :rules="{ required: true, message: '检测位置不能为空', trigger: 'change' }"
                  >
                    <el-select
                      :disabled="!detailItem.releaseType"
                      v-model="detailItem.releaseId"
                      filterable
                      remote
                      reserve-keyword
                      placeholder="请输入检测位置"
                      :remote-method="
                        (query) => {
                          releaseRemoteMethod(query, detailItem)
                        }
                      "
                      :loading="loading"
                    >
                      <el-option
                        v-for="item in detailItem.releaseList"
                        :key="item.id"
                        :label="item.locationNo"
                        :value="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </el-card>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { RefreshRight, Position } from '@element-plus/icons-vue'
import * as MapTaskAPi from '@/api/map/mapTask'

defineOptions({ name: 'taskManagementCreateTask' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const { push } = useRouter()

const formData = ref({
  montageTask: 1, //是否拼接任务（0：不拼接、1：拼接）
  montageNumber: 1, //	拼接任务数量
  taskDetailList: [
    {
      taskType: 1, //任务类型（1：取放货、2：停车、 3：充电、4：移动、5：仅取货、6：仅放货、7：扫描码、8：检测托盘类型）
      releaseType: 1, //放货类型 1：库位、2：线库、 3：区域
      takeType: 1, //取货类型（1：库位、2：线库、 3：区域）
      releaseId: undefined, //放货位置的id
      releaseList: [], //放货的名称的列表
      takeId: undefined, //取货位置的id
      takeList: [], //取货位置的名称列表
      robotNo: undefined, //AGV编号
      needLock: 0, //停车后锁定(0:否、1:是)
      electricity: undefined //所选车辆电量(充电模式)
    }
  ], //任务列表
  skuInfo: undefined, //物料信息
  skuBatch: undefined, //	物料批次号
  skuNumber: undefined, // 物料数量
  priority: 50, //	优先级
  otherMsg: undefined, //	其他信息
  doCycle: 0, //循环(0:不循环、1：循环)
  doMoveAll: 0, //是否搬空所选线库/区域(0:不搬空、1：搬空)
  cycleNumber: 0, //	循环次数
  remainingCycleNumber: 1, //剩余循环次数，默认1
  taskNo: undefined, //	任务号
  taskStatus: 0, //任务状态(0:未开始、1：执行中、2：已完成、3：已取消)
  taskStage: 0, //任务阶段(0:待执行、1：前往取货、2：取货中、3：运输中、4：放货中、5：结束)
  startTime: 0, //开始时间
  endTime: 0 //结束时间
})

//重置
const resetFormData = () => {
  formData.value = {
    montageTask: 1, //是否拼接任务（0：不拼接、1：拼接）
    montageNumber: 1, //	拼接任务数量
    taskDetailList: [
      {
        taskType: 1, //任务类型（1：取放货、2：停车、 3：充电、4：移动、5：仅取货、6：仅放货、7：扫描码、8：检测托盘类型）
        releaseType: 1, //放货类型 1：库位、2：线库、 3：区域
        takeType: 1, //取货类型（1：库位、2：线库、 3：区域）
        releaseId: undefined, //放货位置的id
        releaseList: [], //放货的名称的列表
        takeId: undefined, //取货位置的id
        takeList: [], //取货位置的名称列表
        robotNo: undefined, //AGV编号
        needLock: 0, //停车后锁定(0:否、1:是)
        electricity: undefined //所选车辆电量(充电模式)
      }
    ], //任务列表
    skuInfo: undefined, //物料信息
    skuBatch: undefined, //	物料批次号
    skuNumber: undefined, // 物料数量
    priority: 50, //	优先级
    otherMsg: undefined, //	其他信息
    doCycle: 0, //循环(0:不循环、1：循环)
    doMoveAll: 0, //是否搬空所选线库/区域(0:不搬空、1：搬空)
    cycleNumber: 0, //	循环次数
    remainingCycleNumber: 1, //剩余循环次数，默认1
    taskStatus: 0, //任务状态(0:未开始、1：执行中、2：已完成、3：已取消)
    taskStage: 0, //任务阶段(0:待执行、1：前往取货、2：取货中、3：运输中、4：放货中、5：结束)
    startTime: 0, //开始时间
    endTime: 0, //结束时间
    taskNo: undefined //	任务号
  }
  getTaskNo()
  formRef.value?.resetFields()
}

//获取任务号
const getTaskNo = async () => {
  formData.value.taskNo = await MapTaskAPi.getTaskNo()
}

//获取取货位置可选的列表
const getLocationList = async (type, locationNo) => {
  return await MapTaskAPi.getLocationByName({
    type, //	放货类型（1：库位、2：线库、 3：区域）
    locationNo
  })
}
//放货的选择列表
const loading = ref(false)
const releaseRemoteMethod = async (query, item) => {
  if (query) {
    item.releaseList = await getLocationList(item.releaseType, query)
  } else {
    item.releaseList = []
  }
}
//取货的选择列表
const takeRemoteMethod = async (query, item) => {
  if (query) {
    item.takeList = await getLocationList(item.takeType, query)
  } else {
    item.takeList = []
  }
}

//查询能用的车辆
const robotList = ref([])
const getCanUseRobotList = async () => {
  robotList.value = await MapTaskAPi.getCanUseRobot()
}

//车辆切换
const robotNoChange = (e, detailItem) => {
  let car = robotList.value.filter((item) => {
    return item.robotNo == e
  })
  detailItem.electricity = car[0].electricity
}

//任务类型切换
const taskTypeChange = (item, index) => {
  item.releaseType = 1
  item.takeType = 1
  item.releaseId = undefined
  item.releaseList = []
  item.takeId = undefined
  item.takeList = []
  item.robotNo = undefined
  item.needLock = 0
  item.electricity = undefined
  item.otherMsg = undefined

  let lastItem = formData.value.taskDetailList[index - 1]
  // let nextItem = formData.value.taskDetailList[index + 1]

  if (index === 0) {
    formData.value.doCycle = 0
    formData.value.cycleNumber = 0

    if (item.taskType === 2) {
      formData.value.montageTask = 0
      formData.value.montageNumber = 1
      formData.value.taskDetailList = formData.value.taskDetailList.slice(0, 1)
      message.warning('任务1为停车不可拼接任务')
    }
    if (item.taskType === 3) {
      formData.value.montageTask = 0
      formData.value.montageNumber = 1
      formData.value.taskDetailList = formData.value.taskDetailList.slice(0, 1)
      message.warning('任务1为充电不可拼接任务')
    }
  } else {
    //先判断前一个是什么
    if (lastItem.taskType === 1) {
      if (item.taskType === 2) {
        message.warning('取放货后不可连接停车')
        item.taskType = undefined
        return
      }
      if (item.taskType === 3) {
        message.warning('取放货后不可连接充电')
        item.taskType = undefined
        return
      }
      if (item.taskType === 6) {
        message.warning('取放货后不可连接仅放货')
        item.taskType = undefined
        return
      }
      if (item.taskType === 7) {
        message.warning('取放货后不可连接扫描码')
        item.taskType = undefined
        return
      }
      if (item.taskType === 8) {
        message.warning('取放货后不可连接检测托盘类型')
        item.taskType = undefined
        return
      }
    } else if (lastItem.taskType === 2) {
      //不能拼接任务
    } else if (lastItem.taskType === 3) {
      //不能拼接任务
    } else if (lastItem.taskType === 4) {
      if (item.taskType === 2) {
        message.warning('移动后不可连接停车')
        item.taskType = undefined
        return
      }
      if (item.taskType === 3) {
        message.warning('移动后不可连接充电')
        item.taskType = undefined
        return
      }
      if (item.taskType === 6) {
        message.warning('移动后不可连接仅放货')
        item.taskType = undefined
        return
      }
    } else if (lastItem.taskType === 5) {
      if (item.taskType === 1) {
        message.warning('仅取货后不可连接取放货')
        item.taskType = undefined
        return
      }
      if (item.taskType === 2) {
        message.warning('仅取货后不可连接停车')
        item.taskType = undefined
        return
      }
      if (item.taskType === 3) {
        message.warning('仅取货后不可连接充电')
        item.taskType = undefined
        return
      }
      if (item.taskType === 5) {
        message.warning('仅取货后不可连接仅取货')
        item.taskType = undefined
        return
      }
      if (item.taskType === 7) {
        message.warning('仅取货后不可连接扫描码')
        item.taskType = undefined
        return
      }
      if (item.taskType === 8) {
        message.warning('仅取货后不可连接检测托盘类型')
        item.taskType = undefined
        return
      }
    } else if (lastItem.taskType === 6) {
      if (item.taskType === 2) {
        message.warning('仅放货后不可连接停车')
        item.taskType = undefined
        return
      }
      if (item.taskType === 3) {
        message.warning('仅放货后不可连接充电')
        item.taskType = undefined
        return
      }
      if (item.taskType === 6) {
        message.warning('仅放货后不可连接仅放货')
        item.taskType = undefined
        return
      }
      if (item.taskType === 7) {
        message.warning('仅放货后不可连接扫描码')
        item.taskType = undefined
        return
      }
    } else if (lastItem.taskType === 7) {
      if (item.taskType === 2) {
        message.warning('扫描码后不可连接停车')
        item.taskType = undefined
        return
      }
      if (item.taskType === 3) {
        message.warning('扫描码后不可连接充电')
        item.taskType = undefined
        return
      }
      if (item.taskType === 6) {
        message.warning('扫描码后不可连接仅放货')
        item.taskType = undefined
        return
      }
      if (item.taskType === 7) {
        message.warning('扫描码后不可连接扫描码')
        item.taskType = undefined
        return
      }
    } else if (lastItem.taskType === 8) {
      if (item.taskType === 2) {
        message.warning('检测托盘类型后不可连接停车')
        item.taskType = undefined
        return
      }
      if (item.taskType === 3) {
        message.warning('检测托盘类型后不可连接充电')
        item.taskType = undefined
        return
      }
      if (item.taskType === 6) {
        message.warning('检测托盘类型后不可连接仅放货')
        item.taskType = undefined
        return
      }
    }
  }
}

//搬空区域改变
const doMoveAllChange = (e) => {
  if (e === 1) {
    formData.value.taskDetailList.forEach((item) => {
      item.taskType = 1
    })
  }
}

//拼接的任务数量改变
const montageNumberChange = (currentValue: number, oldValue: number) => {
  if (currentValue > oldValue) {
    //增加
    let addLength = currentValue - formData.value.taskDetailList.length
    for (let index = 0; index < addLength; index++) {
      formData.value.taskDetailList.push({
        taskType: 1, //任务类型（1：取放货、2：停车、 3：充电、4：移动、5：仅取货、6：仅放货、7：扫描码、8：检测托盘类型）
        releaseType: 1, //放货类型 1：库位、2：线库、 3：区域
        takeType: 1, //取货类型（1：库位、2：线库、 3：区域）
        releaseId: undefined, //放货位置的id
        releaseList: [], //放货的名称的列表
        takeId: undefined, //取货位置的id
        takeList: [], //取货位置的名称列表
        robotNo: undefined, //AGV编号
        needLock: 0, //停车后锁定(0:否、1:是)
        electricity: undefined //所选车辆电量(充电模式)
      })
    }
  } else {
    //减少
    formData.value.taskDetailList = formData.value.taskDetailList.slice(0, currentValue)
  }
}

//是否拼接切换
const montageTaskChange = (e) => {
  if (e === 0) {
    formData.value.montageNumber = 1
    formData.value.taskDetailList = formData.value.taskDetailList.slice(0, 1)
  }
}

//任务循环切换
const doCycleChange = (e) => {
  if (e === 0) {
    formData.value.cycleNumber = 0
  } else {
    formData.value.cycleNumber = 1
    formData.value.montageNumber = 1
    formData.value.taskDetailList = formData.value.taskDetailList.slice(0, 1)
    formData.value.taskDetailList.forEach((item) => {
      item.taskType = 1
    })
  }
}

//取货类型切换
const takeTypeChange = (item) => {
  item.takeId = undefined
}

//放货类型切换
const releaseTypeChange = (item) => {
  item.releaseId = undefined
}

//提交表单
const formRef = ref()
const formLoading = ref(false)
//校验数组的

const submit = async () => {
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      formLoading.value = true
      try {
        await MapTaskAPi.createTask(formData.value)
        message.success(t('common.createSuccess'))
        resetFormData()
        push({ name: 'taskManagementTaskList' })
      } finally {
        formLoading.value = false
      }
    } else {
      message.warning('请补充完整表单信息！')
    }
  })
}

/** 初始化 **/
onMounted(() => {
  getTaskNo()
  getCanUseRobotList()
})
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  align-items: center;

  .line {
    display: block;
    width: 4px;
    height: 14px;
    background: #0147eb;
    margin-right: 8px;
  }
}

.page {
  .top-header {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .title {
      font-family:
        PingFangSC,
        PingFang SC;
      font-weight: 500;
      font-size: 16px;
      color: #0d162a;
      line-height: 25px;
      text-align: center;
      font-style: normal;
    }
  }

  .task-list {
    display: flex;
    flex-wrap: wrap;
    padding: 6px 0 0 0;

    .task-item {
      border: 1px solid #e2e7f5;
      box-shadow: rgba(0, 0, 0, 0.05) 0px 0px 0px 1px;
      width: 47%;
      padding: 20px 20px 10px 20px;
      border-radius: 4px;
      margin: 0 11px 24px 11px;
      box-sizing: border-box;
      position: relative;

      .index-logo {
        width: 18px;
        height: 18px;
        line-height: 18px;
        background: #1989fa;
        font-size: 13px;
        text-align: center;
        border-radius: 50%;
        color: #fff;
        position: absolute;
        top: 10px;
        left: 10px;
      }
    }
  }
}
</style>
