<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="载具类型">
            <el-select
              v-model="query.storagevehicle_type"
              clearable
              class="filter-item"
              placeholder="请选择"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.storagevehicle_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模糊搜索">
            <el-input
              v-model="query.storagevehicle_code"
              clearable
              size="mini"
              placeholder="载具号、载具名称"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation/>
        </el-form>

      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :before-close="crud.cancelCU"
        :close-on-click-modal="false"
        :title="crud.status.title"
        :visible.sync="crud.status.cu > 0"
        width="450px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="100px">
          <el-form-item label="载具类型" prop="storagevehicle_type">
            <el-select
              v-model="form.storagevehicle_type"
              class="filter-item"
              placeholder="请选择"
              :disabled="crud.status.edit > 0"
              style="width: 200px;"
            >
              <el-option
                v-for="item in dict.storagevehicle_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="载具编码" prop="storagevehicle_code">
            <el-input v-model="form.storagevehicle_code" style="width: 200px;" :disabled="crud.status.edit > 0" />
          </el-form-item>
          <el-form-item label="载具名称" prop="storagevehicle_name">
            <el-input v-model="form.storagevehicle_name" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="载具重量" prop="weigth">
            <el-input-number v-model="form.weigth" :precision="1" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="是否启用">
            <el-radio v-model="form.is_used" label="0">否</el-radio>
            <el-radio v-model="form.is_used" label="1">是</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="info" @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 v-if="false" prop="storagevehicle_id" label="载具标识" />
        <el-table-column prop="storagevehicle_type_name" label="载具类型" :formatter="formattType" />
        <el-table-column prop="storagevehicle_code" label="载具编码" />
        <el-table-column prop="storagevehicle_name" label="载具名称" />
        <el-table-column prop="pcsn" label="绑定物料" />
        <el-table-column prop="weigth" label="托盘重量" />
        <el-table-column label="是否启用" align="center" prop="is_used">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_used"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              active-value="1"
              inactive-value="0"
              @change="changeEnabled(scope.row, scope.row.is_used)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="create_name" label="创建人"/>
        <el-table-column prop="create_time" label="创建时间" width="150px"/>
        <el-table-column
          v-permission="['admin','storagevehicleinfo:edit','storagevehicleinfo:del']"
          label="操作"
          width="150px"
          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 crudStoragevehicleinfo from '@/views/wms/basedata/storagevehicleinfo/storagevehicleinfo'
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 = {
  storagevehicle_id: null,
  storagevehicle_code: null,
  storagevehicle_name: null,
  one_code: null,
  two_code: null,
  rfid_code: null,
  create_id: null,
  create_name: null,
  create_time: null,
  update_optid: null,
  update_optname: null,
  update_time: null,
  is_delete: null,
  is_used: '1',
  storagevehicle_type: null,
  weigth: null,
  vehicle_width: null,
  vehicle_long: null,
  vehicle_height: null,
  overstruct_type: null,
  occupystruct_qty: null,
  ext_id: null,
  qty: null,
  pcsn: null
}
export default {
  name: 'Storagevehicleinfo',
  dicts: ['storagevehicle_type', 'is_used'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '载具',
      url: 'api/storagevehicleinfo',
      idField: 'storagevehicle_id',
      sort: 'storagevehicle_id,desc',
      crudMethod: { ...crudStoragevehicleinfo },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    var numberOne = (rule, value, callback) => {
      const numReg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
      const numRe = new RegExp(numReg)
      if (!numRe.test(value)) {
        callback(new Error('只能输入数字'))
      } else {
        callback()
      }
    }
    return {
      resultCodeArr: [],
      permission: {},
      classes1: [],
      rules: {
        is_delete: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        is_used: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        storagevehicle_type: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        storagevehicle_code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        storagevehicle_name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        overstruct_type: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        num: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.is_used[val] + '" ' + data.storagevehicle_code + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudStoragevehicleinfo.edit(data).then(res => {
          this.crud.notify(this.dict.label.is_used[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          if (data.is_used === '0') {
            data.is_used = '1'
            return
          }
          if (data.is_used === '1') {
            data.is_used = '0'
          }
        })
      }).catch(() => {
        if (data.is_used === '0') {
          data.is_used = '1'
          return
        }
        if (data.is_used === '1') {
          data.is_used = '0'
        }
      })
    },
    formattType(row) {
      return this.dict.label.storagevehicle_type[row.storagevehicle_type]
    }
  }
}
</script>

<style scoped>

</style>
