<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <QueryAgvTask />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button
            v-permission="permission.edit"
            slot="right"
            class="filter-item"
            type="success"
            size="mini"
            v-show="true"
            :loading="crud.delAllLoading"
            :disabled="crud.selections.length != 1 || crud.selections[0].status!='OPEN'"
            @click="butSendAgvTask(crud.selections[0].id)"
        >
          发送
        </el-button>
        <el-button
            v-permission="permission.edit"
            slot="right"
            class="filter-item"
            type="info"
            size="mini"
            v-show="true"
            disabled ="false"
            :loading="crud.delAllLoading"
            @click="cancelInboundTask(crud.selections[0])"
        >
          取消入库
        </el-button>
<!--        :disabled="!(crud.selections.length === 1 && crud.selections[0] && (crud.selections[0].status === 'ATCALL' || crud.selections[0].status === 'ATWORK'))"            @click="cancelInboundTask(crud.selections[0])"-->

      </crudOperation>
      <!--   顶升  -->
      <el-dialog title="顶升" :visible.sync="agvTaskCallbackStatus" width="400px">
        <el-form ref="form1" :model="form1" :rules="rules" size="small" label-width="80px">
          <el-form-item label="容器号" prop="boxCode">
            <el-input v-model="form1.boxCode" style="width: 200px;"/>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="agvTaskCallbackStatus = false">取 消</el-button>
          <el-button type="primary" @click="agvTaskCallbackMethod()">确 定</el-button>
        </span>
      </el-dialog>
      <el-radio-group v-model="radio3" @change="clickChange" size="small">
        <el-radio-button label="未完成">未完成</el-radio-button>
        <el-radio-button label="全部"> 全部</el-radio-button>
      </el-radio-group>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="任务编码" prop="jobId">
            <el-input v-model="form.jobId" style="width: 190px;"/>
          </el-form-item>
          <el-form-item label="容器类型" prop="stockTypeCode">
            <el-input v-model="form.stockTypeCode" style="width: 190px;"/>
          </el-form-item>
          <el-form-item label="起点位置" prop="startSlotCode">
            <el-input v-model="form.startSlotCode" style="width: 190px;"/>
          </el-form-item>
          <el-form-item label="终点位置" prop="endSlotCode">
            <el-input v-model="form.endSlotCode" style="width: 190px;"/>
          </el-form-item>
          <el-form-item label="任务状态" prop="status">
            <el-select v-model="form.status" filterable placeholder="请选择">
              <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="任务类型">
            <el-input v-model="form.type" style="width: 190px;"/>
          </el-form-item>
          <el-form-item label="工作类型" prop="jobType">
            <el-select v-model="form.jobType" filterable placeholder="请选择">
              <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="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table border
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        height="58vh"
        style="width: 100%;"
        :default-sort="{prop: 'id', order: 'descending'}"
        @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="任务ID"/>
        <el-table-column prop="jobId" label="任务编码"/>
        <el-table-column prop="stockTypeCode" width="170" label="容器类型"/>
        <el-table-column prop="startSlotCode" label="起点位置" width="130"/>
        <el-table-column prop="endSlotCode" label="终点位置" width="130" sortable :formatter="formatter"/>
        <el-table-column prop="status" label="任务状态">
          <template slot-scope="scope">
            {{ dict.label.task_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="任务类型">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column prop="jobType" label="工作类型">
          <template slot-scope="scope">
            <span v-if="scope.row.jobType !=null">{{ dict.label.task_type[scope.row.jobType] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="jobPriority" label="优先级" width="60"/>
        <el-table-column prop="reqMessage" label="发送报文" :show-overflow-tooltip="true" width="150"/>
        <el-table-column prop="jobMessage" label="返回报文" :show-overflow-tooltip="true" width="150"/>
        <el-table-column prop="createTime" label="创建时间" width="140"/>
        <el-table-column prop="startTime" label="开始时间" width="140"/>
        <el-table-column prop="endTime" label="完成时间" width="140"/>
        <el-table-column
            :show-overflow-tooltip="false"
          fixed="right"
          label="操作"
          width="140">
          <template slot-scope="scope">
            <el-button type="primary" :disabled=" scope.row.status!='OPEN' "
                       align="center" size="mini"
                       style="float: right" @click="butSendAgvTask(scope.row.id)">
              发送
            </el-button>
            <el-button type="danger"
                       align="center"
                       size="mini"
                       style="float: left" @click="toAgvTaskFinish(scope.row.id)">
              异常
            </el-button>
          </template>
        </el-table-column>

      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudAgvTask, {missionStateCallback,sendAgvTask} from '@/api/agvTask'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from "@/components/DateRangePicker/index.vue";
import DonMessage from "@/utils/message";
import QueryAgvTask from './queryAgvTask';

const defaultForm = {
  id: null,
  stockCode: null,
  stockTypeCode: null,
  startSlotCode: null,
  endSlotCode: null,
  slotCode: null,
  status: null,
  type: null,
  jobId: null,
  jobType: null,
  jobPriority: null,
  jobPriorityType: null,
  jobForce: null,
  jobForceAsc: null,
  jobMessage: null,
  jobMessageTime: null,
  createTime: null,
  startTime: null,
  endTime: null
}
export default {
  name: 'AgvTask',
  components: {DateRangePicker, pagination, crudOperation, rrOperation, udOperation, QueryAgvTask},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['base_staus', 'task_status', 'task_type'],
  cruds() {
    return CRUD({
      title: 'agvTask',
      url: 'api/agvTask',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudAgvTask},
      optShow: {
        add: false,
        edit: true,
        del: false,
        reset: false,
        download: true
      },
      queryOnPresenterCreated: false //不加载
    })
  },
  data() {
    return {
      radio3: '未完成',
      permission: {
        add: ['admin', 'agvTask:add'],
        edit: ['admin', 'agvTask:edit'],
        del: ['admin', 'agvTask:del']
      },
      MissionStateCallback: {
        missionCode: '',
        containerCode: '',
        missionStatus: ''
      },
      rules: {
        stockTypeCode: [
          {required: true, message: '容器类型不能为空', trigger: 'blur'}
        ],
        startSlotCode: [
          {required: true, message: '起点位置不能为空', trigger: 'blur'}
        ],
        endSlotCode: [
          {required: true, message: '终点位置不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '任务状态不能为空', trigger: 'blur'}
        ],
        jobType: [
          {required: true, message: '工作类型不能为空', trigger: 'blur'}
        ]
      },
      queryTypeOptions: [
        {key: 'dstStockCode', display_name: '终点位置'},
        {key: 'srcLocCode', display_name: '起点位置'},
        {key: 'stockCode', display_name: '容器码'},
        {key: 'stockTypeCode', display_name: '容器类型'},
        {key: 'startSlotCode', display_name: '起点位置'},
        {key: 'endSlotCode', display_name: '终点位置'},
        {key: 'slotCode', display_name: '容器当前位置'},
        {key: 'status', display_name: '任务状态'},
        {key: 'type', display_name: '任务类型'},
        {key: 'jobId', display_name: '下游任务号'},
        {key: 'jobMessage', display_name: '返回报文'}
      ],
      // boxCode
      agvTaskCallbackStatus: false,
      form1: {boxCode: null, missionCode: null}
    }
  },
  mounted() {
    this.resetQuery()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    formatter(row, column) {
      return row.endSlotCode;
    },
    toAgvTaskFinish(id) {// 异常
      crudAgvTask.toAgvTaskFinish(id).then(res => {
        this.crud.notify(res.message, res.status);
        this.crud.toQuery()
      })
    },
    butSendAgvTask(id) {//发送
      crudAgvTask.sendAgvTask(id).then(res => {
        this.crud.notify(res.message, res.status);
        this.crud.toQuery()
      })
    },
    cancelInboundTask(row) {//取消入库
      crudAgvTask.cancelInboundTask(row.id).then(res => {
        this.crud.notify(res.message, res.status);
        this.crud.toQuery()
      })
    },
    agvTaskCallbackMethod() {//顶升
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          //UP_CONTAINER
          let date_ = {missionCode: null, containerCode: null, missionStatus: null};
          date_.missionCode = this.form1.missionCode;
          date_.containerCode = this.form1.boxCode;
          date_.missionStatus = "UP_CONTAINER";
          missionStateCallback(date_).then(res => {
            this.agvTaskCallbackStatus = false;
            this.crud.notify("顶升成功!", CRUD.NOTIFICATION_TYPE.SUCCESS);
            this.crud.toQuery()
            this.form1 = {boxCode: null, missionCode: null};
          }).catch(e => {
            this.crud.notify("顶升失败!", CRUD.NOTIFICATION_TYPE.ERROR);
          })
        } else {
          return false;
        }
      });
    },
    popUpBox(id) {//弹出顶升盒子
      this.form1.missionCode = id;
      this.agvTaskCallbackStatus = true;
    },
    resetQuery() {
      this.radio3 = '未完成';
      this.clickChange(this.radio3)
    },
    clickChange(lab) {
      if (lab === "全部") {
        this.crud.resetQuery();
      } else if (lab === "未完成") {

        this.query.status = ['OPEN', 'ATCALL', 'ATWORK']
        this.crud.toQuery();
      }
    }
  }
}
</script>

<style scoped>

</style>
