<template>
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <!-- 仓库管理——工器具管理 -->
      <el-form :inline="true" ref="searchForm" :model="searchForm" size="medium">
        <!-- <el-form-item label="设备编号">
          <el-input clearable v-model="searchForm.devSn" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="工器具名称">
          <el-input clearable v-model="searchForm.toolName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="工器具状态">
          <el-select clearable v-model="searchForm.toolStatus" placeholder="请选择">
            <el-option v-for="(item, index) in toolTypeOptions" :key="index" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="warning" plain @click="refresh">刷新</el-button>
          <el-button type="primary" size="medium" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <vue-scroll style="height: 80%">
        <el-table class="tables" :data="tableData">
          <el-table-column prop="equipmentName" align="center" label="机械设备名称" width="280"></el-table-column>
          <el-table-column prop="devSn" align="center" label="设备序号"></el-table-column>
          <el-table-column prop="driverName" align="center" label="司机"></el-table-column>
          <el-table-column prop="createTime" align="center" label="新增时间"></el-table-column>
          <el-table-column align="center" label="操作" width="280">
            <template slot-scope="scope">
              <div class="tableBtns">
                <div @click="edit(scope.row)" class="operationText">
                  <img src="@/assets/images/icon-edit.png" width="15px" height="15px" />
                  <span style="white-space: nowrap;">编辑</span>
                </div>
                <div @click="deleteDev(scope.row)" class="operationText">
                  <img src="@/assets/images/icon-delete.png" width="15px" height="15px" />
                  <span style="white-space: nowrap;">删除</span>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </vue-scroll>
      <el-pagination class="pagerBox" @size-change="SizeChange" @current-change="CurrentChange" :current-page="pagInfo.pageNo" :page-sizes="$store.state.PAGESIZRS" :page-size="pagInfo.pageSize" layout="total, sizes, prev, pager, next" :total="Number(pagInfo.total)" background>
      </el-pagination>
    </div>
    <!-- 新增-编辑-弹窗 -->
    <el-dialog :modal-append-to-body="false" @close="close" :title="title" :visible.sync="dialogShow" width="667px">
      <div class="dialog_content">
        <el-form size="medium" ref="addEditForm" :model="addEditForm" :rules="addEditRules" label-width="120px" class="dialogFormBox">
          <el-form-item label="设备名称" prop="equipmentName">
            <el-input v-model="addEditForm.equipmentName" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="设备序号" prop="devSn">
            <el-input v-model="addEditForm.devSn" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="司机" prop="driverId">
            <el-select v-model="addEditForm.driverId" placeholder="请选择" clearable :style="{ width: '100%' }">
              <el-option v-for="(item, index) in workerList" :key="index" :label="item.workerName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="新增时间" prop="createTime">
            <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="addEditForm.createTime" type="datetime" placeholder="请选择时间">
            </el-date-picker>
          </el-form-item>
          <div class="dialog-footer">
            <el-button class="cancleBtn" @click="dialogShow = false" icon="el-icon-circle-close" size="medium">{{ $t('message.deviceManage.cancel') }}
            </el-button>
            <el-button type="primary" icon="el-icon-circle-check" @click="submit" size="medium">{{ $t('message.deviceManage.save') }}
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDeviceCentraliceCentralPageApi,
  addDeviceCentraliceCentralPageApi,
  editDeviceCentraliceCentralPageApi,
  deleteDeviceCentraliceCentralPageApi,
  getWorkerInfoListApi,
} from '@/assets/js/api/devicePosition/equipmentPosition.js'
export default {
  mounted() {},
  created() {
    this.getPage()
    this.getWorkerInfoList()
  },
  data() {
    return {
      title: '',
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0, //总条数
      },
      tableData: [],
      addEditForm: {
        equipmentName: '',
        devSn: '',
        createTime: '',
        driverId: '',
      },
      addEditRules: {
        equipmentName: [{ required: true, message: '必填', trigger: 'blur' }],
        devSn: [{ required: true, message: '必填', trigger: 'blur' }],
        driverId: [{ required: true, message: '必填', trigger: 'blur' }],
      },
      searchForm: {},
      workerList: [],
    }
  },
  methods: {
    getPage() {
      getDeviceCentraliceCentralPageApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        devSn: this.searchForm.devSn,
        projectSn: this.$store.state.projectSn,
      }).then((result) => {
        if (result.success) {
          this.tableData = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },

    add() {
      this.title = '新增'
      this.dialogShow = true
      this.close()
    },

    edit(obj) {
      this.title = '编辑'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
    },
    submit() {
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增') {
            addDeviceCentraliceCentralPageApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getPage()
              }
            })
          } else if (this.title == '编辑') {
            editDeviceCentraliceCentralPageApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getPage()
              }
            })
          }
          this.dialogShow = false
        } else {
          return false
        }
      })
    },
    deleteDev(obj) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          deleteDeviceCentraliceCentralPageApi({
            id: obj.id,
            projectSn: this.$store.state.projectSn,
          }).then((res) => {
            if (res.success) {
              this.getPage()
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
            } else {
              this.$message({
                type: 'error',
                message: res.message,
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    async getWorkerInfoList() {
      await getWorkerInfoListApi({
        pageNo: this.pagInfo.pageNo,
        // pageSize: this.pagInfo.pageSize,
        pageSize: 100000,
        projectSn: this.$store.state.projectSn,
        // personType: 1,
        inserviceType: 1,
      }).then((result) => {
        if (result.success) {
          this.workerList = result.result.records
        }
      })
    },
    close() {
      this.addEditForm = {}
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getPage()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getPage()
    },
    refresh() {
      this.searchForm = {}
      this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getPage()
      this.getWorkerInfoList()
    },
  },
}
</script>
<style lang="less" scoped>
.tables {
  // min-height: 0;
}
.tables2 {
  min-height: auto;
}
.textStyle {
  width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tableBtns {
  display: flex;
  justify-content: center;
  align-item: center;
}
</style>