<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-suffix=":"
        >
          <el-form-item :label="$t('task.txt_box.Task_code')">
            <el-input
              v-model="query.task_code"
              size="small"
              clearable
              :placeholder="$t('task.txt_box.Enter_task_number')"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('task.txt_box.Vehicle_number')">
            <el-input
              v-model="query.vehicle_code"
              size="small"
              clearable
              :placeholder="$t('task.txt_box.Vehicle_registration_number')"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('task.txt_box.Material_type')">
            <el-select
              v-model="query.material_type"
              clearable
              filterable
              size="small"
              :placeholder="$t('task.txt_box.Material_type')"
              class="filter-item"
              style="width: 200px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in materialList" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('task.txt_box.Task_status')">
            <el-select
              v-model="query.status"
              clearable
              filterable
              size="small"
              :placeholder="$t('task.txt_box.Task_status')"
              class="filter-item"
              style="width: 200px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in dict.task_status" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('task.txt_box.start_or_end')">
            <el-input
              v-model="query.point_code"
              size="small"
              clearable
              :placeholder="$t('task.txt_box.Origin_or_destination')"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('task.txt_box.Task_type')">
            <el-select
              v-model="query.task_type"
              clearable
              filterable
              size="small"
              :placeholder="$t('task.txt_box.Task_type')"
              class="filter-item"
              style="width: 200px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in dict.task_type" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="formDia=true"
        >
          {{ $t('auto.common.Create') }}
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-refresh"
          @click="reload"
        >
          {{ $t('auto.common.synchronous') }}
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :visible.sync="formDia" :title="crud.status.title" width="1100px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="135px">
          <el-row>
            <el-col :span="12">
              <div class="grid-content bg-purple" />
              <el-form-item v-if="false" label="任务标识" prop="task_id">
                <el-input v-model="form.task_id" style="width: 240px;" />
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.Material_type')">
                <el-select
                  v-model="form.material"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in dict.material_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.Task_type')">
                <el-select
                  v-model="form.task_type"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in dict.task_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-if="form.task_type == '7'" :label="$t('task.txt_box.Warehouse_task_type')">
                <el-select
                  v-model="form.storage_task_type"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in dict.storage_task_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.AGV_system')">
                <el-select
                  v-model="form.agv_system_type"
                  style="width: 370px;"
                  filterable
                  clearable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in dict.agv_system_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.Routing_scheme')">
                <el-select
                  v-model="form.route_plan_code"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  default-first-option
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in routeList"
                    :key="item.plan_code"
                    :label="item.plan_name"
                    :value="item.plan_code"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.select.Start_point')" prop="start_point_code">
                <el-select
                  v-model="form.start_point_code"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="showStartStorage"
                >
                  <el-option
                    v-for="item in deviceList"
                    :key="item.device_code"
                    :label="item.device_code"
                    :value="item.device_code"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="start_flag" :label="$t('task.select.Starting_column')" prop="from_y">
                <el-select
                  v-model="form.from_y"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in fromYList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="start_flag" :label="$t('task.select.Starting_layer')" prop="from_z">
                <el-select
                  v-model="form.from_z"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in fromZList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.select.Destination')" prop="next_point_code">
                <el-select
                  v-model="form.next_point_code"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="showEndStorage"
                >
                  <el-option
                    v-for="item in deviceList"
                    :key="item.device_code"
                    :label="item.device_code"
                    :value="item.device_code"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="end_flag" :label="$t('task.select.Ending_column')" prop="to_y">
                <el-select
                  v-model="form.to_y"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in toYList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="end_flag" :label="$t('task.select.Ending_layer')" prop="to_z">
                <el-select
                  v-model="form.to_z"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in toZList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                    @change="isDisabled=false"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="grid-content bg-purple-light" />
              <el-form-item :label="$t('task.select.Start_point2')" prop="start_point_code2">
                <el-select
                  v-model="form.start_point_code2"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                >
                  <el-option
                    v-for="item in deviceList"
                    :key="item.device_code"
                    :label="item.device_code"
                    :value="item.device_code"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="start_flag" :label="$t('task.select.Starting_column')" prop="from_y2">
                <el-select
                  v-model="form.from_y2"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in fromYList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="start_flag" :label="$t('task.select.Starting_layer')" prop="from_z2">
                <el-select
                  v-model="form.from_z2"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in fromZList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.select.Destination2')" prop="next_point_code2">
                <el-select
                  v-model="form.next_point_code2"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                >
                  <el-option
                    v-for="item in deviceList"
                    :key="item.device_code"
                    :label="item.device_code"
                    :value="item.device_code"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="end_flag" :label="$t('task.select.Ending_column')" prop="to_y2">
                <el-select
                  v-model="form.to_y2"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in toYList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-show="end_flag" :label="$t('task.select.Ending_layer')" prop="to_z2">
                <el-select
                  v-model="form.to_z2"
                  style="width: 370px;"
                  filterable
                  :placeholder="$t('task.select.Placeholder')"
                  @change="isDisabled=false"
                >
                  <el-option
                    v-for="item in toZList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                    @change="isDisabled=false"
                  />
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.Priority')">
                <el-input v-model="form.priority" style="width: 370px;" @change="isDisabled=false" />
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.Vehicle_number')">
                <el-input v-model="form.vehicle_code" style="width: 370px;" @change="isDisabled=false" />
              </el-form-item>
              <el-form-item :label="$t('task.txt_box.extension_field')">
                <div>
                  <el-button @click="openDialog">{{ $t('task.select.Placeholder') }}</el-button>
                </div>
              </el-form-item>
              <el-form-item :label="$t('task.select.Description')" prop="description">
                <el-input v-model="form.remark" style="width: 370px;" type="textarea" @change="isDisabled=false" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="formDia=false">{{ $t('task.select.Cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" :disabled="isDisabled" type="primary" @click="saveBtn">{{ $t('task.select.Confirm') }}</el-button>
        </div>
      </el-dialog>
      <el-dialog :visible.sync="dialogVisible" :title="$t('task.txt_box.extension_field')" @close="closeDialog">
        <el-form>
          <el-form-item>
            <el-button v-if="showAddRowButton" style="color: #6b75e9;" @click="addNewRow">添加新行</el-button>
          </el-form-item>
          <el-form-item>
            <el-form>
              <el-table
                style="width: 100%;"
                :header-cell-style="{'text-align':'center'}"
                :data="extension"
                stripe
                border
              >
                <el-table-column type="index" label="序号" min-width="50" />
                <el-table-column label="扩展字段" prop="name" min-width="115">
                  <template slot-scope="scope">
                    <el-select
                      v-model="scope.row.name"
                      class="extended-field-select"
                      filterable
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="item in dict.kezhuan"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column label="扩展字段值" prop="value" min-width="115">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.value" style="width: 100%" />
                  </template>
                </el-table-column>
                <el-table-column label="操作" min-width="115" align="center">
                  <template slot-scope="scope">
                    <el-button size="mini" type="danger" icon="el-icon-minus" circle @click="deleteField(scope.$index)" />
                  </template>
                </el-table-column>
              </el-table>
            </el-form>
          </el-form-item>
          <div class="button-container">
            <el-button type="danger" @click="cancelForm">取消</el-button>
            <!-- 确认按钮 -->
            <el-button type="primary" @click="saveForm">确认</el-button>
          </div>
        </el-form>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="25" />
        <el-table-column v-if="false" prop="task_id" label="任务标识" />
        <el-table-column prop="task_code" :label="$t('task.txt_box.Task_code')" :min-width="flexWidth('task_code',crud.data,$t('task.txt_box.Task_code'))" />
        <el-table-column prop="task_type" :label="$t('task.txt_box.Task_type')" :min-width="flexWidth('task_type',crud.data,$t('task.txt_box.Task_type'))">
          <template slot-scope="scope">
            {{ dict.label.task_type[scope.row.task_type] }}
          </template>
        </el-table-column>
        <!--        <el-table-column prop="link_num" label="关联编号" />-->
        <el-table-column prop="vehicle_code" :label="$t('task.txt_box.Vehicle_number')" :min-width="flexWidth('vehicle_code',crud.data,$t('task.txt_box.Vehicle_number'))" />
        <el-table-column prop="task_status" :label="$t('task.txt_box.Task_status')" width="150px">
          <template slot-scope="scope">
            <span v-if="scope.row.task_status==='0' ">{{ $t('task.select.Ready') }}</span>
            <span v-if="scope.row.task_status==='1' ">{{ $t('task.select.In_progress') }}</span>
            <span v-if="scope.row.task_status==='2' ">{{ $t('task.select.Completed') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="priority" :label="$t('task.txt_box.Priority')" :min-width="flexWidth('priority',crud.data,$t('task.txt_box.Priority'))" />
        <el-table-column prop="start_point_code" :label="$t('task.select.Pickup_point')" :min-width="flexWidth('start_point_code',crud.data,$t('task.select.Pickup_point'))" />
        <!--        <el-table-column prop="put_point_code" label="倒料点" width="100" />-->
        <el-table-column prop="next_point_code" :label="$t('task.select.Delivery_point')" :min-width="flexWidth('next_point_code',crud.data,$t('task.select.Delivery_point'))" />
        <!--        <el-table-column prop="start_point_code2" label="取货点2" width="120px" />-->
        <!--        <el-table-column prop="next_point_code2" label="放货点2" width="120px" />-->
        <!--        <el-table-column prop="compound_task" label="复合任务">-->
        <!--          <template slot-scope="scope">-->
        <!--            <span v-if="scope.row.compound_task==='0' ">否</span>-->
        <!--            <span v-if="scope.row.compound_task==='1' ">是</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column prop="compound_task_data" width="200" label="复合路线" />-->
        <!--        <el-table-column prop="matarial" label="物料" />-->
        <!--        <el-table-column prop="quantity" label="数量" />-->
        <el-table-column prop="remark" :label="$t('task.select.Remark')" :min-width="flexWidth('remark',crud.data,$t('task.select.Remark'))" />
        <el-table-column prop="create_by" :label="$t('task.select.Creator')" :min-width="flexWidth('create_by',crud.data,$t('task.select.Creator'))" />
        <el-table-column prop="create_time" :label="$t('task.select.Create_time')" :min-width="flexWidth('create_time',crud.data,$t('task.select.Create_time'))" />
        <el-table-column v-permission="['admin','task:edit','task:del']" :label="$t('task.select.Operation')" width="200px" align="center">
          <template slot-scope="scope">
            <!--            <el-dropdown trigger="click" @command="handleCommand">-->
            <!--              <span class="el-dropdown-link">-->
            <!--                <i class="el-icon-menu" />-->
            <!--              </span>-->
            <!--              <el-dropdown-menu slot="dropdown">-->
            <!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'a')">{{ $t('task.select.Completed') }}</el-dropdown-item>-->
            <!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'b')">{{ $t('task.select.Cancel') }}</el-dropdown-item>-->
            <!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'c')">{{ $t('task.select.Create_command') }}</el-dropdown-item>-->
            <!--              </el-dropdown-menu>-->
            <!--            </el-dropdown>-->
            <!--          </template>-->
            <el-dropdown>
              <span class="el-dropdown-link">
                <i class="el-icon-setting">More</i>
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>
                    <el-button
                      type="text"
                      icon="el-icon-finished"
                      @click="handleCommand(scope.$index, scope.row,'a')"
                    >
                      {{ $t('task.select.Completed') }}
                    </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-button
                      type="text"
                      icon="el-icon-circle-close"
                      @click="handleCommand(scope.$index, scope.row,'b')"
                    >
                      {{ $t('task.select.Cancel') }}
                    </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-button
                      type="text"
                      icon="el-icon-folder-add"
                      @click="handleCommand(scope.$index, scope.row,'c')"
                    >
                      {{ $t('task.select.Create_command') }}
                    </el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTask from '@/api/acs/task/task'
import CRUD, { crud, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import deviceCrud from '@/api/acs/device/device'
import routeCurd from '@/api/acs/route/routePlan'
import { getDicts } from '@/views/system/dict/dict'
import i18n from '@/i18n'

export default {
  name: 'Task',
  components: { pagination, crudOperation },
  dicts: ['task_status', 'task_type', 'agv_system_type', 'storage_task_type', 'kezhuan'],
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({ title: i18n.t('task.txt_box.Task'), url: 'api/task', idField: 'task_id', sort: 'task_id,desc',
      optShow: {
        add: false,
        edit: true,
        del: true,
        reset: false,
        download: true
      },
      crudMethod: { ...crudTask }})
  },
  data() {
    const checkFromY = (rule, value, callback) => {
      if (this.start_flag) {
        if (!value) {
          callback(new Error(this.$t('task.select.please_enter_origin_column')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    const checkFromZ = (rule, value, callback) => {
      if (this.start_flag) {
        if (!value) {
          callback(new Error(this.$t('task.select.please_enter_origin_level')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    const checkToY = (rule, value, callback) => {
      if (this.end_flag) {
        if (!value) {
          callback(new Error(this.$t('task.select.please_select_destination_column')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    const checkToZ = (rule, value, callback) => {
      if (this.end_flag) {
        if (!value) {
          callback(new Error(this.$t('task.select.please_select_destination_level')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      formDia: false,
      isDisabled: false,
      deviceList: [],
      materialList: [],
      statusList: [],
      routeList: [],
      task_type: [],
      fromYList: [],
      fromZList: [],
      toYList: [],
      toZList: [],
      start_flag: false,
      end_flag: false,
      permission: {
        add: ['admin', 'task:add'],
        edit: ['admin', 'task:edit'],
        del: ['admin', 'task:del']
      },
      showAddRowButton: true,
      form: {
        task_id: null,
        vehicle_code: null,
        vehicle_type: null,
        task_type: '1',
        storage_task_type: '',
        task_status: null,
        priority: 1,
        start_point_code: null,
        start_point_code2: null,
        start_device_code: null,
        start_device_code2: null,
        next_point_code: null,
        next_point_code2: null,
        remark: null,
        material: null,
        route_plan_code: 'normal',
        from_x: null,
        from_y: null,
        from_z: null,
        to_x: null,
        to_y: null,
        to_z: null,
        from_x2: null,
        from_y2: null,
        from_z2: null,
        to_x2: null,
        to_y2: null,
        to_z2: null,
        agv_system_type: '1',
        interactionJson: null
      },
      extension: [{
        name: '',
        value: ''
      }],
      dialogVisible: false,
      rules: {
        start_point_code: [
          { required: true, message: '起点不能为空', trigger: 'change' }
        ],
        next_point_code: [
          { required: true, message: '终点不能为空', trigger: 'change' }
        ],
        from_y: [
          { validator: checkFromY, trigger: 'change' }
        ],
        from_z: [
          { validator: checkFromZ, trigger: 'change' }
        ],
        to_y: [
          { validator: checkToY, trigger: 'change' }
        ],
        to_z: [
          { validator: checkToZ, trigger: 'change' }
        ]
      }
    }
  },
  created() {
    deviceCrud.selectDeviceList().then(data => {
      this.deviceList = data
    })
    routeCurd.selectList().then(data => {
      this.routeList = data
    })
    getDicts().then(data => {
      this.dicts = data
    })
  },
  methods: {
    addNewRow() {
      this.extension.push({
        name: '',
        value: ''
      })
    },
    deleteField(index) { // 添加字段的时候删除一条数据
      if (this.extension.length > 1) {
        this.extension.splice(index, 1)
      }
    },
    cancelForm() {
      // 清空表单数据
      this.extension = [{
        name: '',
        value: '' }]
      this.dialogVisible = false
    },
    saveForm() {
      // 保存表单数据
      // 你可以在这里处理保存逻辑，并发送请求保存表单数据
      this.form.interactionJson = this.extension.reduce((extension, item) => {
        extension[item.name] = item.value
        return extension
      }, {})
      this.dialogVisible = false
    },
    saveBtn() {
      crudTask.add(this.form).then(res => {
        this.crud.toQuery()
        this.formDia = false
        this.isDisabled = true
      })
      this.extension = [{
        name: '',
        value: '' }]
    },
    openDialog() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    finish(index, row) {
      crudTask.finish(row.task_id).then(res => {
        this.crud.toQuery()
        this.crud.notify('完成成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    cancel(index, row) {
      crudTask.cancel(row.task_id).then(res => {
        this.crud.toQuery()
        this.crud.notify('取消成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    createInst(index, row) {
      crudTask.createInst(row.task_id).then(res => {
        this.crud.toQuery()
        this.crud.notify('创建成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    reload() {
      crudTask.reload().then(res => {
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },

    beforeHandleCommand(index, row, command) {
      return {
        'index': index,
        'row': row,
        'command': command
      }
    },
    handleCommand(index, row, command) {
      switch (command) {
        case 'a':// 完成
          this.finish(index, row)
          break
        case 'b':// 取消
          this.cancel(index, row)
          break
        case 'c':// 创建指令
          this.createInst(index, row)
          break
      }
    },
    showStartStorage(val) {
      let obj = {}
      obj = this.deviceList.find((item) => {
        return item.device_code === val
      })
      if (obj.device_type === 'storage') {
        this.start_flag = true
        let storage_obj = {}
        deviceCrud.queryStorageExtra(obj.device_code).then(data => {
          storage_obj = data
          const n1 = storage_obj.minY
          const n2 = storage_obj.maxY
          const m1 = storage_obj.minZ
          const m2 = storage_obj.maxZ
          const from_y = []
          const from_z = []
          for (let i = n1; i <= n2; i++) {
            const y = {}
            if (i < 10) {
              y.id = '0' + i
            } else {
              y.id = i
            }
            y.value = i + '列'
            from_y.push(y)
          }
          for (let i = m1; i <= m2; i++) {
            const z = {}
            if (i < 10) {
              z.id = '0' + i
            } else {
              z.id = i
            }
            z.value = i + '层'
            from_z.push(z)
          }
          this.fromYList = from_y
          this.fromZList = from_z
          this.form.from_x = storage_obj.tunnel
        })
      } else {
        this.start_flag = false
        this.form.from_x = ''
        this.form.from_y = ''
        this.form.from_z = ''
      }
      this.isDisabled = false
    },
    showEndStorage(val) {
      let obj = {}
      obj = this.deviceList.find((item) => {
        return item.device_code === val
      })
      if (obj.device_type === 'storage') {
        this.end_flag = true
        let storage_obj = {}
        deviceCrud.queryStorageExtra(obj.device_code).then(data => {
          storage_obj = data
          const n1 = storage_obj.minY
          const n2 = storage_obj.maxY
          const m1 = storage_obj.minZ
          const m2 = storage_obj.maxZ
          const to_y = []
          const to_z = []
          for (let i = n1; i <= n2; i++) {
            const y = {}
            if (i < 10) {
              y.id = '0' + i
            } else {
              y.id = i
            }
            y.value = i + '列'
            to_y.push(y)
          }
          for (let i = m1; i <= m2; i++) {
            const z = {}
            if (i < 10) {
              z.id = '0' + i
            } else {
              z.id = i
            }
            z.value = i + '层'
            to_z.push(z)
          }
          this.toYList = to_y
          this.toZList = to_z
          this.form.to_x = storage_obj.tunnel
        })
      } else {
        this.end_flag = false
        this.form.to_x = ''
        this.form.to_y = ''
        this.form.to_z = ''
      }
      this.isDisabled = false
    }
  }
}
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.button-container {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
}
</style>
