<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.point_code"
              clearable
              style="width: 180px"
              placeholder="起点"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="query.createTime"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="crud.toQuery">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="任务状态">
            <el-select
              v-model="query.task_status"
              multiple
              style="width: 360px"
              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-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" :rules="rules" size="mini" label-width="115px" label-suffix=":">
          <el-form-item label="任务编码">
            <el-input v-model="form.task_code" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务分类">
            <el-input v-model="form.task_class_id" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务状态">
            <el-input v-model="form.task_status" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="配置编码">
            <el-input v-model="form.config_code" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="点位1">
            <el-input v-model="form.point_code1" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="点位2">
            <el-input v-model="form.point_code2" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="点位3">
            <el-input v-model="form.point_code3" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="点位4">
            <el-input v-model="form.point_code4" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="载具类型">
            <el-input v-model="form.vehicle_type" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="载具数量">
            <el-input v-model="form.vehicle_qty" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="载具编码">
            <el-input v-model="form.vehicle_code" style="width: 240px;" />
          </el-form-item>
<!--          <el-form-item label="优先级">-->
<!--            <el-input v-model="form.priority" style="width: 240px;" />-->
<!--          </el-form-item>-->
          <el-form-item v-if="false" label="处理类">
            <el-input v-model="form.handle_class" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="处理状态">
            <el-input v-model="form.handle_status" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="车号">
            <el-input v-model="form.car_no" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务组标识">
            <el-input v-model="form.task_group_id" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务组顺序号">
            <el-input v-model="form.task_group_seq" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务完成类型">
            <el-input v-model="form.finished_type" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="生成方式">
            <el-input v-model="form.create_mode" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="链路标识">
            <el-input v-model="form.acs_trace_id" style="width: 240px;" />
          </el-form-item>
          <el-form-item v-if="false" label="生成任务的请求参数">
            <el-input v-model="form.request_param" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="下发任务的请求参数">
            <el-input v-model="form.response_param" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="车间编码">
            <el-input v-model="form.workshop_code" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="额外组盘信息">
            <el-input v-model="form.ext_group_data" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 240px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <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="55" />
        <el-table-column prop="task_code" label="任务编码" :min-width="flexWidth('task_code',crud.data,'任务编码')" />
        <el-table-column prop="vehicle_code" label="载具编码1" :min-width="flexWidth('vehicle_code',crud.data,'载具编码1')" />
        <el-table-column prop="vehicle_code2" label="载具编码2" :min-width="flexWidth('vehicle_code2',crud.data,'载具编码2')" />
        <el-table-column prop="task_class_id" label="任务分类" :min-width="flexWidth('task_class_id',crud.data,'任务分类')" />
        <el-table-column prop="task_status" label="任务状态" :min-width="flexWidth('task_status',crud.data,'任务状态')">
          <template slot-scope="scope">
            {{ getStatusName(scope.row.task_status) }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="提示信息" :min-width="flexWidth('remark',crud.data,'提示信息')" />
        <el-table-column prop="config_code" label="配置编码" :min-width="flexWidth('config_code',crud.data,'配置编码')" />
        <el-table-column prop="point_code1" label="起点1" :min-width="flexWidth('point_code1',crud.data,'点位1')" />
        <el-table-column prop="point_code2" label="终点1" :min-width="flexWidth('point_code2',crud.data,'点位2')" />
        <el-table-column prop="point_code3" label="起点2" :min-width="flexWidth('point_code3',crud.data,'点位3')" />
        <el-table-column prop="point_code4" label="终点2" :min-width="flexWidth('point_code4',crud.data,'点位4')" />
        <el-table-column prop="vehicle_type" label="载具类型" :min-width="flexWidth('vehicle_type',crud.data,'载具类型', 20)">
          <template slot-scope="scope">
            {{ dict.label.vehicle_type[scope.row.vehicle_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="vehicle_qty" label="载具数量" :min-width="flexWidth('vehicle_qty',crud.data,'载具数量')" />
        <el-table-column prop="priority" label="优先级" :min-width="flexWidth('priority',crud.data,'优先级')" />
        <el-table-column v-if="false" prop="handle_class" label="处理类" :min-width="flexWidth('handle_class',crud.data,'处理类')" />
        <el-table-column prop="handle_status" label="处理状态" :min-width="flexWidth('handle_status',crud.data,'处理状态')" />
        <el-table-column prop="car_no" label="车号" :min-width="flexWidth('car_no',crud.data,'车号')" />
        <el-table-column prop="task_group_id" label="任务组标识" :min-width="flexWidth('task_group_id',crud.data,'任务组标识')" />
        <el-table-column prop="task_group_seq" label="任务组顺序号" :min-width="flexWidth('task_group_seq',crud.data,'任务组顺序号')" />
        <el-table-column prop="finished_type" label="任务完成类型" :min-width="flexWidth('finished_type',crud.data,'任务完成类型')" />
        <el-table-column prop="create_mode" label="生成方式" :min-width="flexWidth('create_mode',crud.data,'生成方式')" >
          <template slot-scope="scope">
            {{ dict.label.create_mode[scope.row.create_mode] }}
          </template>
        </el-table-column>
        <el-table-column prop="acs_trace_id" label="链路标识" :min-width="flexWidth('acs_trace_id',crud.data,'链路标识')" />
        <el-table-column prop="request_param" label="生成任务的请求参数" :min-width="flexWidth('request_param',crud.data,'生成任务的请求参数')" />
        <el-table-column prop="response_param" label="下发任务的请求参数" :min-width="flexWidth('response_param',crud.data,'下发任务的请求参数')" />
        <el-table-column prop="workshop_code" label="车间编码" :min-width="flexWidth('workshop_code',crud.data,'车间编码')" />
        <el-table-column prop="ext_group_data" label="额外组盘信息" :min-width="flexWidth('ext_group_data',crud.data,'额外组盘信息')" />
        <el-table-column prop="create_name" label="创建人" :min-width="flexWidth('create_name',crud.data,'创建人')" />
        <el-table-column prop="create_time" label="创建时间" :min-width="flexWidth('create_time',crud.data,'创建时间')" />
        <el-table-column prop="update_name" label="修改人" :min-width="flexWidth('update_name',crud.data,'修改人')" />
        <el-table-column prop="update_time" label="修改时间" :min-width="flexWidth('update_time',crud.data,'修改时间')" />
        <el-table-column v-permission="[]" label="操作" width="160px" align="center" fixed="right">
          <template slot-scope="scope">
<!--            <el-button-->
<!--              type="text"-->
<!--              icon="el-icon-upload"-->
<!--              @click="doOperate(scope.row, 'c')"-->
<!--            >下发</el-button>-->
            <el-button
              type="text"
              icon="el-icon-success"
              @click="doOperate(scope.row, 'a')"
            >完成</el-button>
            <el-button
              type="text"
              icon="el-icon-error"
              @click="doOperate(scope.row, 'b')"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSchBaseTask from './schBaseTask'
import CRUD, { crud, form, header, presenter } 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'

const defaultForm = {
  task_id: null,
  task_code: null,
  task_class_id: null,
  task_status: null,
  config_code: null,
  point_code1: null,
  point_code2: null,
  point_code3: null,
  point_code4: null,
  vehicle_type: null,
  vehicle_qty: null,
  vehicle_code: null,
  vehicle_code2: null,
  priority: null,
  handle_class: null,
  handle_status: null,
  car_no: null,
  task_group_id: null,
  task_group_seq: null,
  finished_type: null,
  create_mode: null,
  acs_trace_id: null,
  request_param: null,
  response_param: null,
  workshop_code: null,
  ext_group_data: null,
  remark: null,
  is_delete: false
}
export default {
  name: 'Task',
  dicts: ['vehicle_type', 'create_mode'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '任务管理',
      url: 'api/schBaseTask',
      idField: 'task_id',
      sort: 'task_code,desc',
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      },
      crudMethod: { ...crudSchBaseTask }
    })
  },
  data() {
    return {
      permission: {
      },
      rules: {
      },
      taskStatusList: []
    }
  },
  created() {
    this.getTaskStatusList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    handTaskStatus(value) {
      this.crud.query.more_task_status = null
      if (value) {
        this.crud.query.more_task_status = value.toString()
      }
      this.crud.toQuery()
    },
    getTaskStatusList() {
      crudSchBaseTask.getTaskStatusList().then(res => {
        this.taskStatusList = res.content
      })
    },
    getStatusName(code) {
      const status = this.taskStatusList.find(item => item.code === code)
      return status ? status.name : '-'
    },
    doOperate(row, command) {
      let method_name = ''
      switch (command) {
        case 'a':// 完成
          method_name = 'forceFinish'
          break
        case 'b':// 取消
          method_name = 'cancel'
          break
        case 'c':// 下发
          method_name = 'immediateNotifyAcs'
          break
        case 'd':// 详情
          method_name = 'view'
          break
      }
      if (method_name === 'view') {
        // this.view(row)
        return
      }
      const data = {
        task_code: row.task_code,
        method_name: method_name,
        config_code: row.config_code
      }
      crudSchBaseTask.operation(data).then(res => {
        this.crud.toQuery()
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
