<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="90px"
          label-suffix=":"
        >
          <el-form-item label="所属车间">
            <el-select
              v-model="query.workshop_code"
              clearable
              size="mini"
              placeholder="所属车间"
              class="filter-item"
            >
              <el-option
                v-for="item in workShopList"
                :label="item.workshop_name"
                :value="item.workshop_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模糊搜索">
            <el-input
              v-model="query.blurry"
              clearable
              size="mini"
              placeholder="编码名称"
              @keyup.enter.native="crud.toQuery"
            />
          </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="820px">
        <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="135px" label-suffix=":">
          <el-form-item label="所属车间">
            <el-select
              v-model="form.workshop_code"
              placeholder="请选择"
              style="width: 240px;"
            >
              <el-option
                v-for="item in workShopList"
                :label="item.workshop_name"
                :value="item.workshop_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="配置编码" prop="config_code">
            <el-input v-model="form.config_code" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="配置名称">
            <el-input v-model="form.config_name" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务取放类型">
            <el-select
              v-model="form.task_qf_type"
              size="mini"
              placeholder="任务取放类型"
              style="width: 240px;"
            >
              <el-option
                v-for="item in dict.task_qf_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="acs任务类型" prop="acs_task_type">
            <el-select
              v-model="form.acs_task_type"
              size="mini"
              placeholder="acs任务类型"
              style="width: 240px;"
            >
              <el-option
                v-for="item in dict.acs_task_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="任务名字">
            <el-input v-model="form.task_name" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务类型">
            <el-input v-model="form.task_type" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="优先级" pro="priority">
            <el-input-number
              v-model.number="form.priority"
              :min="1"
              :max="999"
              style="width: 240px;"
            />
          </el-form-item>
          <el-form-item label="任务生成数上限" prop="task_create_max_num">
            <el-input-number
              v-model.number="form.task_create_max_num"
              :min="1"
              :max="999"
              style="width: 240px;"
            />
          </el-form-item>
          <el-form-item label="是否自动下发">
            <el-radio-group v-model="form.is_auto_issue" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="任务下发数上限" prop="task_issue_max_num">
            <el-input-number
              v-model.number="form.task_issue_max_num"
              :min="1"
              :max="999"
              style="width: 240px;"
            />
          </el-form-item>
          <el-form-item label="起点区域">
            <el-select v-model="form.start_region_strs" multiple placeholder="请选择" style="width: 240px;">
              <el-option
                v-for="item in regionList"
                :key="item.region_code"
                :label="item.region_name"
                :value="item.region_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="终点区域">
            <el-select v-model="form.next_region_strs" multiple placeholder="请选择" style="width: 240px;">
              <el-option
                v-for="item in regionList"
                :key="item.region_code"
                :label="item.region_name"
                :value="item.region_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="起点前缀">
            <el-input v-model="form.start_point_pre" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="终点前缀">
            <el-input v-model="form.next_region_pre" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="任务方向" prop="task_direction">
            <el-select
              v-model="form.task_direction"
              size="mini"
              placeholder="任务方向"
              style="width: 240px;"
            >
              <el-option
                v-for="item in dict.task_direction"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否校验工单">
            <el-radio-group v-model="form.is_check_workorder" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否判断起点锁">
            <el-radio-group v-model="form.is_check_start_lock" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否立即创建">
            <el-radio-group v-model="form.is_immediate_create" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否判断终点锁">
            <el-radio-group v-model="form.is_check_next_lock" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否起点自动">
            <el-radio-group v-model="form.is_start_auto" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否终点自动">
            <el-radio-group v-model="form.is_next_auto" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否锁定起点">
            <el-radio-group v-model="form.is_lock_start" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否锁定终点">
            <el-radio-group v-model="form.is_lock_next" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </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 v-if="false" label="下发任务的请求参数">
            <el-input v-model="form.response_param" style="width: 240px;" />
          </el-form-item>
          <el-form-item v-if="false" label="是否按组控制下发顺序">
            <el-radio-group v-model="form.is_group_congrol_issue_seq" style="width: 240px">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="路由编码" prop="route_plan_code">
            <el-input v-model="form.route_plan_code" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="未完成通知时间" prop="unfinish_notify_time">
            <el-input-number
              v-model.number="form.unfinish_notify_time"
              :min="0"
              :max="999"
              style="width: 240px;"
            />
          </el-form-item>
          <el-form-item v-if="false" label="sql配置">
            <el-input v-model="form.sql_param" style="width: 240px;" />
          </el-form-item>
          <el-form-item v-if="false" label="车间编码">
            <el-input v-model="form.workshop_code" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <label slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</label>
            <el-input v-model.trim="form.remark" style="width: 480px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
          </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="config_code" label="配置编码" :min-width="flexWidth('config_code',crud.data,'配置编码')" />
        <el-table-column prop="config_name" label="配置名称" :min-width="flexWidth('config_name',crud.data,'配置名称')" />
        <el-table-column prop="task_qf_type" label="任务取放类型" :min-width="flexWidth('task_qf_type',crud.data,'任务取放类型')">
          <template slot-scope="scope">
            {{ dict.label.task_qf_type[scope.row.task_qf_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="acs_task_type" label="acs任务类型" :min-width="flexWidth('acs_task_type',crud.data,'acs任务类型')">
          <template slot-scope="scope">
            {{ dict.label.acs_task_type[scope.row.acs_task_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="task_name" label="任务名字" :min-width="flexWidth('task_name',crud.data,'任务名字')" />
        <el-table-column prop="task_type" label="任务类型" :min-width="flexWidth('task_type',crud.data,'任务类型')" />
        <el-table-column prop="priority" label="优先级" :min-width="flexWidth('priority',crud.data,'优先级')" />
        <el-table-column prop="task_create_max_num" label="任务生成数上限" :min-width="flexWidth('task_create_max_num',crud.data,'任务生成数上限')" />
        <el-table-column prop="task_issue_max_num" label="任务下发数上限" :min-width="flexWidth('task_issue_max_num',crud.data,'任务下发数上限')" />
        <el-table-column prop="is_auto_issue" label="是否自动下发" :min-width="flexWidth('is_auto_issue',crud.data,'是否自动下发')">
          <template slot-scope="scope">
            {{ scope.row.is_auto_issue?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="start_region_str" label="起点区域" :min-width="flexWidth('start_region_str',crud.data,'起点区域')">
          <template slot-scope="scope">
            {{ getRegionNames(scope.row.start_region_str) }}
          </template>
        </el-table-column>
        <el-table-column prop="next_region_str" label="终点区域" :min-width="flexWidth('next_region_str',crud.data,'终点区域')">
          <template slot-scope="scope">
            {{ getRegionNames(scope.row.next_region_str) }}
          </template>
        </el-table-column>
        <el-table-column prop="start_point_pre" label="起点前缀" :min-width="flexWidth('start_point_pre',crud.data,'起点前缀')" />
        <el-table-column prop="next_region_pre" label="终点前缀" :min-width="flexWidth('next_region_pre',crud.data,'终点前缀')" />
        <el-table-column prop="route_plan_code" label="路由编码" :min-width="flexWidth('route_plan_code',crud.data,'终点前缀')" />
        <el-table-column prop="task_direction" label="任务方向" :min-width="flexWidth('task_direction',crud.data,'终点前缀')" >
          <template slot-scope="scope">
            {{ dict.label.task_direction[scope.row.task_direction] }}
          </template>
        </el-table-column>
        <el-table-column prop="is_check_workorder" label="是否校验工单" :min-width="flexWidth('is_check_workorder',crud.data,'是否校验工单')">
          <template slot-scope="scope">
            {{ scope.row.is_check_workorder?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_check_start_lock" label="是否判断起点锁" :min-width="flexWidth('is_check_start_lock',crud.data,'是否判断起点锁')">
          <template slot-scope="scope">
            {{ scope.row.is_check_start_lock?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_immediate_create" label="是否立即创建" :min-width="flexWidth('is_immediate_create',crud.data,'是否立即创建')">
          <template slot-scope="scope">
            {{ scope.row.is_immediate_create?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_check_next_lock" label="是否判断终点锁" :min-width="flexWidth('is_check_next_lock',crud.data,'是否判断终点锁')">
          <template slot-scope="scope">
            {{ scope.row.is_check_next_lock?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_start_auto" label="是否起点自动" :min-width="flexWidth('is_start_auto',crud.data,'是否起点自动')">
          <template slot-scope="scope">
            {{ scope.row.is_start_auto?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_next_auto" label="是否终点自动" :min-width="flexWidth('is_next_auto',crud.data,'是否终点自动')">
          <template slot-scope="scope">
            {{ scope.row.is_next_auto?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_lock_start" label="是否锁定起点" :min-width="flexWidth('is_lock_start',crud.data,'是否锁定起点')">
          <template slot-scope="scope">
            {{ scope.row.is_lock_start?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="is_lock_next" label="是否锁定终点" :min-width="flexWidth('is_lock_next',crud.data,'是否锁定终点')">
          <template slot-scope="scope">
            {{ scope.row.is_lock_next?'是':'否' }}
          </template>
        </el-table-column>
        <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 v-if="false" prop="is_group_congrol_issue_seq" label="是否按组控制下发顺序" :min-width="flexWidth('is_group_congrol_issue_seq',crud.data,'是否按组控制下发顺序')">
          <template slot-scope="scope">
            {{ scope.row.is_group_congrol_issue_seq?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column prop="unfinish_notify_time" label="未完成通知时间" :min-width="flexWidth('unfinish_notify_time',crud.data,'未完成通知时间')" />
        <el-table-column v-if="false" prop="sql_param" label="sql配置" :min-width="flexWidth('sql_param',crud.data,'sql配置')" />
        <el-table-column prop="workshop_code" label="车间编码" :min-width="flexWidth('workshop_code',crud.data,'车间编码')" />
        <el-table-column prop="remark" label="备注" :min-width="flexWidth('remark',crud.data,'备注')" />
        <!--        <el-table-column prop="is_used" label="是否启用" :min-width="flexWidth('is_used',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="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSchBaseTaskconfig from './schBaseTaskconfig'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'
import crudMdBaseWorkShop from '@/views/wms/basedata/workshop/mdBaseWorkshop'
import crudSchBaseRegion from '@/views/wms/sch/region/schBaseRegion'

const defaultForm = {
  config_id: null,
  config_code: null,
  config_name: null,
  task_qf_type: null,
  acs_task_type: null,
  task_name: null,
  task_type: null,
  priority: null,
  task_create_max_num: null,
  task_issue_max_num: null,
  is_auto_issue: true,
  start_region_str: null,
  next_region_str: null,
  start_point_pre: null,
  next_region_pre: null,
  is_check_workorder: true,
  is_check_start_lock: false,
  is_immediate_create: true,
  is_check_next_lock: false,
  is_start_auto: true,
  is_next_auto: true,
  is_lock_start: false,
  is_lock_next: false,
  request_param: null,
  response_param: null,
  is_group_congrol_issue_seq: false,
  unfinish_notify_time: null,
  sql_param: null,
  route_plan_code: null,
  workshop_code: null,
  task_direction: null,
  remark: null,
  is_used: true,
  is_delete: false,
  start_region_strs: null,
  next_region_strs: null
}
export default {
  name: 'TaskConfig',
  dicts: ['task_qf_type', 'acs_task_type', 'task_direction'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '任务配置',
      url: 'api/schBaseTaskconfig',
      idField: 'config_code',
      sort: 'config_code,desc',
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: false,
        reset: true
      },
      crudMethod: { ...crudSchBaseTaskconfig }
    })
  },
  data() {
    return {
      permission: {
      },
      rules: {
        config_code: [
          { required: true, message: '请输入配置编码', trigger: 'blur' }
        ],
        acs_task_type: [
          { required: true, message: '请输入acs类型', trigger: 'blur' }
        ],
        task_create_max_num: [
          { required: true, message: '请输入最大任务生成数', trigger: 'blur' }
        ],
        task_issue_max_num: [
          { required: true, message: '请输入最大任务下发数', trigger: 'blur' }
        ],
        unfinish_notify_time: [
          { required: true, message: '请输入任务未完成通知时间数', trigger: 'blur' }
        ]
      },
      workShopList: [],
      regionList: []
    }
  },
  computed: {
    getRegionNames() {
      return (nextRegionStr) => {
        if (!nextRegionStr) {
          return '-'
        }
        const regionCodes = nextRegionStr.split(',').map(code => code.trim())
        const regionNames = regionCodes
          .map(code => {
            const region = this.regionList.find(item => item.region_code === code)
            return region ? region.region_name : ''
          })
          .filter(name => name !== '')

        return regionNames.join('、')
      }
    }
  },
  created() {
    this.getWorkShopList()
    this.getRegionList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getWorkShopList() { // 获取车间列表
      crudMdBaseWorkShop.getWorkShopList().then(res => {
        this.workShopList = res
      })
    },
    getRegionList() { // 获取区域列表
      crudSchBaseRegion.getRegionList().then(res => {
        this.regionList = res
      })
    }
  }
}
</script>

<style scoped>

</style>
