<template>
  <!-- 架桥机- 设备档案 -->
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <el-form :inline="true" ref="searchForm" :model="searchForm" size="medium">
        <el-form-item label="设备名称">
          <el-input v-model="searchForm.devName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="getList">{{ $t('message.energyManage.waybill.query') }}</el-button>
          <el-button type="warning" plain @click="refresh">{{ $t('message.deviceManage.refresh') }}</el-button>
          <!-- <el-button type="primary" size="medium" @click="add">新增</el-button> -->
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List">
        <!-- <el-table-column prop="image" label="照片" align="center">
          <template slot-scope="scope">
            <img
              :preview="scope.row.image[0] ? scope.row.image[0].url : []"
              :src="scope.row.image[0] ? scope.row.image[0].url : []"
              alt=""
              width="50px"
            />
          </template>
        </el-table-column> -->
        <el-table-column prop="devName" align="center" label="设备名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="devCode" align="center" label="设备代码" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.devCode == '' || scope.row.devCode == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.devCode }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="serialNumber" align="center" label="出厂编号">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.serialNumber == '' || scope.row.serialNumber == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.serialNumber }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150" show-overflow-tooltip prop="manufacturingLicense" align="center" label="制造许可证">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.manufacturingLicense == '' || scope.row.manufacturingLicense == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.manufacturingLicense }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="manufacturer" align="center" label="制造厂家">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.manufacturer == '' || scope.row.manufacturer == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.manufacturer }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="manufactureDate" align="center" label="出厂日期">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.manufactureDate == '' || scope.row.manufactureDate == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.manufactureDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="propertyNumber" align="center" label="产权编号">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.propertyNumber == '' || scope.row.propertyNumber == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.propertyNumber }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150" show-overflow-tooltip prop="equipmentPropertyRightsUnit" align="center" label="设备产权单位">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.equipmentPropertyRightsUnit == '' || scope.row.equipmentPropertyRightsUnit == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.equipmentPropertyRightsUnit }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="contactPerson" align="center" label="联系人">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.contactPerson == '' || scope.row.contactPerson == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.contactPerson }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="contactNumber" align="center" label="联系电话">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.contactNumber == '' || scope.row.contactNumber == null">
              {{ "-" }}
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.contactNumber }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150" show-overflow-tooltip prop="useRegistrationNumber" align="center" label="使用登记编号">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.useRegistrationNumber == '' || scope.row.useRegistrationNumber == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.useRegistrationNumber }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="installationUnit" align="center" label="安装单位">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.installationUnit == '' || scope.row.installationUnit == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.installationUnit }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="inspectionNumber" align="center" label="检测编号">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.inspectionNumber == '' || scope.row.inspectionNumber == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.inspectionNumber }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150" show-overflow-tooltip prop="inspectionAndTestingAgency" align="center" label="检验检测机构">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.inspectionAndTestingAgency == '' || scope.row.inspectionAndTestingAgency == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.inspectionAndTestingAgency }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="acceptanceDate" align="center" label="验收日期">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.acceptanceDate == '' || scope.row.acceptanceDate == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.acceptanceDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="registrationDate" align="center" label="登记日期">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.registrationDate == '' || scope.row.registrationDate == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.registrationDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="registrationDepartment" align="center" label="登记部门">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.registrationDepartment == '' || scope.row.registrationDepartment == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.registrationDepartment }}
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="inspectionDate" align="center" label="检验日期">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.inspectionDate == '' || scope.row.inspectionDate == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.inspectionDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150" show-overflow-tooltip prop="registrationDepartmentContactPerson" align="center" label="登记部门联系人">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.registrationDepartmentContactPerson == '' || scope.row.registrationDepartmentContactPerson == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.registrationDepartmentContactPerson }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150" show-overflow-tooltip prop="registrationDepartmentContactNumber" align="center" label="登记部门联系电话">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.registrationDepartmentContactNumber == '' || scope.row.registrationDepartmentContactNumber == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.registrationDepartmentContactNumber }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="200" show-overflow-tooltip prop="installationUnitUnifiedSocialCreditCode" align="center" label="安装单位统一社会信用代码">
          <template slot-scope="scope">
            <div class="textStyle" v-if="scope.row.installationUnitUnifiedSocialCreditCode == '' || scope.row.installationUnitUnifiedSocialCreditCode == null">
              -
            </div>
            <div class="textStyle" v-else>
              {{ scope.row.installationUnitUnifiedSocialCreditCode }}
            </div>
          </template>
        </el-table-column>

        <!-- <el-table-column prop="joinPartyDeclaration" align="center" label="入党誓言">
          <template slot-scope="scope"
            ><div class="textStyle" :title="scope.row.joinPartyDeclaration">
              {{ scope.row.joinPartyDeclaration }}
            </div></template
          >
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="250px" fixed="right">
          <template slot-scope="scope">
            <div class="tableBtns" style="z-index: 2;position: relative;">
              <div class="operationText" @click.stop="openDetail(scope.row)">
                <i class="el-icon-tickets" style="color: #8dacfa; font-size: 16px; margin-right: 2px"></i>
                <span style="white-space: nowrap;">详情</span>
              </div>
              <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>
      <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" label-width="120px" class="dialogFormBox">
          <el-form-item label="设备名称" prop="devName">
            <el-input :disabled="true" v-model="addEditForm.devName" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="设备代码" prop="devCode">
            <el-input :disabled="isDetail" v-model="addEditForm.devCode" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="出厂编号" prop="serialNumber">
            <el-input :disabled="isDetail" v-model="addEditForm.serialNumber" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="制造许可证" prop="manufacturingLicense">
            <el-input :disabled="isDetail" v-model="addEditForm.manufacturingLicense" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="制造厂家" prop="manufacturer">
            <el-input :disabled="isDetail" v-model="addEditForm.manufacturer" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="出厂日期" prop="manufactureDate">
            <el-date-picker
              :disabled="isDetail"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="addEditForm.manufactureDate"
              type="datetime"
              placeholder="请选择时间"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="产权编号" prop="propertyNumber">
            <el-input :disabled="isDetail" v-model="addEditForm.propertyNumber" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="设备产权单位" prop="equipmentPropertyRightsUnit">
            <el-input :disabled="isDetail" v-model="addEditForm.equipmentPropertyRightsUnit" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contactPerson">
            <el-input :disabled="isDetail" v-model="addEditForm.contactPerson" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contactNumber">
            <el-input :disabled="isDetail" v-model="addEditForm.contactNumber" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="使用登记编号" prop="useRegistrationNumber">
            <el-input :disabled="isDetail" v-model="addEditForm.useRegistrationNumber" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="安装单位" prop="installationUnit">
            <el-input :disabled="isDetail" v-model="addEditForm.installationUnit" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="检测编号" prop="inspectionNumber">
            <el-input :disabled="isDetail" v-model="addEditForm.inspectionNumber" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="检验检测机构" prop="inspectionAndTestingAgency">
            <el-input :disabled="isDetail" v-model="addEditForm.inspectionAndTestingAgency" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="验收日期" prop="acceptanceDate">
            <el-date-picker
              :disabled="isDetail"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="addEditForm.acceptanceDate"
              type="datetime"
              placeholder="请选择时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="登记日期" prop="registrationDate">
            <el-date-picker
              :disabled="isDetail"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="addEditForm.registrationDate"
              type="datetime"
              placeholder="请选择时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="登记部门" prop="registrationDepartment">
            <el-input :disabled="isDetail" v-model="addEditForm.registrationDepartment" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="检验日期" prop="inspectionDate">
            <el-date-picker
              :disabled="isDetail"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="addEditForm.inspectionDate"
              type="datetime"
              placeholder="请选择时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="登记部门联系人" prop="registrationDepartmentContactPerson">
            <el-input :disabled="isDetail" v-model="addEditForm.registrationDepartmentContactPerson" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="登记部门联系电话" prop="registrationDepartmentContactNumber">
            <el-input :disabled="isDetail" v-model="addEditForm.registrationDepartmentContactNumber" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="安装单位统一社会信用代码" prop="installationUnitUnifiedSocialCreditCode">
            <el-input :disabled="isDetail" v-model="addEditForm.installationUnitUnifiedSocialCreditCode" placeholder="请输入"></el-input>
          </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 {
  bridgeErectMachineDevArchiveAdd,
  bridgeErectMachineDevArchiveDelete,
  bridgeErectMachineDevArchiveEdit,
  bridgeErectMachineDevArchivePage,
  getCrewListDataApi // 负责人下拉
} from '@/assets/js/api/bridgeMachineMonitor.js'
import { checkPhone } from '@/assets/js/util.js'
export default {
  mounted() {},
  created() {
    this.getList()
    this.getDutyPerson()
  },
  data() {
    return {
      isDetail: false, //是否详情
      dutyList: [], // 负责人
      fileUplodList: [],
      title: '',
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      addEditForm: {
        belongBeamField: '',
        createTime: '',
        devSn: '',
        deviceModel: '',
        deviceName: '',
        entryTime: '',
        equipmentPicture: '',
        manufacturerName: '',
        dutyUserId: '',
        dutyUserName: '',
        projectSn: '',
        updateTime: ''
      },
      addEditRules: {
        deviceName: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          }
        ],
        devSn: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          }
        ],
        belongBeamField: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          }
        ]
      },
      searchForm: {
        belongBeamField: '',
        deviceName: ''
      }
    }
  },
  methods: {
    //  负责人下拉回调
    dutySelectChange(e) {
      const item = this.dutyList.find(item => item.userId === e);
      this.addEditForm.dutyUserName = item.realName
      console.log("下拉选择",e,this.addEditForm.dutyUserName);
    },
    //查询负责人下拉
    getDutyPerson() {
      let data = {
        projectSn: this.$store.state.projectSn,
      }
      getCrewListDataApi(data).then(res => {
        if (res.code == 200) {
          this.dutyList = res.result
          console.log('获取负责人下拉', this.dutyList)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //查询列表
    getList() {
      bridgeErectMachineDevArchivePage({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        devName: this.searchForm.devName,
        // belongBeamField: this.searchForm.belongBeamField,
      }).then(result => {
        if (result.success) {
          // result.result.records.map(item => {
          //   if (item.image.includes('[')) {
          //     item.image = JSON.parse(item.image)
          //     if (item.image.length !== 0 && !item.image[0].url.includes(this.$store.state.FILEURL)) {
          //       item.image[0].url = this.$store.state.FILEURL + item.image[0].url
          //     }
          //   } else {
          //     let url = item.image

          //     if (url.length !== 0 && !url.includes(this.$store.state.FILEURL)) {
          //       item.image = [{}]
          //       item.image[0].url = this.$store.state.FILEURL + url
          //     }
          //   }
          // })
          this.List = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },

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

    edit(obj) {
      this.title = '编辑'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.fileUplodList = JSON.parse(obj.equipmentPicture)
    },
    openDetail(obj) {
      this.title = '详情'
      this.dialogShow = true
      this.isDetail = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.fileUplodList = JSON.parse(obj.equipmentPicture)
    },
    submit() {
      // if (this.addEditForm.contactNumber) {
      //   if (!checkPhone(this.addEditForm.contactNumber)) {
      //     this.$message.error('请输入正确的手机号码')
      //     return
      //   }
      // }
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      params.equipmentPicture = JSON.stringify(this.fileUplodList)
      // if (this.fileUplodList.length > 0) {
      //   this.$message.error('请上传照片')
      //   return false
      // }
      this.$refs.addEditForm.validate(valid => {
        if (valid) {
          if (this.title == '新增') {
            bridgeErectMachineDevArchiveAdd(params).then(result => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title == '编辑') {
            bridgeErectMachineDevArchiveEdit(params).then(result => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.dialogShow = false
        } else {
          return false
        }
      })
    },
    deleteDev(obj) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          bridgeErectMachineDevArchiveDelete({ id: obj.id }).then(res => {
            if (res.success) {
              this.getList()
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 删除
    handleRemove(file, fileList) {
      this.fileUplodList = fileList
    },
    handleExceed(file, fileList) {
      console.log("超出限制",file)
      this.$message.warning("只能上传一张图片")
    },
    handleSuccess(res, file, type) {
      if (res.status == 'SUCCESS') {
        this.fileUplodList.push({
          name: file.name,
          url: this.$store.state.FILEURL + file.response.data[0].imageUrl
        })
      }
    },
    close() {
      this.isDetail = false
      this.addEditForm = {}
      this.fileUplodList = []
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getList()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getList()
    },
    refresh() {
      this.searchForm = {}
      this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getList()
    }
  }
}
</script>
<style lang="less" scoped>
.tables2 {
  min-height: auto;
}
.textStyle {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  // z-index: 0;
  // position: relative;
}
</style>
