<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">模糊搜索</label>
        <el-input
          v-model="query.search"
          clearable
          size="small"
          placeholder="请输入设备编码或名称"
          style="width: 220px;"
          class="filter-item"
        />
        <rrOperation :crud="crud" />
      </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="500px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
          <el-form-item label="设备编码" prop="device_code">
            <el-input v-model="form.device_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备名称" prop="device_name">
            <el-input v-model="form.device_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备型号">
            <el-input v-model="form.device_model" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备产能">
            <el-input v-model="form.productivity" type="number" oninput="value = value.replace(/[^\d]/g, '')" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="外部编码">
            <el-input v-model="form.extend_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" 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="mini" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="device_code" label="设备编码" />
        <el-table-column prop="device_name" label="设备名称" min-width="200" show-overflow-tooltip />
        <el-table-column prop="device_model" label="设备型号" min-width="200" show-overflow-tooltip />
        <el-table-column prop="productivity" label="设备产能" />
        <el-table-column label="是否启用" align="center" prop="is_used">
          <template slot-scope="scope">
            <el-switch
              :value="format_is_used(scope.row.is_used)"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.is_used)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="remark" 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 crudDevice from '@/views/wms/mps/device/device'
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'

const defaultForm = { device_id: null, device_code: null, device_name: null, device_model: null, workprocedure_id: null, extend_code: null, devicebill_id: null, remark: null, is_active: null, create_id: null, create_name: null, create_time: null, is_delete: null, productivity: null }
export default {
  name: 'Device',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '生产设备', url: 'api/device', idField: 'device_id', sort: 'device_id,desc',
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: true,
        download: false
      },
      crudMethod: { ...crudDevice }})
  },
  data() {
    return {
      permission: {
      },
      rules: {
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeEnabled(data, val) {
      let msg = '此操作将停用设备，是否继续！'
      if (val !== '1') {
        msg = '此操作将启用设备，是否继续！'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudDevice.changeActive(data).then(res => {
          this.crud.toQuery()
          this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.is_used = !data.is_used
        })
      }).catch(() => {
      })
    },
    format_is_used(is_used) {
      return is_used === '1'
    }
  }
}
</script>

<style scoped>

</style>
