<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"
        />
        <label class="el-form-item-label">所属工序</label>
        <el-select v-model="query.workprocedure_id" filterable clearable size="small" placeholder="所属工序" class="filter-item" style="width: 220px" @change="changeHeader(query.search, query.workprocedure_id)">
          <el-option
            v-for="item in workList"
            :key="item.workprocedure_id"
            :label="item.workprocedure_name"
            :value="item.workprocedure_id"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="danger"
          icon="el-icon-position"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="deviceInfo(crud.selections[0])"
        >
          设备详情
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          icon="el-icon-document-copy"
          :disabled="crud.selections.length !== 1"
          size="mini"
          @click="copyAdd(crud.selections[0])"
        >
          复制新增
        </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="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="生产区域" prop="product_area">
            <el-input v-model="form.product_area" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备编码2">
            <el-input v-model="form.device_code2" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备来料仓上限数" prop="inupperlimit_qty">
            <el-input v-model="form.inupperlimit_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备来料仓下限数" prop="inlowerlimit_qty">
            <el-input v-model="form.inlowerlimit_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备产品仓上限数" prop="outupperlimit_qty">
            <el-input v-model="form.outupperlimit_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备产品仓下限数" prop="outlowerlimit_qty">
            <el-input v-model="form.outlowerlimit_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备初始来料仓数" prop="deviceinitinstor_qty">
            <el-input v-model="form.deviceinitinstor_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备实时来料仓数" prop="deviceinstor_qty">
            <el-input v-model="form.deviceinstor_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备实时产品仓数" prop="deviceoutstor_qty">
            <el-input v-model="form.deviceoutstor_qty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否人工" prop="is_artificial">
            <el-input v-model="form.is_artificial" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" 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="设备编码" width="100px" show-overflow-tooltip/>
        <el-table-column prop="device_name" label="设备名称" />
        <el-table-column prop="product_area" label="生产区域" />
        <el-table-column prop="device_code2" label="设备编码2" />
        <el-table-column prop="inupperlimit_qty" label="设备来料仓上限数" width="120px"/>
        <el-table-column prop="inlowerlimit_qty" label="设备来料仓下限数" width="120px"/>
        <el-table-column prop="outupperlimit_qty" label="设备产品仓上限数" width="120px"/>
        <el-table-column prop="outlowerlimit_qty" label="设备产品仓下限数" width="120px"/>
        <el-table-column prop="deviceinitinstor_qty" label="设备初始来料仓数" width="120px"/>
        <el-table-column prop="deviceinstor_qty" label="设备实时来料仓数" width="120px"/>
        <el-table-column prop="deviceoutstor_qty" label="设备实时产品仓数" width="120px"/>
        <el-table-column prop="is_artificial" label="是否人工" >
          <template slot-scope="scope">
            {{scope.row.is_artificial=='1'?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="120px" show-overflow-tooltip/>
        <el-table-column prop="create_name" label="创建人" />
        <el-table-column prop="create_time" label="创建时间" width="120px" show-overflow-tooltip/>
        <el-table-column prop="update_name" label="修改人" />
        <el-table-column prop="update_time" label="修改时间" width="120px" show-overflow-tooltip/>
        <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>
    <DeviceItemDialog ref="child" @AddChanged="querytable" />
    <DeviceInfoDialog ref="child1" @AddChanged="querytable" />
    <CopyDialog ref="copyChiled" @AddChanged="querytable" />
  </div>
</template>

<script>
import crudDevice from '@/api/wms/pdm/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'
import DeviceItemDialog from '@/views/wms/pdm/device/DeviceItemDialog'
import DeviceInfoDialog from '@/views/wms/pdm/device/DeviceInfoDialog'
import CopyDialog from '@/views/wms/pdm/device/CopyDialog'

const defaultForm = {device_code: null, device_name: null, product_area: null, workprocedure_id: null, ext_code: null,
  material_id: null, workorder_code: null, device_code2: null, inupperlimit_qty: null, inlowerlimit_qty: null,
  outupperlimit_qty: null, outlowerlimit_qty: null, deviceinitinstor_qty: null, deviceinstor_qty: null,
  deviceoutstor_qty: null, is_artificial: null, remark: null, create_id: null, create_name: null, create_time: null,
  update_id: null, update_name: null, update_time: null, is_delete: null }
export default {
  name: 'Device',
  components: { pagination, crudOperation, rrOperation, udOperation, DeviceItemDialog, CopyDialog, DeviceInfoDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['is_used'],
  cruds() {
    return CRUD({ title: '生产设备', url: 'api/device', idField: 'device_code', sort: 'device_code,desc',
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: true,
        download: false
      },
      crudMethod: { ...crudDevice }})
  },
  data() {
    return {
      permission: {
      },
      workList: [],
      tableLabel: [],
      rules: {
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        product_area: [
          { required: true, message: '生产区域不能为空', trigger: 'blur' }
        ],
        inupperlimit_qty: [
          { required: true, message: '设备来料仓上限数不能为空', trigger: 'blur' }
        ],
        inlowerlimit_qty: [
          { required: true, message: '设备来料仓下限数不能为空', trigger: 'blur' }
        ],
        outupperlimit_qty: [
          { required: true, message: '设备产品仓上限数不能为空', trigger: 'blur' }
        ],
        outlowerlimit_qty: [
          { required: true, message: '设备产品仓下限数不能为空', trigger: 'blur' }
        ],
        deviceinitinstor_qty: [
          { required: true, message: '设备初始来料仓数不能为空', trigger: 'blur' }
        ],
        deviceinstor_qty: [
          { required: true, message: '设备实时来料仓数不能为空', trigger: 'blur' }
        ],
        deviceoutstor_qty: [
          { required: true, message: '设备实时产品仓数不能为空', trigger: 'blur' }
        ],
        is_artificial: [
          { required: true, message: '是否人工不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getWorkprocedure()
  },
  methods: {
    changeHeader(search, workprocedure_id) {
      this.crud.toQuery()
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 改变状态
    format_is_used(is_used) {
      return is_used === '1'
    },
    changeEnabled(data, val) {
      debugger
      this.$confirm('此操作将 "' + this.dict.label.is_used[val] + '" ' + data.device_code + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudDevice.edit(data).then(res => {
          this.crud.notify(this.dict.label.is_used[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          if (data.is_active === '0') {
            data.is_active = '1'
            return
          }
          if (data.is_active === '1') {
            data.is_active = '0'
          }
        })
      }).catch(() => {
        if (data.is_active === '0') {
          data.is_active = '1'
          return
        }
        if (data.is_active === '1') {
          data.is_active = '0'
        }
      })
    },
    getWorkprocedure() { // 获取工序下拉框
      crudDevice.getWorkprocedure().then(res => {
        this.workList = res
      })
    },
    deviceInfo(data) {
      this.$refs.child1.setForm(data)
    },
    copyAdd(data) {
      const d = JSON.parse(JSON.stringify(data))
      this.$refs.copyChiled.setForm(d)
    },
    querytable() {
      this.crud.toQuery()
    }
  }
}
</script>

<style scoped>

</style>
