<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="success"
          icon="el-icon-position"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="addItem(crud.selections[0])"
        >
          设备扩展项
        </el-button>
        <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="设备型号">
            <el-input v-model="form.device_model" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备产能">
            <el-input type="number" oninput="value = value.replace(/[^\d]/g, '')" v-model="form.productivity" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="所属工序">
            <el-select v-model="form.workprocedure_id" filterable clearable class="filter-item" style="width: 370px">
              <el-option
                v-for="item in workList"
                :key="item.workprocedure_id"
                :label="item.workprocedure_name"
                :value="item.workprocedure_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="外部编码">
            <el-input v-model="form.extend_code" style="width: 370px;" />
          </el-form-item>
          <!--          <el-form-item label="设备档案标识">
            未设置字典，请手动设置 Select
          </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="productivity" label="设备产能" />
        <el-table-column prop="produceorder_code" label="工单编号" />
        <template v-for="(item, index) in tableLabel">
          <el-table-column
            v-if="item.show"
            :key="index"
            :show-overflow-tooltip="true"
            :prop="item.prop"
            :label="item.label"
            :min-width="item.width"
          />
        </template>
        <el-table-column label="是否启用" align="center" prop="is_active">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_active"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              active-value="1"
              inactive-value="0"
              @change="changeEnabled(scope.row, scope.row.is_active)"
            />
          </template>
        </el-table-column>
        <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'
import crudSectattr from "@/api/wms/basedata/st/sectattr";

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, DeviceItemDialog, CopyDialog, DeviceInfoDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['is_used'],
  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: {
      },
      workList: [],
      tableLabel: [],
      rules: {
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getWorkprocedure()
  },
  methods: {
    changeHeader(search, workprocedure_id) {
      this.getHeader(search, workprocedure_id)
      this.crud.toQuery()
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.getHeader(this.query.search, this.query.workprocedure_id)
      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
      })
    },
    addItem(data) {
      this.$refs.child.setForm(data)
    },
    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()
    },
    getHeader(search, workprocedure_id) {
      crudDevice.getItemByDeviceId({ search: search, workprocedure_id: workprocedure_id }).then(res => {
        this.tableLabel = res.newArray
      })
    }
  }
}
</script>

<style scoped>

</style>
