<template>
  <div class="fullHeight whiteBlock">
    <div style="padding: 15px 15px 0">
      <p class="pageTitle">见证取样管理</p>
      <el-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        size="medium"
      >
        <el-form-item label="项目名称">
          <el-input
            v-model="searchForm.projectName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="样品名称">
          <el-input
            v-model="searchForm.sampleName"
            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="projectName"
          label="项目名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sampleName"
          align="center"
          label="样品名称"
        ></el-table-column>
        <el-table-column
          prop="engineeringPart"
          align="center"
          label="工程部位"
        ></el-table-column>
        <el-table-column
          prop="sampleSpecification"
          align="center"
          label="样品规格"
        ></el-table-column>
        <el-table-column
          prop="conservationMethod"
          align="center"
          label="养护方式"
        ></el-table-column>
        <el-table-column
          prop="manufacturer"
          align="center"
          label="生产厂家"
        ></el-table-column>
        <el-table-column
          prop="productionDate"
          align="center"
          label="生产日期"
        ></el-table-column>
        <el-table-column
          prop="sampler"
          align="center"
          label="取样人"
        ></el-table-column>
        <el-table-column prop="samplerPhoto" align="center" label="取样人照片">
          <template slot-scope="scope">
            <img
              :preview="
                scope.row.samplerPhoto[0] ? scope.row.samplerPhoto[0].url : []
              "
              :src="
                scope.row.samplerPhoto[0] ? scope.row.samplerPhoto[0].url : []
              "
              alt=""
              width="50px"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="witness"
          align="center"
          label="见证人"
        ></el-table-column>
        <el-table-column prop=" witnessPhoto" align="center" label="见证人照片">
          <template slot-scope="scope">
            <img
              :preview="
                scope.row.witnessPhoto[0] ? scope.row.witnessPhoto[0].url : []
              "
              :src="
                scope.row.witnessPhoto[0] ? scope.row.witnessPhoto[0].url : []
              "
              alt=""
              width="50px"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="witnessSamplingVideo"
          align="center"
          label="见证取样视频"
        >
          <template slot-scope="scope">
            <i
              @click.stop="playerVideo(scope.row.witnessSamplingVideo)"
              class="el-icon-video-play"
              style="font-size: 25px; cursor: pointer"
            ></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="testimonialPhoto"
          align="center"
          label="见证照片"
        >
          <template slot-scope="scope">
            <img
              :preview="
                scope.row.testimonialPhoto[0]
                  ? scope.row.testimonialPhoto[0].url
                  : []
              "
              :src="
                scope.row.testimonialPhoto[0]
                  ? scope.row.testimonialPhoto[0].url
                  : []
              "
              alt=""
              width="50px"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="witnessTime"
          align="center"
          label="见证时间"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <div class="tableBtns" style="margin-left: 30px !important">
              <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"
        @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="projectName">
            <el-input
              v-model="addEditForm.projectName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="样品名称" prop="sampleName">
            <el-input
              v-model="addEditForm.sampleName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="工程部位" prop="engineeringPart">
            <el-input
              v-model="addEditForm.engineeringPart"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="样品规格" prop="sampleSpecification">
            <el-input
              v-model="addEditForm.sampleSpecification"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="养护方式" prop="manufacturer">
            <el-input
              v-model="addEditForm.conservationMethod"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="生产厂家" prop="manufacturer">
            <el-input
              v-model="addEditForm.manufacturer"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="生产日期" prop="productionDate">
            <el-date-picker
              v-model="addEditForm.productionDate"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="取样人" prop="sampler">
            <el-input
              v-model="addEditForm.sampler"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="见证人" prop="witness">
            <el-input
              v-model="addEditForm.witness"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="见证日期" prop="productionDate">
            <el-date-picker
              v-model="addEditForm.witnessTime"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="取样人照片" prop="samplerPhoto">
            <el-upload
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              :file-list="fileUplodList1"
              :on-success="(file) => handleSuccess(file, 1)"
              :on-remove="(file, fileList) => handleRemove(file, fileList, 1)"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="见证人照片" prop="witnessPhoto">
            <el-upload
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              :file-list="fileUplodList2"
              :on-success="(file) => handleSuccess(file, 2)"
              :on-remove="(file, fileList) => handleRemove(file, fileList, 2)"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="见证照片" prop="testimonialPhoto">
            <el-upload
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              :file-list="fileUplodList3"
              :on-success="(file) => handleSuccess(file, 3)"
              :on-remove="(file, fileList) => handleRemove(file, fileList, 3)"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="见证取样视频" prop="witnessSamplingVideo">
            <div class="videoUpload">
              <el-upload
                class="upload-demo"
                name="files"
                :action="$store.state.UPLOADURL"
                :on-success="(file) => handleSuccess(file, 4)"
                :show-file-list="false"
                accept="video/mp4"
              >
                <el-button type="primary">上传视频</el-button>
              </el-upload>
              <video
                v-if="isRouterAlive || addEditForm.witnessSamplingVideo"
                :src="$store.state.FILEURL + addEditForm.witnessSamplingVideo"
                width="100%"
                height="200"
                controls
              ></video>
            </div>
          </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 class="video" style="position: absolute; top: 12%; left: 17%">
      <video
        v-if="isShow || player"
        width="100%"
        height="600"
        controls
        autoplay
        style="margin-left: 10px"
      >
        <source :src="player" type="video/mp4" />
        <source :src="player" type="video/mp4" />
        <source :src="player" type="video/mp4" />
        您的浏览器不支持 video 标签。
      </video>
      <i
        style="
          position: absolute;
          top: 1%;
          left: 97%;
          color: #fff;
          font-size: 30px;
          cursor: pointer;
        "
        class="el-icon-circle-close"
        @click.stop="closeBtn"
      ></i>
    </div>
  </div>
</template>
<script>
import {
  addgtMaterialWitnessSampleApi,
  deletgtMaterialWitnessSampleApi,
  editgtMaterialWitnessSampleApi,
  gettgtMaterialWitnessSamplePageApi
} from '@/assets/js/api/materialManagement.js'
export default {
  mounted() {
    this.getList()
  },
  data() {
    return {
      isRouterAlive: false,
      title: "",
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      addEditForm: {
        projectName: "",
        sampleName: "",
        engineeringPart: "",
        sampleSpecification: "",
        conservationMethod: "",
        manufacturer: "",
        productionDate: "",
        sampler: "",
        samplerPhoto: "",//取样人照片
        witness: "",
        witnessPhoto: "",//见证人照片
        witnessSamplingVideo: "",//见证取样视频
        testimonialPhoto: "",//见证照片
        witnessTime: "",
      },
      addEditRules: {
        projectName: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
        ],
        sampleName: [
          {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
        ],
      },
      fileUplodList1: [],
      fileUplodList2: [],
      fileUplodList3: [],
      fileList: [],
      searchForm: {
        projectName: '',
        sampleName: '',
      },
      videoUrl: '',
      isShow: false,
      player: '',

    }
  },
  methods: {
    //关闭视频
    closeBtn() {
      this.isShow = false
      this.player = ''
    },
    //视频播放
    playerVideo(item) {
      console.log('点击', this.$store.state.FILEURL + item);
      if (item == null) {
        this.$message.warning('暂无视频')
        this.isShow = false
      } else {
        this.isShow = false
        this.$nextTick(() => {
          this.player = this.$store.state.FILEURL + item
          this.isShow = true

        })
      }

    },
    // 删除
    handleRemove(file, fileList, type) {
      if (type == 1) {
        this.fileUplodList1 = fileList
      } else if (type == 2) {
        this.fileUplodList2 = fileList
      }
      else if (type == 3) {
        this.fileUplodList3 = fileList
      }

    },
    //上传
    handleSuccess(file, type) {
      let data = {
        name: file.data[0].filename,
        url: this.$store.state.FILEURL + file.data[0].imageUrl
      }
      if (type == 1) {
        this.fileUplodList1.push(
          data
        )
      }
      else if (type == 2) {
        this.fileUplodList2.push(
          data
        )
      }
      else if (type == 3) {
        this.fileUplodList3.push(
          data
        )
      }
      else if (type == 4) {
        this.isRouterAlive = false;
        this.$nextTick(() => {
          this.addEditForm.witnessSamplingVideo = file.data[0].imageUrl
          this.isRouterAlive = true;
        })
      }
    },

    //查询仓库数据
    getList() {
      gettgtMaterialWitnessSamplePageApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        projectName: this.searchForm.projectName,
        sampleName: this.searchForm.sampleName,
      }).then((res) => {
        console.log(' res.result.records======', res.result.records);
        res.result.records.map((item) => {

          if (item.samplerPhoto.includes("[")) {
            item.samplerPhoto = JSON.parse(item.samplerPhoto);
            if (item.samplerPhoto.length !== 0 && !item.samplerPhoto[0].url.includes(this.$store.state.FILEURL)) {
              item.samplerPhoto[0].url =
                this.$store.state.FILEURL + item.samplerPhoto[0].url;
            }
          } else {
            let url = item.samplerPhoto

            if (url.length !== 0 && !url.includes(this.$store.state.FILEURL)) {
              item.samplerPhoto = [{}]
              item.samplerPhoto[0].url =
                this.$store.state.FILEURL + url;
            }
          }
          if (item.witnessPhoto.includes("[")) {
            item.witnessPhoto = JSON.parse(item.witnessPhoto);
            if (item.witnessPhoto.length !== 0 && !item.witnessPhoto[0].url.includes(this.$store.state.FILEURL)) {
              item.witnessPhoto[0].url =
                this.$store.state.FILEURL + item.witnessPhoto[0].url;
            }
          } else {
            let url = item.witnessPhoto

            if (url.length !== 0 && !url.includes(this.$store.state.FILEURL)) {
              item.witnessPhoto = [{}]
              item.witnessPhoto[0].url =
                this.$store.state.FILEURL + url;
            }
          }
          if (item.testimonialPhoto.includes("[")) {
            item.testimonialPhoto = JSON.parse(item.testimonialPhoto);
            if (item.testimonialPhoto.length !== 0 && !item.testimonialPhoto[0].url.includes(this.$store.state.FILEURL)) {
              item.testimonialPhoto[0].url =
                this.$store.state.FILEURL + item.testimonialPhoto[0].url;
            }
          } else {
            let url = item.testimonialPhoto

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

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

    edit(obj) {
      this.title = '编辑取样'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.fileUplodList1 = obj.samplerPhoto
      this.fileUplodList2 = obj.witnessPhoto
      this.fileUplodList3 = obj.testimonialPhoto
    },
    submit() {
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      params.samplerPhoto = JSON.stringify(this.fileUplodList1)
      params.witnessPhoto = JSON.stringify(this.fileUplodList2)
      params.testimonialPhoto = JSON.stringify(this.fileUplodList3)
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增取样') {
            addgtMaterialWitnessSampleApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title == '编辑取样') {
            editgtMaterialWitnessSampleApi(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(() => {
          deletgtMaterialWitnessSampleApi({ 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: "已取消删除",
          });
        });
    },

    close() {
      this.fileUplodList1 = [],
        this.fileUplodList2 = [],
        this.fileUplodList3 = [],
        this.addEditForm = {}
      this.isRouterAlive = false;
      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;
}
</style>