<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-width="80px"
          label-suffix=":"
        >
          <el-form-item label="任务号">
            <label slot="label">任&nbsp;&nbsp;务&nbsp;号:</label>
            <el-input
              v-model="query.task_code"
              clearable
              style="width: 180px"
              placeholder="任务号"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="载具号">
            <label slot="label">载&nbsp;&nbsp;具&nbsp;号:</label>
            <el-input
              v-model="query.vehicle_code"
              clearable
              style="width: 180px"
              placeholder="载具号"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="起始点位">
            <el-input
              v-model="query.start_point_code"
              clearable
              style="width: 180px"
              placeholder="起点"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="下一点位">
            <el-input
              v-model="query.next_point_code"
              clearable
              style="width: 180px"
              placeholder="终点"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="任务类型">
            <el-select
              filterable
              v-model="query.task_type"
              style="width: 180px"
              placeholder="任务类型"
              class="filter-item"
              clearable
              @change="hand"
            >
              <el-option
                v-for="item in dict.SCH_TASK_TYPE"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="任务明细">
            <el-select
              filterable
              v-model="query.taskdtl_type"
              style="width: 180px"
              placeholder="任务类型"
              class="filter-item"
              clearable
              @change="hand"
            >
              <el-option
                v-for="item in dict.SCH_TASK_TYPE_DTL"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              @change="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="完成方式">
            <el-select
              v-model="query.finished_type"
              style="width: 180px"
              placeholder="完成方式"
              class="filter-item"
              clearable
              @change="hand"
            >
              <el-option
                v-for="item in finishTypeList"
                :key="item.code"
                clearable
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>


          <el-form-item label="任务状态">
            <el-select
              v-model="query.task_status"
              multiple
              style="width: 400px"
              placeholder="任务状态"
              class="filter-item"
              clearable
              @change="handTaskStatus"
            >
              <el-option
                v-for="item in taskStatusList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <rrOperation />
        </el-form>

      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column v-if="false" prop="taskdtl_id" label="任务标识" />
        <el-table-column prop="task_code" label="任务编码" />
        <el-table-column v-if="false" prop="task_type" label="任务类型" />
        <el-table-column prop="task_type_name" label="任务类型"  />
        <el-table-column v-if="false" prop="taskdtl_type" label="任务明细" />
        <el-table-column prop="taskdtl_type_name" label="任务明细" />
        <el-table-column v-if="false" prop="task_status" label="任务状态" />
        <el-table-column prop="task_status_name" label="任务状态" width="95px" :formatter="formatTaskStatusName" />
        <!--
                <el-table-column v-if="false" prop="finished_type" label="完成方式" />
        -->
        <!--        <el-table-column prop="finished_type_name" label="完成方式" :formatter="formatFinishTypeName"/>-->
        <el-table-column prop="start_area_name" label="起始区域" width="95" show-overflow-tooltip />
        <el-table-column prop="start_point_code" label="起点编码" width="85" />
        <el-table-column prop="start_point_name" label="起点名称" width="105" show-overflow-tooltip/>
        <el-table-column prop="next_area_name" label="下一区域" width="95" show-overflow-tooltip />
        <el-table-column prop="next_point_code" label="下一点编码" width="85" />
        <el-table-column prop="next_point_name" label="下一点名称" width="105" show-overflow-tooltip/>
        <el-table-column prop="vehicle_code" label="载具编码" />
        <el-table-column prop="remark" label="备注" width="120" show-overflow-tooltip />
        <el-table-column prop="update_by" label="修改者" v-if="false"/>
        <el-table-column prop="create_time" label="创建时间" width="135" />
        <el-table-column prop="update_time" label="修改时间" width="135" />
        <el-table-column
          v-permission="['admin','instruction:edit','instruction:del']"
          fixed="right"
          label="操作"
          width="80px"
          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')">完成</el-dropdown-item>
<!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'b')">取消</el-dropdown-item>-->
                <!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'c')">拉回</el-dropdown-item>-->
<!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'d')">重发</el-dropdown-item>-->
<!--                <el-dropdown-item :command="beforeHandleCommand(scope.$index, scope.row,'e')">详情</el-dropdown-item>-->
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog
      title="任务详情"
      :fullscreen="fullscreen"
      :visible.sync="viewDialogVisible"
      width="30%"
    >
      <!--  组件-->
      <component
        :is="currentComponent"
        :task-uuid="taskdtl_id"
        :dialog-visible="viewDialogVisible"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="viewDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="viewDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import crudTask from '@/api/wms/sch/task'
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

export default {
  name: 'Task',
  dicts: ['SCH_TASK_TYPE', 'SCH_TASK_TYPE_DTL'],
  components: {
    pagination, crudOperation, rrOperation
  },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '任务',
      url: 'api/task',
      idField: 'taskdtl_id',
      sort: 'taskdtl_id,desc',
      crudMethod: { ...crudTask },
      query: {
        task_code:'',vehicle_code:'',start_point_code:'',next_point_code:'',task_type:'',taskdtl_type:'',finished_type:'',task_status:""
      },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      viewDialogVisible: false,
      fullscreen: false,
      taskdtl_id: '',
      currentComponent: '',
      openParam: {},
      create_time: [],
      taskStatusList: [],
      taskTypeList: [],
      finishTypeList: [],
      permission: {

      },
      rules: {}
    }
  },
  created() {
    crudTask.getTaskStatus().then(data => {
      this.taskStatusList = data
    })
    crudTask.getFinishType().then(data => {
      this.finishTypeList = data
    })
    this.crud.query.task_status = ['-1']
    this.crud.toQuery()
  },
  methods: {
    hand(value) {
      this.crud.toQuery()
    },
    handTaskStatus(value) {
      if (value) {
        this.query.task_status = this.task_status.toString()
      }
      this.crud.toQuery()
    },
    formatTaskStatusName(row, column) {
      for (const item of this.taskStatusList) {
        if (item.code === row.task_status) {
          return item.name
        }
      }
      return ''
    },
    formatFinishTypeName(row, column) {
      for (const item of this.finishTypeList) {
        if (item.code === row.finished_type) {
          return item.name
        }
      }
      return ''
    },
    beforeHandleCommand(index, row, command) {
      return {
        'index': index,
        'row': row,
        'command': command
      }
    },
    handleCommand(command) {
      let method_name = ''
      switch (command.command) {
        case 'a':// 完成
          method_name = 'forceFinish'
          break
        case 'b':// 取消
          method_name = 'cancel'
          break
        case 'c':// 拉回
          method_name = 'pullBack'
          break
        case 'd':// 重发
          method_name = 'renotifyAcs'
          break
        case 'e':// 详情
          method_name = 'view'
          break
      }
      if (method_name === 'view') {
        this.view(command.row)
        return
      }
      const data = {
        taskdtl_id: command.row.taskdtl_id,
        method_name: method_name
      }
      crudTask.operation(data).then(res => {
        this.crud.toQuery()
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    view(row) {
      this.taskdtl_id = row.taskdtl_id
      switch (row.task_type) {
        case '01':// 入库
          this.fullscreen = true
          this.currentComponent = 'IOStore'
          break
        case '02':// 出库
          this.fullscreen = true
          this.currentComponent = 'IOStore'
          break
        case '03':// 入空载具
          this.crud.notify('入空载具无详情', CRUD.NOTIFICATION_TYPE.INFO)
          return
        case '04':// 出空载具
          this.crud.notify('出空载具无详情', CRUD.NOTIFICATION_TYPE.INFO)
          return
        case '05':// 转储
          this.fullscreen = true
          this.currentComponent = 'DumpInv'
          break
        case '06':// 质检
          this.fullscreen = true
          this.currentComponent = 'Quality'
          break
      }
      this.viewDialogVisible = true
    }

  }

}
</script>

<style scoped>

</style>
