<template>
  <div class="mlt">
    <div>
      <el-form :inline="true" size="mini">
        <el-row>
          <el-form-item>
            <el-input v-model="tag" style="width: 250px" placeholder="请输入景点名称" class="search_tag" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="btn_search()"><i class="el-icon-search"/> 查询</el-button>
            <el-button @click="btn_clear()"><i class="el-icon-remove-outline"/> 重置</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="addPlace">添加</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        border>
        <el-table-column
          prop="id"
          label="编号"/>
        <el-table-column
          prop="name"
          label="景点名称"/>
        <el-table-column
          prop="placeType.typeName"
          label="景点类型"/>
        <el-table-column
          prop="address"
          label="景点地址"/>
        <el-table-column
          prop="detail"
          label="景点详情"/>
        <el-table-column
          prop="logo"
          label="景点logo">
          <template slot-scope="scope">
            <img :src="scope.row.logo" min-width="40" height="40">
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="editPlace(scope.row, 1)">编辑</el-button>
            <el-button type="text" @click="deletePlaceConfirm(scope.row)">删除</el-button>
            <el-button type="text" @click="editPlace(scope.row, 2)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mydiv">
        <el-pagination
          :current-page.sync="pageNo"
          :page-sizes="[10, 50, 100, 300,1000]"
          :page-size="pageSize"
          :total="totalDataNumber"
          layout="sizes,total, prev, pager, next"
          class="table-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>

    </div>
    <!--弹出框-->
    <el-dialog :visible.sync="dialogFormVisiable" title="添加景点">
      <el-form :model="addForm" :rules="rules" :label-width="formLabelWidth" >
        <el-form-item label="景点名称" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入景点名称" size="mini"/>
        </el-form-item>
        <el-form-item label="景点logo" >
          <el-upload
            :action="uploadUrl"
            :headers="uploadHeaders"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="logoList"
            list-type="picture-card">
            <i class="el-icon-plus"/>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="景点类型" prop="placeType">
          <el-select v-model="addForm.placeTypeId" placeholder="请选择景点类型">
            <el-option
              v-for="item in placeTypes"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="景点地址" prop="address">
          <el-input v-model="addForm.address" placeholder="请输入景点地址" size="mini"/>
        </el-form-item>
        <el-form-item label="开放时间">
          <el-input v-model="addForm.openTime" placeholder="请输入开放时间" size="mini"/>
        </el-form-item>
        <el-form-item label="交通信息">
          <el-input v-model="addForm.trafficInfo" placeholder="请输入交通信息" size="mini"/>
        </el-form-item>
        <el-form-item label="景点详情">
          <el-input
            v-model="addForm.detail"
            :rows="5"
            placeholder="请输入景点详情"
            size="mini"
            type="textarea"/>
        </el-form-item>
        <el-form-item label="景点图集" >
          <el-upload
            :action="uploadUrl"
            :headers="uploadHeaders"
            :on-remove="handleRemove2"
            :on-success="handleSuccess2"
            :limit="5"
            :on-exceed="handleExceed2"
            :file-list="photoList"
            list-type="picture-card">
            <i class="el-icon-plus"/>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addPlaceCancel">取 消</el-button>
        <el-button size="mini" type="primary" @click="addPlaceSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogModifyVisiable" title="修改景点" @close="closeDialog">
      <el-form :model="modifyForm" :rules="rules" :label-width="formLabelWidth">
        <el-form-item label="景点名称" prop="name">
          <el-input v-model="modifyForm.name" placeholder="请输入景点名称" size="mini"/>
        </el-form-item>
        <el-form-item label="景点logo" >
          <el-upload
            :disabled="showDetail"
            :action="uploadUrl"
            :headers="uploadHeaders"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="logoList"
            list-type="picture-card">
            <i class="el-icon-plus"/>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="景点类型" prop="placeType">
          <el-select :disabled="showDetail" v-model="modifyForm.placeType.id" placeholder="请选择景点类型">
            <el-option
              v-for="item in placeTypes"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="景点地址" prop="address">
          <el-input :disabled="showDetail" v-model="modifyForm.address" placeholder="请输入景点地址" size="mini"/>
        </el-form-item>
        <el-form-item label="开放时间">
          <el-input :disabled="showDetail" v-model="modifyForm.openTime" placeholder="请输入开放时间" size="mini"/>
        </el-form-item>
        <el-form-item label="交通信息">
          <el-input :disabled="showDetail" v-model="modifyForm.trafficInfo" placeholder="请输入交通信息" size="mini"/>
        </el-form-item>
        <el-form-item label="景点详情">
          <el-input
            v-model="modifyForm.detail"
            :disabled="showDetail"
            :rows="5"
            placeholder="请输入景点详情"
            size="mini"
            type="textarea"/>
        </el-form-item>
        <el-form-item label="景点图集" >
          <el-upload
            :disabled="showDetail"
            :action="uploadUrl"
            :headers="uploadHeaders"
            :on-remove="handleRemove2"
            :on-success="handleSuccess2"
            :limit="5"
            :on-exceed="handleExceed2"
            :file-list="photoList"
            list-type="picture-card">
            <i class="el-icon-plus"/>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="updatePlaceCancel">取 消</el-button>
        <el-button size="mini" type="primary" @click="updatePlace">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 点击删除按钮，删除确认框-->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="提示"
      width="30%">
      <span>确定删除选择的景点? </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleClickDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import { getToken } from '@/utils/auth'
export default {
  name: 'Place',
  data() {
    return {
      rules: {
        name: [{ required: true, message: '请输入景点名称', trigger: 'blur' }],
        placeType: [{ required: true, message: '请选择景点类型', trigger: 'blur' }],
        address: [{ required: true, message: '请输入景点地址', trigger: 'blur' }]
      },
      tag: '',
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      formLabelWidth: '150px', // 弹出框form宽
      dialogFormVisiable: false,
      dialogModifyVisiable: false,
      deleteDialogVisible: false,
      placeTypes: [],
      placeItems: [],
      addForm: {
        name: '',
        placeTypeId: '',
        address: '',
        detail: '',
        photos: '',
        logo: '',
        openTime: '',
        trafficInfo: ''
      },
      modifyForm: {
        id: '',
        name: '',
        placeType: '',
        address: '',
        detail: '',
        photos: '',
        logo: '',
        openTime: '',
        trafficInfo: ''
      },
      deleteId: '',
      logoList: [],
      photoList: [],
      uploadUrl: process.env.UPLOAD_URL,
      uploadHeaders: {
        token: getToken()
      },
      showDetail: false
    }
  },
  created() {
    this.searchAllPlaceTypes()
    this.btn_search()
  },
  methods: {
    searchAllPlaceTypes() {
      const vm = this
      axios.get(process.env.BASE_API + '/placetype/list', {
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.placeTypes = []
        } else {
          if (parseInt(obj.total) > 0) {
            vm.placeTypes = obj.data
          } else {
            vm.placeTypes = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_search() {
      const vm = this
      axios.get(process.env.BASE_API + '/place/list', {
        params: {
          tag: this.tag
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalDataNumber = 0
          vm.allData = []
          vm.tableData = []
        } else {
          vm.totalDataNumber = parseInt(obj.total)
          if (vm.totalDataNumber > 0) {
            vm.allData = obj.data
            vm.tableData = obj.data.slice(0, vm.pageSize)
          } else {
            vm.allData = []
            vm.tableData = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_clear() {
      this.tag = ''
      this.pageNo = 1
      this.btn_search()
    },
    handleCurrentChange() {
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    addPlace() {
      this.dialogFormVisiable = true
    },
    addPlaceCancel() {
      this.dialogFormVisiable = false
      this.clearForm()
    },
    clearForm() {
      this.addForm.name = ''
      this.addForm.placeTypeId = ''
      this.addForm.detail = ''
      this.addForm.address = ''
      this.modifyForm.id = ''
      this.modifyForm.name = ''
      this.modifyForm.placeTypeId = ''
      this.modifyForm.detail = ''
      this.modifyForm.address = ''
      this.logoList = []
      this.photoList = []
    },
    addPlaceSubmit() {
      const vm = this
      var params = new URLSearchParams()
      if (vm.addForm.name === '') {
        this.$message.error('请输入景点名称')
        return
      }
      if (vm.addForm.placeTypeId === '') {
        this.$message.error('请选择景点类型')
        return
      }
      if (vm.addForm.address === '') {
        this.$message.error('请输入景点地址')
        return
      }
      params.append('name', vm.addForm.name)
      params.append('placeType', vm.addForm.placeTypeId)
      params.append('address', vm.addForm.address)
      params.append('openTime', vm.addForm.openTime)
      params.append('trafficInfo', vm.addForm.trafficInfo)
      params.append('detail', vm.addForm.detail)
      if (vm.logoList !== undefined && vm.logoList.length > 0) {
        params.append('logo', vm.logoList[0].url)
      }
      if (vm.photoList !== undefined && vm.photoList.length > 0) {
        for (let i = 0; i < vm.photoList.length; i++) {
          vm.addForm.photos += vm.photoList[i].url + ','
        }
        params.append('photos', vm.addForm.photos)
      }
      axios.post(process.env.BASE_API + '/place/create', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.pageNo = 1
          vm.clearForm()
          vm.btn_search()
        }
      }).catch(function(error) {
        alert(error)
      })
      this.dialogFormVisiable = false
    },
    editPlace(row, value) {
      this.dialogModifyVisiable = true
      this.modifyForm = row
      if (value === 2) {
        this.showDetail = true
      }
      if (row.logo !== undefined) {
        this.logoList.push({
          url: row.logo
        })
      }
      if (row.photos !== undefined && row.photos !== null) {
        var strs = row.photos.split(',')
        for (var i = 0; i < strs.length - 1; i++) {
          this.photoList.push({
            url: strs[i]
          })
        }
      }
    },
    updatePlaceCancel() {
      this.dialogModifyVisiable = false
      this.clearForm()
      this.btn_search()
    },
    updatePlace() {
      const vm = this
      var params = new URLSearchParams()
      params.append('id', vm.modifyForm.id)
      params.append('name', vm.modifyForm.name)
      params.append('placeType', vm.modifyForm.placeType.id)
      params.append('address', vm.modifyForm.address)
      params.append('openTime', vm.modifyForm.openTime)
      params.append('trafficInfo', vm.modifyForm.trafficInfo)
      params.append('detail', vm.modifyForm.detail)
      if (vm.logoList !== undefined && vm.logoList.length > 0) {
        params.append('logo', vm.logoList[0].url)
      }
      if (vm.photoList !== undefined && vm.photoList.length > 0) {
        var strs = ''
        for (let i = 0; i < vm.photoList.length; i++) {
          strs = strs + vm.photoList[i].url + ','
        }
        params.append('photos', strs)
      }
      axios.post(process.env.BASE_API + '/place/update', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.pageNo = 1
          vm.btn_search()
        }
        vm.clearForm()
      }).catch(function(error) {
        alert(error)
      })
      this.dialogModifyVisiable = false
      this.showDetail = false
    },
    deletePlaceConfirm(row) {
      this.deleteId = row.id
      this.deleteDialogVisible = true
    },
    handleClickDelete() {
      const vm = this
      var params = new URLSearchParams()
      params.append('id', vm.deleteId)
      axios.post(process.env.BASE_API + '/place/delete', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.pageNo = 1
          vm.btn_search()
        }
      }).catch(function(error) {
        alert(error)
      })
      this.deleteDialogVisible = false
    },
    handleRemove(file, fileList) {
      this.logoList = fileList
    },
    handleSuccess(res, file, fileList) {
      if (res.code === 10000) {
        this.logoList.push({
          url: res.imageUrl
        })
      } else {
        this.$message.error(res.message)
        fileList.splice(fileList.indexOf(file), 1)
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    handleRemove2(file, fileList) {
      this.photoList = fileList
    },
    handleSuccess2(res, file, fileList) {
      if (res.code === 10000) {
        this.photoList.push({
          url: res.imageUrl
        })
      } else {
        this.$message.error(res.message)
        fileList.splice(fileList.indexOf(file), 1)
      }
    },
    handleExceed2(files, fileList) {
      this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    closeDialog() {
      this.logoList = []
      this.photoList = []
      this.showDetail = false
    }
  }
}
</script>

<style scoped>
  .mlt {
    margin-left: 20px;
    margin-top: 20px;
    margin-right: 20px;
  }
</style>
