<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.order_code"
          size="small"
          clearable
          placeholder="工单编码"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.material"
          size="small"
          clearable
          placeholder="物料编码、名称或规格"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.device_code"
          size="small"
          clearable
          placeholder="输入设备号"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.order_status"
          clearable
          filterable
          size="small"
          placeholder="工单状态"
          class="filter-item"
          style="width: 190px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in dict.order_status" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="danger"
          icon="el-icon-position"
          size="mini"
          :disabled="is_disabled(crud.selections.length, crud.selections[0])"
          @click="finishd(crud.selections[0])"
        >
          强制完成
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          @click="synchron()"
        >
          同步
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="550px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="生产总量" prop="qty">
            <el-input v-model="form.qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="物料编码" prop="material_code">
            <el-input v-model="form.material_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="物料名称">
            <el-input v-model="form.material_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="规格型号">
            <el-input v-model="form.material_spec" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="满框数量" prop="one_qty">
            <el-input v-model="form.one_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="托盘模板">
            <el-input v-model="form.lane_tray_template" style="width: 370px;" @input="handleEdit" />
          </el-form-item>
          <el-form-item label="生产设备" prop="device_code">
            <el-select
              v-model="form.device_code"
              filterable
              clearable
              size="small"
              placeholder="请选择"
              class="filter-item"
              style="width: 370px"
            >
              <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-if="form.device_code === 'A1_CPQXJ_01_S'" label="上料设备" prop="in_devices">
            <el-input v-model="form.in_devices" placeholder="清洗上料时储料仓设备号,多个设备以,分割" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="agv搬运" prop="is_needmove">
            <el-radio-group v-model="form.is_needmove" size="mini">
              <el-radio-button label="1">是</el-radio-button>
              <el-radio-button label="0">否</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="启用封箱线" prop="is_used_fxx">
            <el-radio-group v-model="form.is_used_fxx" size="mini">
              <el-radio-button label="1">是</el-radio-button>
              <el-radio-button label="0">否</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="装箱机编号" prop="ealing_device_code">
            <el-input v-model="form.ealing_device_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="开箱机编号" prop="unboxing_device_code">
            <el-input v-model="form.unboxing_device_code" style="width: 370px;" />
          </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="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="order_code" label="工单编码" min-width="120" show-overflow-tooltip />
        <el-table-column prop="order_status" label="工单状态" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ dict.label.order_status[scope.row.order_status] }}
          </template>
        </el-table-column>
        <el-table-column prop="qty" label="生产数量" min-width="100" show-overflow-tooltip />
        <el-table-column prop="device_code" label="设备编号" min-width="120" show-overflow-tooltip />
        <el-table-column prop="material_code" label="物料编码" min-width="120" show-overflow-tooltip />
        <el-table-column prop="material_name" label="物料名称" min-width="120" show-overflow-tooltip />
        <el-table-column prop="material_spec" label="规格型号" min-width="120" show-overflow-tooltip />
        <el-table-column prop="one_qty" label="满框数量" min-width="100" show-overflow-tooltip />
        <el-table-column prop="is_needmove" label="是否AGV搬运" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ dict.label.is_need_move[scope.row.is_needmove] }}
          </template>
        </el-table-column>
        <el-table-column prop="lane_tray_template" label="托盘模板" min-width="100" show-overflow-tooltip />
        <el-table-column prop="in_devices" label="上料模板" min-width="100" show-overflow-tooltip />
        <el-table-column prop="tray_num" label="托盘总数" min-width="100" show-overflow-tooltip />
        <el-table-column prop="is_used_fxx" label="启用封箱线" min-width="100" show-overflow-tooltip />
        <el-table-column prop="ealing_device_code" label="装箱机编号" min-width="100" show-overflow-tooltip />
        <el-table-column prop="unboxing_device_code" label="开箱机编号" min-width="100" show-overflow-tooltip />
        <el-table-column prop="create_by" label="创建者" min-width="100" show-overflow-tooltip />
        <el-table-column prop="create_time" label="创建时间" min-width="150" show-overflow-tooltip />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudProduceshiftorder from '@/api/acs/order/produceshiftorder'
import deviceCrud from '@/api/acs/device/device'
import rrOperation from '@crud/RR.operation'

const defaultForm = {
  order_id: null,
  order_code: null,
  order_status: null,
  qty: null,
  one_qty: null,
  material_code: null,
  material_name: null,
  material_uuid: null,
  material_spec: null,
  create_by: null,
  create_time: null,
  update_by: null,
  update_time: null,
  is_deleted: null,
  ext_order_id: null,
  device_code: null,
  is_needmove: '1',
  lane_tray_template: null,
  tray_num: '',
  in_devices: '',
  is_used_fxx: '0',
  ealing_device_code: '',
  unboxing_device_code: ''
}
export default {
  name: 'Produceshiftorder',
  dicts: ['order_status', 'is_need_move'],
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '工单信息',
      url: 'api/produceshiftorder',
      idField: 'order_id',
      sort: 'order_id,desc',
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: true,
        reset: false
      },
      crudMethod: { ...crudProduceshiftorder }
    })
  },
  data() {
    return {
      deviceList: [],
      permission: {
        add: ['admin', 'produceshiftorder:add'],
        edit: ['admin', 'produceshiftorder:edit'],
        del: ['admin', 'produceshiftorder:del']
      },
      rules: {
        order_code: [
          { required: true, message: '工单编码不能为空', trigger: 'blur' }
        ],
        order_status: [
          { required: true, message: '工单状态不能为空', trigger: 'blur' }
        ],
        qty: [
          { required: true, message: '生产总量不能为空', trigger: 'blur' }
        ],
        material_code: [
          { required: true, message: '物料编码不能为空', trigger: 'blur' }
        ],
        device_code: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    deviceCrud.selectDeviceList().then(data => {
      this.deviceList = data
    })
  },
  methods: {
    // 在 Input 值改变时触发
    handleEdit(e) {
      let value = e.replace(/^(0+)|[^d]+/g, '') // 以0开头或者输入非数字，会被替换成空
      value = value.replace(/(d{15})d*/, '$1') // 最多保留15位整数
      this.form.lane_tray_template = value
    },
    is_disabled(length, data) {
      if (length !== 1) {
        return true
      } else {
        return false
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    finishd(data) {
      const reqData = {
        order_id: data.order_id
      }
      crudProduceshiftorder.finished(reqData).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    synchron() {
      crudProduceshiftorder.synchron().then(res => {
        this.crud.notify('同步成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    }
  }
}
</script>

<style scoped>

</style>
