<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item flex-h">
          <el-button size="mini" type="success" @click='addMajorProjects'>新增</el-button>
        </div>
        <ul>
          <li>
            <!-- <span>选择案场：</span> -->
            <el-select v-model="tableSearch.caseId" placeholder="请选择案场">
              <el-option
                key="all"
                label="所有案场"
                value="all">
              </el-option>
              <el-option
                v-for="item in caseList"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </li>
        </ul>
        <tableSearch value="请输入项目专题名称" @exportSearchKey="searchList"></tableSearch>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table
        :data="tableData"
        stripe
        style="width: 100%">
        <el-table-column
          prop="Name"
          label="名称">
        </el-table-column>
        <el-table-column
          prop="Title"
          label="标题">
        </el-table-column>
        <el-table-column
          prop="CaseName"
          label="对应案场">
        </el-table-column>
        <el-table-column
          prop="CreateDate"
          label="创建时间">
        </el-table-column>
        <el-table-column label="操作" width="450">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="showItem(scope.$index, scope.row)"><span v-if="scope.row.Status === 1">取消前台展示</span><span v-if="scope.row.Status === 0">前台展示</span></el-button>
            <el-button
              size="mini"
              type="warning"
              v-if="scope.row.Status === 0"
              @click="editItem(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="success"
              v-clipboard:copy="returnUrl(scope.row.CmsCaseId)"
              v-clipboard:success="onCopy"
              v-clipboard:error="onError"
              >点击复制链接</el-button>
            <el-button
              size="mini"
              type="danger"
              v-if="scope.row.Status === 0"
              @click="deleteItem(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="postData.currentPage"
      :page-size="postData.pageSize"
      layout="prev, pager, next, jumper"
      :total="postData.total">
    </el-pagination>
  </div>
</template>

<script>
import tableSearch from '@/components/tableSearch/index'
import { mapState, mapActions } from 'vuex'

export default {
  name: '',
  data () {
    return {
      postData: {
        currentPage: 1, // 当前页码
        pageSize: 10,
        total: 0,
      },
      tableSearch: { // 表格搜索条件
        key: '', // 搜索关键字
        caseId: '', // 案场id
      },
      tableData: []
    }
  },
  computed: {
    ...mapState({
      OrgId: x => x.app.user.OrgId,
      caseList: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      clientUrl: x => x.app.clientUrl,
    })
  },
  components: {
    tableSearch,
  },
  created () {
    this.updateSystemInfo().then(() => {
      // this.tableSearch.caseId = this.defaultCaseId
      this.getList()
    })
  },
  methods: {
    ...mapActions(['updateSystemInfo']),
    handleCurrentChange (val) {
      this.postData.currentPage = val
      this.getList()
    },
    showItem (index, data) { // 前台显示
      let api = data.Status === 1 ? this.$api.cms.caseHide : this.$api.cms.caseShow
      this.$ajax(api.url, {
        method: api.method,
        urlData: {
          id: data.CmsCaseId
        }
      }).then(res => {
        this.$message({
          message: '编辑成功',
          type: 'success',
          duration: 1000
        })
        this.getList()
      }).catch(msg => {

      })
    },
    editItem (index, row) { // 编辑
      this.$router.push({ name: 'editIndexCase', query: { id: row.CmsCaseId } })
    },
    returnUrl (id) {
      return this.clientUrl + 'user.html#/majorProjects?id=' + id
    },
    deleteItem (index, row) { // 删除
      this.$confirm('确认删除此项目专题?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteInfo(row.CmsCaseId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchList (key) { // 搜索列表
      this.tableSearch.key = key
      this.getList()
    },
    addMajorProjects () {
      this.$router.push({ name: 'addIndexCase' })
    },
    getList () {
      this.tableData = []
      this.$ajax(this.$api.cms.case.url, {
        method: this.$api.cms.case.method,
        queryData: {
          page: this.postData.currentPage,
          pagesize: this.postData.pageSize,
          caseid: this.tableSearch.caseId === 'all' ? '' : this.tableSearch.caseId,
          name: this.tableSearch.key
        }
      }).then(res => {
        for (let i = 0; i < res.list.length; i++) {
          res.list[i].CreateDate = this.toolClass.dateFormat(res.list[i].CreateDate)
        }
        this.tableData = res.list
        this.postData.total = res.pagenum
        this.postData.currentPage = res.page
      }).catch(msg => {

      })
    },
    deleteInfo (id) {
      this.$ajax(this.$api.cms.deleteCase.url, {
        method: this.$api.cms.deleteCase.method,
        urlData: {
          id: id
        }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(msg => {

      })
    },
    onCopy () {
      this.$message({
        type: 'success',
        message: '复制成功！'
      })
    },
    onError () {
      this.$message({
        type: 'error',
        message: '复制失败！'
      })
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
