<template>
  <!-- 质量亮点页面 -->
  <div class="panoramaPlan">
    <div class="header-box">
      <el-button type="primary" @click="addForm"> 新增 </el-button>
    </div>
    <div class="table-box">
      <el-table class="tables" :data="tableData">
        <el-table-column
          prop="title"
          label="标题"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createDate"
          label="上传时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="typeId"
          label="文件类型"
          align="center"
        >
        <template slot-scope="scope">
          {{ getStatus(scope.row.typeId) }}
        </template>
        </el-table-column>
        <el-table-column prop="imageUrl" label="图片" align="center">
          <template slot-scope="scope">
            <img
              :preview="scope.row.imageUrl[0] ? scope.row.imageUrl[0].url : []"
              :src="scope.row.imageUrl[0] ? scope.row.imageUrl[0].url : []"
              alt=""
              width="50px"
            />
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit-outline"
              @click="editData(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              class="delete-btn"
              type="text"
              icon="el-icon-delete"
              @click="deleteData(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="Number(total)"
        background
      ></el-pagination>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      @close="close"
      class="dialog"
      :title="dialogTitle"
      :modal-append-to-body="false"
      :visible.sync="dialogVisible"
      width="667px"
    >
      <div class="dialog-content">
        <vue-scroll>
          <el-form
            ref="form"
            :model="dialogdata"
            :rules="rules"
            label-width="155px"
            size="medium"
            class="dialogFormBox"
          >
            <el-form-item label="文件类型" prop="typeId">
              <el-select v-model="dialogdata.typeId" placeholder="请选择">
                <el-option :value="1" label="质量"></el-option>
                <el-option :value="2" label="安全"></el-option>
                <el-option :value="3" label="技术"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="上传时间" prop="createDate">
              <el-date-picker
                v-model="dialogdata.createDate"
                type="datetime"
                placeholder="选择日期"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="文件标题" prop="title">
              <el-input
                v-model="dialogdata.title"
                placeholder="请输入"
              ></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"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-form>
        </vue-scroll>
      </div>
      <div class="dialog-btn">
        <el-button type="primary" @click="submitForm"> 确定 </el-button>
        <el-button @click="dialogVisible = false"> 取消 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getWindowDisplayApi, //分页查询亮点展示
  addWindowDisplaytApi, //新增
  updateWindowDisplayApi,//编辑
  deleteWindowDisplayApi,//删除
} from "@/assets/js/api/quality";

export default {
  data() {
    return {
      tableData: [], //列表数据
      dialogTitle: "",
      dialogVisible: false,
      dialogdata: {
        typeId: "", //文件类型
        createDate: "", //上传时间
        title: "", //文件标题
        imageUrl: "", //图片地址
      },
      rules: {},
      fileList: [],
      current: 1,
      size: 10,
      total: 0,
    };
  },
  created() {
    this.getData();
  },
  methods: {
     getStatus(typeId){
     if(typeId===1){
      return '质量'
     }else if(typeId===2){
      return '安全'
     }else{
      return '技术'
     }
    },
    //查询分部分项验收列表
    getData() {
      getWindowDisplayApi({
        projectSn: this.$store.state.projectSn,
        size:this.size,
        current:this.current
      }).then((res) => {
        res.result.records.map((item) => {

          if(item.imageUrl.includes("[")){
            item.imageUrl = JSON.parse(item.imageUrl);
            console.log("亮点展示 ====");
          if (item.imageUrl.length !== 0 && !item.imageUrl[0].url.includes(this.$store.state.FILEURL)) {
            item.imageUrl[0].url =
              this.$store.state.FILEURL + item.imageUrl[0].url;
          }
          }else{
            let url = item.imageUrl
           
            if (url.length !== 0 && !url.includes(this.$store.state.FILEURL)) {
              item.imageUrl = [{}]
            item.imageUrl[0].url =
              this.$store.state.FILEURL + url;
          }
          }
          
         
        });
        console.log("亮点展示", res);
        this.tableData = res.result.records;
        this.total = res.result.total;
      });
    },
    //新增
    addForm() {
      this.dialogVisible = true;
      this.dialogTitle = "新增质量亮点";
    },
    //编辑
    editData(val) {
      this.dialogVisible = true;
      this.dialogTitle = "编辑质量亮点";
      this.dialogdata = JSON.parse(JSON.stringify(val));
      this.fileList = val.imageUrl;
    },
    //删除
    deleteData(val){
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteWindowDisplayApi({ id: val.id }).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增或编辑提交
    submitForm() {
      this.processTheFile();
      let params = JSON.parse(JSON.stringify(this.dialogdata));
      params.projectSn = this.$store.state.projectSn;
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.dialogTitle == "新增质量亮点") {
            addWindowDisplaytApi(params).then((res) => {
              this.$message.success("新增成功");
              this.dialogVisible = false;
              this.getData();
            });
          }else if(this.dialogTitle == "编辑质量亮点"){
            updateWindowDisplayApi(params).then((res) => {
              this.$message.success("编辑成功");
              this.dialogVisible = false;
              this.getData();
            });
          }
        }
      });
    },
    //图片处理
    processTheFile() {
      //处理el上传的文件格式(结构)
      this.dialogdata.imageUrl = [];
      this.fileList.map((item) => {
        if (!item.response) {
          this.dialogdata.imageUrl.push(item);
          // console.log('符合的结构', item)
        } else if (item.response) {
          // console.log('不符合的结构', item)
          this.dialogdata.imageUrl.push({
            name: item.response.data[0].filename,
            url: item.response.data[0].imageUrl,
          });
        }
      });
      console.log("处理el上传的结构后", this.dialogdata.imageUrl);
      this.dialogdata.imageUrl = JSON.stringify(this.dialogdata.imageUrl);
      console.log("转字符串后", this.dialogdata.imageUrl);
    },
    close() {
      this.dialogdata = {};
      this.fileList = [];
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleSuccess(response, file, fileList) {
      console.log("图片上传成功", fileList);
      this.fileList = fileList;
    },
    handleRemove(response, fileList) {
      this.fileList = fileList;
    },
    handleSizeChange(value) {
      this.size = value;
      this.getData();
    },
    handleCurrentChange(value) {
      this.current = value;
      this.getData();
    },
  },
};
</script>

<style lang="less" scoped>
.panoramaPlan {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 20px;
  box-sizing: border-box;
  .header-box {
    margin-bottom: 20px;
    padding-left: 10px;
    .search-item {
      display: inline-flex;
      align-items: center;
      white-space: nowrap;
      margin-right: 20px;
      /deep/.el-input {
        width: 200px;
      }
    }
  }
  .delete-btn {
    color: #f56c6c;
  }

  .dialog-content {
    height: 360px;
    margin-bottom: 60px;
    margin-left: -100px;
  }
  .dialog-btn {
    margin-left: 255px;
  }
  ::v-deep .el-upload--picture-card {
    position: absolute;
  }
}
</style>
