<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <el-input
        v-model="query.search"
        clearable
        style="width: 300px"
        size="mini"
        placeholder="输入编码或名称"
        prefix-icon="el-icon-search"
        class="filter-item"
      />
      <rrOperation />
      <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="500px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="150px">
          <el-form-item label="载具编码" prop="vehicle_code">
            <el-input v-model="form.vehicle_code" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="是否打印" prop="is_print">
            <el-radio v-for="item in dict.IS_OR_NOT" :key="item.id" v-model="form.is_print" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item v-if="false" label="打印时间">
            <el-input v-model="form.print_time" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="是否可用" prop="is_active">
            <el-radio v-for="item in dict.IS_OR_NOT" :key="item.id" v-model="form.is_active" :label="item.value">{{ item.label }}</el-radio>
          </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="vehicle_code" label="载具编码" />
        <el-table-column prop="is_print" label="是否打印">
          <template slot-scope="scope">
            <el-switch
              :value="format_is_print(scope.row.is_print)"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled1(scope.row, scope.row.is_print)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="150px" />
        <el-table-column prop="update_time" label="更新时间" width="150px" />
        <el-table-column prop="print_time" label="打印时间" />
        <el-table-column prop="is_active" label="是否可用">
          <template slot-scope="scope">
            <el-switch
              :value="format_is_active(scope.row.is_active)"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.is_active)"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="false" prop="create_id" label="创建人" />
        <el-table-column prop="create_name" label="创建人" />
        <el-table-column v-if="false" prop="update_optid" label="修改人" />
        <el-table-column prop="update_optname" label="修改人" />
        <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 crudVehicle, { changePrint } from '@/api/wms/cacheline/vehicle'
import CRUD, { presenter, header, form, crud } 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'
import crudStoragevehicleinfo from '@/api/wms/basedata/master/storagevehicleinfo'

const defaultForm = { vehicle_uuid: null, vehicle_code: null, print_num: null, is_print: '1', create_time: null, update_time: null, print_time: null, is_active: '1', is_delete: null, create_id: null, create_name: null, update_optid: null, update_optname: null }
export default {
  name: 'Vehicle',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['IS_OR_NOT'],
  cruds() {
    return CRUD({ title: '缓存线条码', url: 'api/vehicle', idField: 'vehicle_uuid', sort: 'vehicle_uuid,desc', crudMethod: { ...crudVehicle }})
  },
  data() {
    return {
      permission: {
      },
      rules: {
        vehicle_code: [
          { required: true, message: '载具编码不能为空', trigger: 'blur' }
        ],
        is_print: [
          { required: true, message: '是否打印不能为空', trigger: 'blur' }
        ],
        is_active: [
          { required: true, message: '是否可用不能为空', trigger: 'blur' }
        ]
      }}
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    format_is_print(is_print) {
      return is_print === '1'
    },
    format_is_active(is_active) {
      return is_active === '1'
    },
    changeEnabled(data, val) {
      let msg = '此操作将停用载具，是否继续！'
      if (val !== '1') {
        msg = '此操作将启用载具，是否继续！'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudVehicle.changeActive(data).then(res => {
          this.crud.toQuery()
          this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.is_used = !data.is_used
        })
      }).catch(() => {
      })
    },
    changeEnabled1(data, val) {
      let msg = '此操作将停用打印，是否继续！'
      if (val !== '1') {
        msg = '此操作将启用打印，是否继续！'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudVehicle.changePrint(data).then(res => {
          this.crud.toQuery()
          this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.is_print = !data.is_print
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
