<template>
  <div class="app-container calendar-list-container" v-loading.body="showLoadingBody">
    <div>
      <div class="filter-container" ref="filter-container">
        <el-form :inline="inline" ref="queryForm" :model="listQuery" label-width="100px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="发布时间" prop="startTime">
                <el-date-picker
                  v-model="listQuery.startTime"
                  type="date"
                  :editable="false"
                  format="yyyy-MM-dd"
                  prop="startTime"
                  placeholder="请选择开始时间">
                </el-date-picker>~
                <el-date-picker
                  v-model="listQuery.endTime"
                  type="date"
                  :editable="false"
                  format="yyyy-MM-dd"
                  prop="endTime"
                  placeholder="请选择结束时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="来源">
                <el-select class="filter-item" v-model="listQuery.source" placeholder="请选择来源">
                  <el-option :key="undefined" label="全部" :value="undefined"></el-option>
                  <el-option key="1" label="后台创建" value="1"></el-option>
                  <el-option key="2" label="app用户" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="点赞数 >">
                <el-input v-model.number="listQuery.praise" type="number" min="0" placeholder="请输入点赞数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="评论数 >">
                <el-input v-model.number="listQuery.comment" type="number" min="0" placeholder="请输入评论数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="用户名">
                <el-input v-model.number="listQuery.username" placeholder="请输入用户名"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
        <el-button class="filter-item" type="primary" v-waves @click="reloadPage">重置</el-button>
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate"
                   type="primary" icon="edit">添加
        </el-button>
      </div>
      <el-table :key='tableKey' :data="list" border fit highlight-current-row
                style="width: 100%">
        <el-table-column
          type="index"
          align="center" label="序号" width="65">
        </el-table-column>
        <el-table-column align="center" label="msgId">
          <template scope="scope">
            <span>{{scope.row.msgId}}</span>
          </template>
        </el-table-column>
        <!--<el-table-column align="center" label="标题">-->
          <!--<template scope="scope">-->
            <!--<span>{{scope.row.body?scope.row.body.title:''}}</span>-->
          <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" label="描述">
          <template scope="scope">
            <span>{{scope.row.body?scope.row.body.text:''}}</span>
          </template>
        </el-table-column>
        <el-table-column  align="center" width="100" label="图片">
          <template scope="scope">
            <el-button v-if="scope.row.body.images" size="small" @click="handleImg(scope.row.body.images)" class="el-button el-button--text el-button--small" >点击查看
            </el-button>
          </template>
        </el-table-column>
        <el-table-column width="100" align="center" label="点赞数">
          <template scope="scope">
            <span>{{scope.row.count?scope.row.count.praise:0}}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" align="center" label="评论数">
          <template scope="scope">
            <span>{{scope.row.count?scope.row.count.comment:0}}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" align="center" label="来源">
          <template scope="scope">
            <span>{{scope.row.source == 1? '后台创建':'app用户'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="操作" fixed="right">
          <template scope="scope">
            <el-button size="small"  class="el-button el-button--text el-button--small" @click="handleUpdate(scope.row)">编辑
            </el-button>
            <el-button size="small"class="el-button el-button--text el-button--small" style="color: green;" v-if="scope.row.visible==2" @click="upStatus(scope.row)">设置为公开
            </el-button>
            <el-button size="small" class="el-button el-button--text el-button--small" style="color: red" v-if="scope.row.visible==1" @click="upStatus(scope.row)">设置为保密
            </el-button>
            <el-button  class="el-button el-button--text el-button--small" style="color:red;" size="small" v-if="scope.row.source == 1" @click="deleteHandler(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                       layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </div>
    </div>
    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="oneTypeDialogVisible">
      <el-form label-width="90px">
        <el-row>
          <el-col :span="16">
            <el-row>
              <el-col :span="16">
                <el-form-item label="标题">
                  <el-input v-model="formE.title" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="16">
                <el-form-item label="描述">
                  <el-input v-model="formE.desc" disabled type="textarea" :rows="4"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="图片" :style="{display:'block'}">
                  <img style="width: 100px;height: 100px;object-fit: cover;margin: 5px;" v-for="(iitem, iindex) in formE.images" :key="iindex" class="text item" :src="iitem.oUrl"/>
                 </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间" prop="name">
              <el-input v-model="formE.crtTime" disabled></el-input>
            </el-form-item>
            <el-form-item label="userId" prop="name">
              <el-input v-model="formE.userId" disabled></el-input>
            </el-form-item>
            <el-form-item label="点赞数" prop="name">
              <el-input v-model="formE.praise" disabled></el-input>
            </el-form-item>
            <el-form-item label="评论数" prop="name">
              <el-input v-model="formE.comment" disabled></el-input>
            </el-form-item>
            <el-form-item label="分享次数" prop="name">
              <el-input v-model="formE.share" disabled></el-input>
            </el-form-item>
            <el-form-item label="创建位置" prop="name">
              <el-input v-model="formE.address" disabled></el-input>
            </el-form-item>
            <el-form-item label="对外可见">
              <el-select class="filter-item" v-model="formE.visible" placeholder="请选择来源">
                <el-option key="1" label="是" value="1"></el-option>
                <el-option key="2" label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="来源">
              <el-select class="filter-item" v-model="formE.source" placeholder="请选择来源">
              <el-option key="1" label="后台创建" value="1"></el-option>
              <el-option key="2" label="app用户" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelHandel">取 消</el-button>
        <el-button type="primary" @click="updateMsgs">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 创建 -->
    <el-dialog title="创建" :visible.sync="createDialogVisible">
      <el-form label-width="90px">
        <el-row>
          <el-col :span="16">
            <el-row>
              <el-col :span="16">
                <el-form-item label="标题">
                  <el-input v-model="formC.title"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="16">
                <el-form-item label="描述">
                  <el-input :rows="4" v-model="formC.desc" type="textarea"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="图片" :style="{display:'block'}">
                  <el-upload
                    class="upload-demo"
                    :headers="getHeaderWithToken"
                    :action="BASE_API+'/api/universal/file/app/unauth/admin/upload'"
                    :on-remove="handleRemove"
                    :file-list="fileList2"
                    :on-success="handleBannerSuccess"
                    list-type="picture">
                    <div slot="tip" class="el-upload__tip">最多上传9张</div>
                    <el-button  size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-form-item label="点赞数" prop="name">
              <el-input v-model="formC.praise"></el-input>
            </el-form-item>
            <el-form-item label="评论数" prop="name">
              <el-input v-model="formC.comment"></el-input>
            </el-form-item>
            <el-form-item label="分享次数" prop="name">
              <el-input v-model="formC.share"></el-input>
            </el-form-item>
            <el-form-item label="创建位置" prop="name">
              <el-input v-model="formC.addressStr" readonly placeholder="请拾取地址" :on-icon-click="selectDestination"
                        icon="edit"></el-input>
            </el-form-item>
            <el-form-item label="对外可见">
              <el-select class="filter-item" v-model="formC.visible" placeholder="请选择来源">
                <el-option key="1" label="是" value="1"></el-option>
                <el-option key="2" label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="来源">
              <el-select class="filter-item" v-model="formC.source" placeholder="请选择来源">
                <el-option key="1" label="后台创建" value="1"></el-option>
                <el-option key="2" label="app用户" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelCreateHandel">取 消</el-button>
        <el-button type="primary" @click="createMsgs">确 定</el-button>
      </div>
    </el-dialog>

    <!--查看图片-->
    <el-dialog title="查看图片" :visible.sync="imgDialogVisible">
      <el-carousel arrow="always" height="500px" :interval="5000">
        <el-carousel-item v-for="item in imgList" :key="item.oUrl" style="display: flex;align-content: center;justify-content: center;">
          <img style="height:500px;object-fit: cover;" class="text item" :src="item.oUrl"/>
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
    <!--拾取创建位置-->
    <address-modal v-if="showAddressVisible" :obj="addressObj"  v-on:addressEvent="addressEvent"></address-modal>
  </div>
</template>
<style>
  textarea {
    display: none;
  }
  .el-upload-list {
    display: flex;
    flex-wrap: wrap;
  }
  .el-upload-list li {
    margin-left: 10px;
  }
  .el-upload-list--picture .el-upload-list__item{
    width: 100px;
  }
</style>
<script>
  import 'static/css/uploadImg.css';// 引入图片上传组件对话框
  import addressModal from './modal/addressModal'

  import {
    formatDate
  } from 'utils/dateFormattor';
  import {
    toEast8Date,
    deepCopyDate,
    newEast8Date,
    convertDate2Str,
    timestamp2Date
  } from 'utils/dateUtils';

  import rsCode from '../../utils/rsCode';

  import {mapGetters} from 'vuex';
  import {
    getToken
  } from 'utils/auth';
  import {
    objDeepCopy
  } from 'utils/index';

  import {
    page,
    updateMsg,
    delMsg
  } from 'api/discoveryModule';

  import Element1 from "../admin/menu/components/element";
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";
  import {
    getSonRegionByCodes,
    getRegionByCodes,
  } from 'api/base_info/region/';

  export default {
    name: 'tourManage',
    components: {
      ElCol,
      ElRow,
      Element1,
      addressModal
    },
    data() {
      return {
        modalTitle: "创建",
        BASE_API: process.env.BASE_API,
        oneTypeDialogVisible: false,//编辑弹框
        showLoadingBody: false,
        createDialogVisible: false,//创建弹框
        imgDialogVisible: false,//查看图片
        imgList: [],//查看图片列表
        form: {
          tagNames:"",//标签
          name: undefined,//旅游名称
          saleCount: undefined,//销量
          stock: undefined,//总数量
        },
        showAddressVisible: false,//拾取创建地址
        addressObj: {},//创建位置实体
        formE:{
          msgId: undefined,//消息id
          title: undefined,//标题
          desc:undefined,//描述
          crtTime: undefined,//创建时间
          userId: undefined,
          share: undefined,//分享次数
          praise:undefined,//点赞数
          comment: undefined,//评论数
          address: undefined,//创建位置
          visible: undefined,//1、公开 2、保密
          source: undefined,//来源： 1、'后台创建' 2、'app用户'
        },
        formC: {
          title: undefined,//标题
          desc:undefined,//描述
          share: undefined,//分享次数
          praise:undefined,//点赞数
          comment: undefined,//评论数
          address: undefined,//创建位置
          addressStr:'',
          visible: '1',//1、公开 2、保密
          source: "1",//来源： 1、'后台创建' 2、'app用户'
          listPicUrl:'',//图片
        },
        fileList2: [],//图片
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          startTime: undefined,//开始时间
          endTime: undefined,//结束时间
          source: undefined,//来源
          praise:undefined,//点赞数
          comment: undefined,//评论数
          username: undefined,//用户名
          type: 2,// 消息类型 1=文字消息、2=图文消息（拍拍）、3=语音消息、4=视频消息、 5=文件消息 、 6=SDK分享消息
        },
        inline: true,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
      }
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters([
        'elements',
        'belong2Type'
      ]),
      getHeaderWithToken() {
        return {Authorization: getToken()};
      }
    },
    methods: {
      /**
       * 关闭关键标签配置
       * */
      cancelHandel() {
        this.oneTypeDialogVisible = false;
      },
      /**
       * 编辑创建地址
       * */
      selectDestination() {
        let that = this;
        this.addressObj.eventName = 'addressEvent';
        this.addressObj.keyword = this.addressObj.address?this.addressObj.address:this.addressObj.addressStr?this.addressObj.addressStr:'';
        this.showAddressVisible = true;
      },

      /**
       * 关闭创建弹框
       * */
      cancelCreateHandel(){
        this.createDialogVisible = false;
      },
      /**
       * 关闭地址弹框
       * */
      addressEvent(obj) {
        this.showAddressVisible = false;
        if (obj) {
          this.addressObj = obj;
          let provinceObj = getRegionByCodes([obj.province]);
          let cityObj = getRegionByCodes([obj.city]);
          this.addressObj.provinceName = provinceObj[0].name;
          this.addressObj.cityName = cityObj[0].name;
          this.formC.addressStr = this.addressObj.provinceName + this.addressObj.cityName + this.addressObj.address;
          console.log(this.addressObj);
        }
      },
      /**
       * 创建拍拍消息
       * */
      createMsgs(){
        this.createDialogVisible = false;
//        let params = {
//          id: this.formE.msgId,
//          visible: this.formE.visible,//1、公开 2、保密
//          source: this.formE.source,//来源： 1、'后台创建' 2、'app用户'
//        };
        console.log(this.formC);
        console.log(this.addressObj);
//        updateMsg(params).then(response => {
//          if (response.status === 200) {
//            this.$notify({
//              title: '成功',
//              message: '成功',
//              type: 'success',
//              duration: 2000
//            });
//            this.oneTypeDialogVisible = false;
//            this.getList();
//          } else {
//            this.$notify({
//              title: '失败',
//              message: '操作失败！',
//              type: 'error',
//              duration: 2000
//            });
//          }
//        });
      },

      /**
       * 保存编辑
       * */
      updateMsgs(){
        let params = {
          id: this.formE.msgId,
          visible: this.formE.visible,//1、公开 2、保密
          source: this.formE.source,//来源： 1、'后台创建' 2、'app用户'
        };
        console.log(params);
        updateMsg(params).then(response => {
          if (response.status === 200) {
            this.$notify({
              title: '成功',
              message: '成功',
              type: 'success',
              duration: 2000
            });
            this.oneTypeDialogVisible = false;
            this.getList();
          } else {
            this.$notify({
              title: '失败',
              message: '操作失败！',
              type: 'error',
              duration: 2000
            });
          }
        });
      },
      /**
       * 添加
       * */
      handleCreate() {
        this.modalTitle = '创建';
        this.formC = {
          title: undefined,//标题
          desc:undefined,//描述
          share: undefined,//分享次数
          praise:undefined,//点赞数
          comment: undefined,//评论数
          address: undefined,//创建位置
          listPicUrl:'',//图片
          visible: '1',//1、公开 2、保密
          source: "1",//来源： 1、'后台创建' 2、'app用户'
        };
        this.createDialogVisible = true;
      },
      /**
       * 查看图片
       * */
      handleImg(imgList){
        this.imgList = imgList;
        this.imgDialogVisible = true;
      },
      /**
       * 删除图片
       * */
      handleRemove(file, fileList) {
        let list = [];
        fileList.map(function (item) {
          if (item.url != file.url) {
            list.push(item);
          }
        });
        this.fileList2 = list;
        let arr = [];
        let listPicUrl = this.form.listPicUrl.split(",");
        listPicUrl.map(function (item) {
          if (item != file.url) {
            arr.push(item);
          }
        });
        this.formC.listPicUrl = arr.join(",");
      },
      /**
       * 图片上传
       * */
      handleBannerSuccess(res, file) {
        this.fileList2.push({url: res.data});
        let c = [];
        this.fileList2.map(function (i) {
          c.push(i.url);
        });
        this.formC.listPicUrl = c.join(",");
      },
      /**
       * 公开、秘密  参数 visible 1：公开，2：私密
       */
      upStatus(row){
        let params = {
          id: row.msgId,
          visible: row.visible==1?2:1
        };
        updateMsg(params).then(response => {
          if (response.status === 200) {
            this.$notify({
              title: '成功',
              message: '成功',
              type: 'success',
              duration: 2000
            });
            this.getList();
          } else {
            this.$notify({
              title: '失败',
              message: '操作失败！',
              type: 'error',
              duration: 2000
            });
          }
        });
      },
      /**
       * 删除
       * */
      deleteHandler(row) {
        this.$confirm('确定删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delMsg({id: row.msgId}).then(response => {
            if (response.status === 200) {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              this.bannerDialogVisible = false;
              this.getList();
            } else {
              this.$notify({
                title: '删除失败',
                message: rsCode.msg[response.code] ? rsCode.msg[response.code] : '操作失败！',
                type: 'error',
                duration: 2000
              });
            }
          });
        })
      },
      /**
       * 编辑
       * */
      handleUpdate(row) {
        this.modalTitle = '编辑';
        console.log(row);
        this.formE = {
          msgId: row.msgId,//消息id
          title: row.body.title?row.body.title:'',//标题
          images: row.body.images?row.body.images:{},//图片
          desc:row.body.text,//描述
          crtTime: timestamp2Date(row.body.time),//创建时间
          userId: row.userId,
          share: row.count.share,//分享次数
          praise: row.count.praise,//点赞数
          comment: row.count.comment,//评论数
          address: row.body.address,//创建位置
          visible: row.visible+'',//1、公开 2、保密
          source: row.source+''
        };
        this.oneTypeDialogVisible = true;
      },
      /**
       * 详情modal传递回来的数据
       * */
      oneDialogEvent(e){
        this.oneTypeDialogVisible = false;
        if(e){
          //编辑成功-重新加载列表
          this.getList();
        }
      },
      /**
       * 获取拍拍列表
       * */
      getList() {
        this.listLoading = true;
        let params = objDeepCopy(this.listQuery);
        params.comment  = this.listQuery.comment? this.listQuery.comment:undefined;
        params.praise = this.listQuery.praise?this.listQuery.praise:undefined;

        if(this.listQuery.startTime  && (typeof this.listQuery.startTime == "object")){
          params.startTime = this.listQuery.startTime.getTime()/1000;
        }
        if(this.listQuery.endTime  && (typeof this.listQuery.endTime == "object")){
          params.endTime = this.listQuery.endTime.getTime()/1000;
        }
        page(params).then(response => {
          let totalCountRs = undefined;
          let listRs = undefined;
          if (!this.$utils.isEmpty(response.data.list) && this.$utils.isInteger(response.data.total)) {
            response.data.list.map(function(item){

            });
            listRs = response.data.list;
            totalCountRs = response.data.total;
          }
          this.listLoading = false;
          this.list = listRs;
          this.total = totalCountRs;
        })
      },
     /**
      * 搜索
      * */
      handleFilter() {
        this.listQuery.page = 1;
        this.$refs.queryForm.validate(valid => {
          if (valid) {
            this.getList();
          } else {
            return false;
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      /**
       * 重置
       * */
      reloadPage() {
        this.listQuery = {
          page: 1,
          limit: 20,
          startTime: undefined,//开始时间
          endTime: undefined,//结束时间
          source: undefined,//来源
          startTime: undefined,//创建时间
          praise:undefined,//点赞数
          comment: undefined,//评论数
          username: undefined,//用户名
          type: 2,//消息类型
        };
        this.getList()
      },
    }
  }
</script>
