<template>
  <div class="app-container">
    <el-form v-if="1==2" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图片路径" prop="imageUrl">
        <el-input v-model="queryParams.imageUrl" placeholder="请输入图片路径" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="视频路径" prop="videoUrl">
        <el-input v-model="queryParams.videoUrl" placeholder="请输入视频路径" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="视频日期" prop="videoDate">
        <el-date-picker clearable v-model="queryParams.videoDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择视频日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input v-model="queryParams.state" placeholder="请输入状态" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="${comment}" prop="isDel">
        <el-input v-model="queryParams.isDel" placeholder="请输入${comment}" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="${comment}" prop="createDate">
        <el-date-picker clearable v-model="queryParams.createDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="${comment}" prop="updateDate">
        <el-date-picker clearable v-model="queryParams.updateDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['video:prjphotography:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['video:prjphotography:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['video:prjphotography:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="1==2">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['video:prjphotography:export']">导出</el-button>
      </el-col>
      <right-toolbar :search="false"  :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prjphotographyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />      
      <el-table-column label="月份" align="center" prop="videoDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.videoDate, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片路径" align="center" prop="imageUrl" >
        <template slot-scope="{row}">
          <el-image ref="preview" style="height: 80px; " :preview-src-list="row.images"
                            :src="row.image + '.min.jpg'">
                        </el-image>
        </template>
      </el-table-column>
      <el-table-column label="视频路径" align="center" prop="videoUrl" >
        <template slot-scope="{row}">
          <video  height="80" controls>
            <source :src="row.videoUrl" type="video/mp4"/>
          您的浏览器不支持Video标签。
          </video>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['video:prjphotography:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['video:prjphotography:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改项目延时摄影对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="720px" custom-class="prj-photography-drawer-dlg"
    append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频" prop="videoUrl" required>
                <file-upload ref="fu1"  v-model="form.videoUrl" @input="clearForm"  :limit="1" :fileSize="100"  :fileType="['mp4']"/>
          </el-form-item>
          <el-form-item label="视频月份" prop="videoDate" required>
          <el-date-picker clearable v-model="form.videoDate" type="month" value-format="yyyy-MM-dd" placeholder="请选择视频月份">
          </el-date-picker>
        </el-form-item>  
        <el-form-item label="照片" prop="imageUrls" required>
          <phontographyUpload :videoDate="form.videoDate" ref="phUp1" v-model="form.imageUrls"  @input="clearForm" ></phontographyUpload>
        </el-form-item>  
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import phontographyUpload from './phontographyUpload.vue'
import { listPrjphotography, getPrjphotography, delPrjphotography, addPrjphotography, updatePrjphotography } from "@/api/video/prjphotography";

export default {
  components:{
    phontographyUpload
  },
  data() {
    let checkImages=this.checkImages;
    let checkVideo=this.checkVideo;
    return {
      prj:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目延时摄影表格数据
      prjphotographyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imageUrl: null,
        imageUrls:[],
        videoUrl: null,
        videoDate: null,
        state: null,
        isDel: null,
        createDate: null,
        updateDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        videoUrl:[{ required: true, validator: checkVideo }],
        videoDate:[{ required: true, message: '视频月份不能为空' }],
        imageUrls:[{ required: true,  validator: checkImages }],
      }
    };
  },
  created() {
    
    window.app99=this
  },
  methods: {
    clearForm(){
      this.$refs["form"].clearValidate();
    },
    checkVideo(rule, value, callback){
      if(!value || value.length==0){
        callback(new Error('视频不能为空'));
        return;
      }     
      callback();
    },
    checkImages(rule, value, callback){
      if(!value){
        callback(new Error('照片不能为空'));
        return;
      }
      if(value.filter(d=>!d.date).length>0){
        callback(new Error("照片日期不能为空"));
        return;
      }
      if(value.filter(d=>!d.url).length>0){
        callback(new Error("照片不能为空"));
        return;
      }
      callback();
    },
    init(prj) {
      this.prj=prj;
      this.getList();
    },
    /** 查询项目延时摄影列表 */
    getList() {
      this.loading = true;      
      this.queryParams.projectId=this.prj.id;
      listPrjphotography(this.queryParams).then(response => {
        this.prjphotographyList = (response.rows||[]).map(it=>{
          let tmps=it.imageUrl?this.$tryToJson(it.imageUrl,[]):[];
          it.imageUrls=tmps;
          it.images=tmps.map(item=>item.url);
          it.image=it.images.length>0?it.images[0]:'';
          return it;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        imageUrl: null,
        videoUrl: null,
        videoDate: null,
        state: null,
        remark: null,
        isDel: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "项目延时摄影 - 新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPrjphotography(id).then(response => {
        this.form = response.data;
        this.form.imageUrls=(this.$tryToJson(this.form.imageUrl,[]));
        this.open = true;
        this.title = "项目延时摄影 - 修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.imageUrls.forEach(it=>{
            it.date=this.$dt(it.date).format("YYYY-MM-DD HH:mm:ss");
          })
          let postData={
            imageUrl:JSON.stringify(this.form.imageUrls),
            isDel:0,
            remark:'',
            projectId:this.prj.id,
            id:this.form.id,
            state:0,
            videoDate:this.form.videoDate,
            videoUrl:this.form.videoUrl[0].url
          }
          if (this.form.id != null) {
            updatePrjphotography(postData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrjphotography(postData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目延时摄影编号为"' + ids + '"的数据项？').then(function () {
        return delPrjphotography(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/prjphotography/export', {
        ...this.queryParams
      }, `prjphotography_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.prj-photography-drawer-dlg{
  .el-dialog__body{
    padding:12px 20px;
    .ele-upload-list__item-content-action{
      min-width: 40px;
      text-align: right;
    }
    .upload-file.up-file-cnt-1{
      .upload-file-uploader{
        display: none;
      }
    }
  }
  
}
</style>