<template>
  <!-- 烟感设备管理-设备管理 -->
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <!-- 列表搜索栏 -->
      <el-form
        :inline="true"
        ref="searchFormInput"
        :model="searchFormInput"
        size="medium"
      >
        <el-form-item label="设备名称">
          <el-input
            placeholder="请输入"
            v-model="searchFormInput.devName"
          ></el-input>
        </el-form-item>
        <el-button type="primary" size="medium" plain @click="getList"
          >查询</el-button
        >
        <el-button type="warning" size="medium" plain @click="refresh"
          >刷新</el-button
        >
        <el-button type="primary" size="medium" @click="add">新增</el-button>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List" height="800">
        <el-table-column
          prop="devName"
          align="center"
          label="设备名称"
        ></el-table-column>
        <el-table-column
          prop="devSn"
          label="设备编号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="location"
          label="安装位置"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="dutyTeamInfoName"
          label="责任班组"
          align="center"
        ></el-table-column>
        <el-table-column label="图片" width="80" align="center" prop="image">
          <div slot 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"
            />
          </div>
        </el-table-column>
        <el-table-column prop="online " label="使用状态" align="center">
          <template slot-scope="scope">{{
                       scope.row.online  == 0 ? '离线' : '在线'

          }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div class="tableBtns">
              <div class="operationText last" @click="deilBtn(scope.row)">
                <i
                  class="el-icon-tickets"
                  style="color: #8dacfa; font-size: 16px; margin-right: 2px"
                ></i>
                <span>详情</span>
              </div>
              <div @click="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>编辑</span>
              </div>
              <div @click="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(pageTotal)"
      ></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="devName">
            <el-input
              :disabled="title == '设备详情'"
              v-model="addEditForm.devName"
              :placeholder="isAdding ? '请输入' : ''"
            ></el-input>
          </el-form-item>
          <el-form-item label="设备编号" prop="devSn">
            <el-input
              :disabled="title == '设备详情'"
              v-model="addEditForm.devSn"
              :placeholder="isAdding ? '请输入' : ''"
            ></el-input>
          </el-form-item>
          <el-form-item label="安装位置" prop="location">
            <el-input
              :disabled="title == '设备详情'"
              v-model="addEditForm.location"
              :placeholder="isAdding ? '请输入' : ''"
            ></el-input>
          </el-form-item>
          <el-form-item label="责任班组" prop="dutyTeamInfoId">
            <el-select
              :disabled="title == '设备详情'"
              v-model="addEditForm.dutyTeamInfoId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.teamName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="使用状态"
            prop="location"
            v-if="title == '设备详情'"
          >
            <el-input
              :disabled="title == '设备详情'"
              :placeholder="status==1?'正常':'异常'"
              type="number"
            >正常</el-input>
          </el-form-item>
          <el-form-item label="上传图片">
            <el-upload
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              :file-list="fileList"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
              :disabled="title == '设备详情'"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              :disabled="title == '设备详情'"
              type="textarea"
              :rows="2"
              v-model="addEditForm.remark"
              :placeholder="isAdding ? '请输入' : ''"
            ></el-input>
          </el-form-item>
          <div class="dialog-footer" v-if="title != '设备详情'">
            <el-button
              class="cancleBtn"
              @click="diaLogShow = false"
              icon="el-icon-circle-close"
              size="medium"
              >取消
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submit"
              size="medium"
              >保存
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addSmokeDevApi,
  deleteSmokeDevApi,
  editeSmokeDevApi,
  getSmokeDevPageApi,
  getProjectTeamListApi,//查询所有班组列表
} from "@/assets/js/api/edgeProtection";
export default {
  mounted() {
    this.getTeamList()
    this.getList()
  },
  data() {
    return {
      isAdding:true,
      searchFormInput: {
        devName: '',
      },
      addEditForm: {
        devName: '',
        devSn: '',
        location: '',
        dutyTeamInfoId: '',
        image: '',
        remark: '',
      },
      diaLogShow: false,
      pageTotal: 0,
      searchForm: {
        pageNo: 1,
        pageSize: 10,
      },
      fileList: [],
      addEditRules: {
        devName: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        devSn: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        location: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],

        dutyTeamInfoId: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],

      },
      options: [],
      List: [],
      title: '',
      status:'',

    }
  },
  methods: {
    //查看详情
    deilBtn(val) {
      this.isAdding=false
      console.log('查看详情',val);
      this.title = '设备详情'
      this.diaLogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(val))
      this.fileList = val.image
      this.status=val.status
    },
    //获取责任班组
    getTeamList() {
      getProjectTeamListApi({
        projectSn: this.$store.state.projectSn
      }).then((res) => {
        if (res.success) {
          console.log('获取责任班组', res);
          this.options = res.result
        }
      })
    },
    add() {
      this.diaLogShow = true
      this.title = '新增设备'
      this.fileList = []
      this.isAdding=true
    },
    edit(obj) {
      this.diaLogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.fileList = obj.image
      this.title = '编辑设备'
    },
    deleteDev(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteSmokeDevApi({ id: obj.id }).then((result) => {
            if (result.success) {
              this.$message.success(result.message)
              this.getList()
            }
          })
        })
        .catch(() => { })
    },
    submit() {
      this.processTheFile()
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增设备') {
            addSmokeDevApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title == '编辑设备') {
            console.log('编辑信息', this.addEditForm)
            editeSmokeDevApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.diaLogShow = false
        } else {
          return false
        }
      })
    },
    refresh() {
      this.searchFormInput = {}
      this.getList()
    },
    getList() {
      getSmokeDevPageApi({
        projectSn: this.$store.state.projectSn,
        pageSize: this.searchForm.pageSize,
        pageNo: this.searchForm.pageNo,
        devName: this.searchFormInput.devName
      }).then((result) => {
        if (result.success) {
          console.log('查询设备管理分页列表', result)
          this.List = result.result.records
          this.pageTotal = result.result.total
          result.result.records.map((item) => {
            if (item.image != null) {
              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{
              item.image = []
            }
            
          })
        
          console.log('run', this.List)
        }
      })
    },

    close() {
      this.addEditForm = {}
      this.fileList = []
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    processTheFile() {
      //处理el上传的文件格式(结构)
      this.addEditForm.image = []
      this.fileList.map((item) => {
        if (!item.response) {
          this.addEditForm.image.push(item)
          // console.log('符合的结构', item)
        } else if (item.response) {
          // console.log('不符合的结构', item)
          this.addEditForm.image.push({
            name: item.response.data[0].filename,
            url: item.response.data[0].imageUrl
          })
        }
      })
      this.addEditForm.image = JSON.stringify(this.addEditForm.image)

    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList
    },
    handleRemove(response, fileList) {
      this.fileList = fileList
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.searchForm.pageNo = val
      this.getList()
    },
  }
}
</script>
<style lang="less">
.tables {
  img {
    cursor: pointer;
  }
}

.tableBtns {
  display: flex;
  justify-content: center;
}

.yj-dialogFormBox {
  width: 462px;
}
</style>