<template>
  <div class="diaryList">
    <div class="whiteBlock title">
      <el-form :inline="true" size="medium" :model="formInline" class="demo-form-inline">
        <el-form-item label="设备名称">
          <el-input v-model="formInline.buildName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" plain>查询</el-button>
          <el-button type="primary" @click="refreshBtn" plain>刷新</el-button>
          <el-button type="primary" @click="dialogVisibleAdd = true" plain>新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content whiteBlock">
      <vue-scroll style="height: 100%">
        <el-table class="tables" :data="tableList">
          <el-table-column prop="name" label="设备名称" align="center"></el-table-column>
          <el-table-column prop="plateNumber" label="车牌号" align="center"></el-table-column>
          <el-table-column prop="type" label="设备类型" align="center">
            <template slot-scope="scope">
              <div>{{ typeList2[`${scope.row.type}`] }}</div>
            </template>
          </el-table-column>
          <el-table-column label="燃油类型" align="center">
            <template slot-scope="scope">
              <div>{{ typeList[`${scope.row.fuelType}`] }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" align="center"></el-table-column>
          <el-table-column label="操作" align="center" width="250">
            <template slot-scope="scope">
              <div class="tableBtns">
                <div @click="editBtn(scope.row)" class="operationText">
                  <i class="el-icon-edit" style="color: #8dacfa; font-size: 16px; margin-right: 2px"></i>
                  <span>编辑</span>
                </div>
                <div @click="deleteBtn(scope.row)" class="operationText">
                  <i class="el-icon-delete" style="color: #eb5760; font-size: 16px; margin-right: 2px"></i>
                  <span>删除</span>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagerBox"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="$store.state.PAGESIZRS"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="Number(total)"
        ></el-pagination>
      </vue-scroll>
      <el-dialog title="新增" :visible.sync="dialogVisibleAdd" width="30%" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="150px">
          <el-form-item label="设备名称：">
            <el-input v-model="form.name" style="width:350px" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="车牌号：">
            <el-input v-model="form.plateNumber" style="width:350px" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="燃油类型：">
            <el-select v-model="valueFuel" placeholder="请选择" style="width:350px" @change="changeFuelType($event)">
              <el-option v-for="item in fuelType" :key="item.id" :label="item.typeName" :value="item.typeName"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备类型：">
            <el-select v-model="valueEquipment" placeholder="请选择" style="width:350px" @change="changeEquipmentType($event)">
              <el-option v-for="item in equipmentType" :key="item.id" :label="item.typeName" :value="item.typeName"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="noAdd">取 消</el-button>
          <el-button type="primary" @click="okAdd">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="编辑" :visible.sync="dialogVisibleEdit" width="30%" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="150px">
          <el-form-item label="设备名称：">
            <el-input v-model="editForm.name" style="width:350px" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="车牌号：">
            <el-input v-model="editForm.plateNumber" style="width:350px" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="燃油类型：">
            <el-select v-model="valueFuel" placeholder="请选择" style="width:350px" @change="changeFuelType($event)">
              <el-option v-for="item in fuelType" :key="item.id" :label="item.label" :value="item.typeName"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备类型：">
            <el-select v-model="valueEquipment" placeholder="请选择" style="width:350px" @change="changeEquipmentType($event)">
              <el-option v-for="item in equipmentType" :key="item.id" :label="item.label" :value="item.typeName"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleEdit = false">取 消</el-button>
          <el-button type="primary" @click="okEdit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getBicarbonList, addBicarbon, editBicarbon, deleteBicarbon } from '@/assets/js/api/gantryCrane'

export default {
  components: {
    //   diaryMod,
  },
  data() {
    return {
      typeList: {
        1: '柴油',
        2: '汽油'
      },
      typeList2: {
        1: '升降机',
        2: '塔吊'
      },
      projectSn: '',
      formInline: {
        buildName: ''
      },
      downloadData: {}, //下载数据
      tableList: [],
      current: 1,
      pageSize: -1,
      total: 0,
      dialogVisibleAdd: false,
      dialogVisibleEdit: false,
      form: {
        name: '',
        plateNumber: ''
      },
      editForm: {
        name: '',
        plateNumber: ''
      },
      fuelType: [
        { id: 1, typeName: '柴油' },
        { id: 2, typeName: '汽油' }
      ],
      equipmentType: [
        { id: 1, typeName: '升降机' },
        { id: 2, typeName: '塔吊' }
      ],
      valueFuel: '',
      valueEquipment: '',
      fuelTypeId: '',
      equipmentTypeId: '',
      editId: ''
    }
  },
  created() {
    this.projectSn = this.$store.state.projectSn
    this.getListData()
  },
  methods: {
    changeFuelType(val) {
      this.fuelType.forEach((item, index, array) => {
        if (item.typeName == val) {
          this.fuelTypeId = item.id
        }
      })
    },
    changeEquipmentType(val) {
      this.equipmentType.forEach((item, index, array) => {
        if (item.typeName == val) {
          this.equipmentTypeId = item.id
        }
      })
    },
    noAdd() {
      this.dialogVisibleAdd = false
      this.form = {
        name: '',
        plateNumber: ''
      }
      this.valueFuel = ''
      this.valueEquipment = ''
    },
    okAdd() {
      this.dialogVisibleAdd = false
      let data = {
        name: this.form.name,
        fuelType: this.fuelTypeId,
        plateNumber: this.form.plateNumber,
        type: this.equipmentTypeId,
        projectSn: this.projectSn
      }
      addBicarbon(data).then(res => {
        if (res.code == 200) {
          this.$message.success('新增成功！')
          this.getListData()
          this.form = {
            name: '',
            plateNumber: ''
          }
          this.valueFuel = ''
          this.valueEquipment = ''
        } else {
          this.$message.success('新增失败！')
        }
      })
    },
    okEdit() {
      this.dialogVisibleEdit = false
      let data = {
        name: this.editForm.name,
        fuelType: this.fuelTypeId,
        plateNumber: this.editForm.plateNumber,
        type: this.equipmentTypeId,
        projectSn: this.projectSn,
        id: this.editId
      }
      editBicarbon(data).then(res => {
        if (res.code == 200) {
          this.$message.success('编辑成功！')
          this.getListData()
        } else {
          this.$message.success('编辑失败！')
        }
      })
      this.getListData()
    },
    handleClose(done) {
      done()
    },
    //获取列表数据
    getListData() {
      let data = {
        name: this.formInline.buildName,
        projectSn: this.projectSn,
        current: this.current,
        pageSize: this.pageSize
      }
      getBicarbonList(data).then(res => {
        this.tableList = res.result.records
        this.total = res.result.total
      })
    },
    //查询按钮
    onSubmit() {
      this.current = 1
      this.getListData()
    },
    //刷新按钮
    refreshBtn() {
      this.current = 1
      this.pageSize = 10
      this.formInline = {
        buildName: ''
      }
      this.getListData()
    },
    //编辑按钮
    editBtn(val) {
      console.log(val)
      this.editForm = { ...val }
      this.dialogVisibleEdit = true
      this.editId = val.id
      this.valueFuel = this.typeList[val.fuelType]
      this.valueEquipment = this.typeList2[val.type]
    },
    //删除按钮
    deleteBtn(val) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let data = {
            id: val.id
          }
          deleteBicarbon(data).then(res => {
            if (res.code == 200) {
              this.$message.success('删除成功！')
              this.getListData()
            } else {
              this.$message.success('删除失败！')
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //查看条数
    handleSizeChange(val) {
      this.current = val
      this.pageSize = 10
      this.getListData()
    },
    //查看页
    handleCurrentChange(val) {
      this.current = val
      this.pageSize = 10
      this.getListData()
    }
  }
}
</script>
<style lang="less" scoped>
.diaryList {
  width: 100%;
  height: 100%;
}
.title {
  padding: 20px 15px 0;
  width: 100%;
}
.content {
  margin-top: 15px;
  height: calc(100% - 93px);
}
.table_wrap {
  width: 100%;
  height: 100%;
  padding: 15px 20px;
}
table {
  border-collapse: collapse;
}

table,
td,
th {
  border: 1px solid #dde5f9;
  text-align: left;
  // height: 35px;
  // line-height: 35px;
  padding: 10px 15px;
}

.setTd {
  width: 25%;
}
.site {
  text-align: right;
}
.site2 {
  text-align: center;
}
.ellipsis {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
