<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-__newshonest}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="新闻标题">
          <el-input v-model="dataForm.newsTitle" placeholder="新闻标题" @change="resetPage" clearable></el-input>
        </el-form-item>
        <el-form-item label="新闻模块"
                      prop="businessId">
          <el-select v-model="dataForm.businessId" @change="businessChange"
                     placeholder="新闻模块" clearable>
            <el-option v-for="item in options"
                       :key="item.code"
                       :label="item.title"
                       :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻类别"
                      prop="newsCateroryId">
          <el-select v-model="dataForm.newsCateroryId" :disabled="!dataForm.businessId"
                     placeholder="新闻类别" clearable @change="categoryChange">
            <el-option v-for="item in newsCateroryIds"
                       :key="item.id"
                       :label="item.categoryName"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻栏目"
                      prop="newsSubCateroryId">
          <el-select v-model="dataForm.newsSubCateroryId" :disabled="!dataForm.newsCateroryId"
                     placeholder="新闻栏目" @change="resetPage" clearable>
            <el-option v-for="item in newsSubCateroryIds"
                       :key="item.id"
                       :label="item.title"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态"
                      prop="newsApprovalState">
          <el-select v-model="dataForm.newsApprovalState"
                     placeholder="审核状态" @change="resetPage" clearable>
            <el-option v-for="item in approvalOptions"
                       :key="item.id"
                       :label="item.title"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column prop="newsTitle" label="新闻标题" header-align="center" width="400" align="center"></el-table-column>
        <el-table-column prop="newsProperty" label="新闻类别" header-align="center" align="center"></el-table-column>
        <el-table-column prop="newsColumn" label="新闻栏目" header-align="center" align="center"></el-table-column>
        <el-table-column prop="newsApprovalState" label="审核状态" sortable="custom" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.newsApprovalState === '0'" size="small" type="warn">未审核</el-tag>
            <el-tag v-if="scope.row.newsApprovalState === '1'" size="small" type="danger">驳回</el-tag>
            <el-tag v-if="scope.row.newsApprovalState === '2'" size="small" type="success">审核完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="newsUpDownState" label="上线状态" sortable="custom" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.newsUpDownState === '1'" size="small" type="danger">下线</el-tag>
            <el-tag v-else size="small" type="success">上线</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="readingAmount" label="阅读量" header-align="center" width="80" align="center"></el-table-column>
        <el-table-column prop="creatorName" label="创建人姓名" header-align="center" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.businessId === 'HONEST'">system</span>
            <span v-else >{{ scope.row.creatorName }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('honest:newshonest:review') && scope.row.newsApprovalState === '2' && scope.row.newsUpDownState === '1'" type="text" size="small" @click="UpStateChange(scope.row.id)">上线</el-button>
            <el-button v-if="$hasPermission('honest:newshonest:review') && scope.row.newsUpDownState === '0'" type="text" size="small" @click="DownStateChange(scope.row.id)">下线</el-button>
            <el-button v-if="$hasPermission('honest:newshonest:review') && scope.row.newsApprovalState === '0'" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">审核</el-button>
            <el-button v-if="($hasPermission('honest:newshonest:view') && scope.row.newsApprovalState === '2') || ($hasPermission('honest:newshonest:review') && scope.row.newsApprovalState !== '0')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './allnewshonest-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/news/newshonest/list',
        getDataListIsPage: true,
        deleteURL: '/news/newshonest',
        deleteIsBatch: true
      },
      dataForm: {
        id: '',
        newsUpDownState: ''
      },
      approvalOptions: [
        {
          id: '0',
          title: '未审核'
        },
        {
          id: '1',
          title: '驳回'
        },{
          id: '2',
          title: '审核完成'
        }
      ],
      newsCateroryIds: [],
      newsSubCateroryIds: [],
      options: []
    }
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    // 获取业务下拉菜单
    this.getBusinessOptions()
  },
  methods: {
    resetPage () {
      this.page = 1
    },
    businessChange (item) {
      this.resetPage()
      // 类别
      this.$http
          .get(`/news/newssection/options/` + item)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.loading = false
            this.newsCateroryIds = res.data
          })
          .catch(() => {
            this.loading = false
          })
    },
    categoryChange (item) {
      this.resetPage()
      // 类别
      this.$http
          .get(`/news/newssection/options/` + item)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.loading = false
            this.newsCateroryIds = res.data
          })
          .catch(() => {
            this.loading = false
          })
    },
    getBusinessOptions () {
      // 归属栏目
      this.$http
          .get(`/news/newsappmenu/options/0`)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.loading = false
            this.options = res.data
          })
          .catch(() => {
            this.loading = false
          })
    },
    DownStateChange (id) {
      this.$confirm(this.$t('prompt.info', { 'handle': '下线' }), this.$t('下线'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
      this.dataForm.id = id
      this.dataForm.newsUpDownState = 1
      this.$http['post']('/news/newshonest/review', this.dataForm).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshDataList')
            this.dataForm = {}
          }
        })
        this.getDataList()
      }).catch(() => { })
      })
    },
    UpStateChange (id) {
      this.$confirm(this.$t('prompt.info', { 'handle': '上线' }), this.$t('上线'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
      this.dataForm.id = id
      this.dataForm.newsUpDownState = 0
      this.$http['post']('/news/newshonest/review', this.dataForm).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshDataList')
            this.dataForm = {}
          }
        })
        this.getDataList()
      }).catch(() => { })
      })
    }
  }
}
</script>
