<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-input
        v-model="query.search"
        clearable
        style="width: 300px"
        size="mini"
        placeholder="输入设备编码或设备名称"
        prefix-icon="el-icon-search"
        class="filter-item"
      />
      <rrOperation />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， 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="1100px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="140px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="设备编码" prop="device_code">
                <el-input v-model="form.device_code" disabled style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系人" prop="upload_user">
                <el-select v-model="form.upload_user" placeholder="请选择" filterable multiple clearable style="width: 250px">
                  <el-option
                    v-for="item in userList"
                    :key="item.username"
                    :label="item.username"
                    :value="item.username"
                  >
                    <span style="float: left">{{ item.username }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.personName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </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 prop="device_code" label="设备编码" show-overflow-tooltip width="150px" />
        <el-table-column prop="device_name" label="设备名称" show-overflow-tooltip width="150px" />
        <el-table-column prop="product_area" label="区域" show-overflow-tooltip />
        <el-table-column prop="device_type" label="设备类型" show-overflow-tooltip />
        <el-table-column prop="mode" label="工作状态" show-overflow-tooltip />
        <el-table-column prop="mode_update_time" label="工作状态变更时间" show-overflow-tooltip width="150px" />
        <el-table-column prop="error" label="故障状态" show-overflow-tooltip />
        <el-table-column prop="error_msg" label="故障信息" width="180px" show-overflow-tooltip />
        <el-table-column prop="error_update_time" label="故障状态更新时间" width="150px" show-overflow-tooltip />
        <el-table-column prop="error_update_time" label="故障状态更新时间" width="150px" show-overflow-tooltip />
        <el-table-column label="是否上报" align="center" prop="upload_flag">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.upload_flag"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              active-value="1"
              inactive-value="0"
              @change="changeEnabled(scope.row, scope.row.upload_flag)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="upload_user" label="负责人" width="150px" show-overflow-tooltip />
        <el-table-column
          v-permission="['admin','customerbase:edit','customerbase:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :is-visiable-del="false"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDeviceStatus from '@/views/wms/agvrush/devicestatus/devicestatus'
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'
import crudUser from '@/views/system/user/user'

const defaultForm = {
  device_code: null,
  upload_user: null
}
export default {
  name: 'DeviceStatus',
  dicts: ['is_used', 'print_temple'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备状态',
      url: 'api/devicestatus',
      optShow: {
        add: true,
        reset: true
      },
      idField: 'device_code',
      sort: 'device_code,desc',
      crudMethod: { ...crudDeviceStatus }
    })
  },
  data() {
    return {
      permission: {},
      classes: [],
      userList: [],
      rules: {}
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      this.getUserList()
      form.upload_user = form.upload_user.split(',')
    },
    // 改变状态
    getUserList() {
      crudUser.getUserList().then(res => {
        this.userList = res
      })
    },
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.is_used[val] + '" ' + data.device_code + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        data.need_update_flag = '1'
        crudDeviceStatus.edit(data).then(res => {
          this.crud.notify(this.dict.label.is_used[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          if (data.upload_flag === '0') {
            data.upload_flag = '1'
            return
          }
          if (data.upload_flag === '1') {
            data.upload_flag = '0'
          }
        })
      }).catch(() => {
        if (data.upload_flag === '0') {
          data.upload_flag = '1'
          return
        }
        if (data.upload_flag === '1') {
          data.upload_flag = '0'
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
