<template>
  <div class="app-container">
    <search-form
      :list-loading="listLoading"
      @change="searchHandle"
    />
    <table-list
      :operationsDefaultLength="4"
      :list-loading="listLoading"
      :data="tableData2"
      :columns="listConfig"
      :current-page="currentPage"
      :page-size="pageSize"
      :total-records="totalRecords"
      @currentChange="pageHandle"
      @sizeChange="sizeChange"
      :insertSlotArr="[4]"
    >
      <div slot="OI4">
        <el-table-column
          width="50"
          label="链接"
          align="center"
        >
          <template slot-scope="scope">
            <a v-if="scope.row.news_type === 'adminadd'" :href="'https://h5.honglounews.com/wxnews.html?id=' + scope.row.id" target="_blank" class="scoped-link">链接</a>
            <a v-else :href="scope.row.link" target="_blank" class="scoped-link">链接</a>
          </template>
        </el-table-column>
      </div>
      <div slot="otherItem">
        <el-button type="small" icon="el-icon-plus" class="xl-form-btn bgc1" @click="openPopup">添加公众号文章</el-button>
        <el-button type="small" icon="el-icon-plus" class="xl-form-btn bgc2" @click="openIESPopup">添加新资讯</el-button>
      </div>
    </table-list>
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
    <index-edit-self
      :isShow="isIESShow"
      :curObj="curObj"
      @close="closeIESPopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Index'
import PopupEdit from './components/popup/IndexEdit'
import IndexEditSelf from './components/popup/IndexEditSelf'
import baseTable from '_m/baseTable.js'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    IndexEditSelf,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'house.adminformationlist',
      searchForm: null,
      isDtlShow: false,
      isIESShow: false,
      curObj: {}
    }
  },
  computed: {
    tableData2() {
      const defaultImg = require('@/assets/ex_test.jpg')
      const arr = [...this.tableData]
      arr.map(item => {
        // item.pri_image = this.IMadd(item.pri_image)
        if (item.link) {
          item.linkStr = `${item.link.length}---${item.link.substring(item.link.length-16, item.link.length-1)}`
        }
        item.hideStatus = Number(item.hide_status)
        item.selectStatus = Number(item.select_status)
        if (Number(item.select_status) === 1) item.noDel = true
        if (Number(item.select_status) !== 1) item.recommend = true
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '浏览数', prop: 'view_count' },
        { label: '标题', prop: 'title', fullShow: true, minWidth: 200 }, 
        { label: '分类', prop: 'information_category', type: 'flag', flags: arrToObj(this.$dictData.information_category) }, 
        { label: '主图', prop: 'pri_image', type: 'img' },
        { label: '链接Str', prop: 'linkStr', fullShow: true },
        { label: '作者', prop: 'author' },
        { label: '状态', prop: 'hideStatus', type: 'tag', tags: arrToObj(this.$dictData.hide_status), tagTypeObj: {'1': 'success', '2': 'danger'} }, 
        { label: '精选', prop: 'selectStatus', type: 'tag', tags: arrToObj(this.$dictData.select_status), tagTypeObj: {'1': 'success', '2': 'danger'} }, 
        { label: '创建时间', prop: 'create_at' },
        { label: '更新人', prop: 'update_by' },
        { label: '更新时间', prop: 'update_at' },
        { label: '操作', width: 130, type: 'handle2', operations:
          [
            // { labelFor: 'selectStatus', func: this.recommendHandle, hide: 'recommend',
            //   labelConfig: {
            //     texts: {
            //       1: '不推',
            //       2: '推荐'
            //     },
            //     btnTypes: {
            //       1: 'danger',
            //       2: 'success'
            //     }
            //   }
            // },
            { label: '推首页', func: this.recommendHandle, btnType: 'success', hide: 'recommend' },
            { label: '编辑', func: this.openPopup, btnType: 'primary' },
            { label: '删除', func: this.delHandle, btnType: 'danger', hide: 'noDel' },
          ]
        }
      ]
    }
  },
  methods: {
    recommendHandle (row) {
      let flag = 2
      const apiStr = flag=== 1 ? 'adminformationrefineddel' : 'adminformationrefinedadd'
      const msgText = flag=== 1 ? '取消推首页' : '推首页'
      this.$msg(`确定该文章要${msgText}吗？`, 'confirm', ()=> {
        this.$api.house[apiStr]({
          id: row.id,
        }).then(data => {
          this.$msgs(`${msgText}成功！`)
          this.fetchData()
        })
      }, null, true)
    },
    delHandle(row) {
      this.$msg(`您确定要删除该文章吗？`, 'confirm', () => {
        this.$api.house.adminformationdel({
          id: row.id,
        }).then(data => {
          this.$msgs(`已删除！`)
          this.fetchData()
        })
      }, null, true)
    },
    openPopup(row) {
      if (row && row.news_type === 'adminadd') {
        this.openIESPopup(row)
      } else {
        if (row && row.id) {
          this.curObj = row
        } else {
          this.curObj = {}
        }
        this.isDtlShow = true
      }
    },
    closePopup(obj) {
      this.isDtlShow = false
      if (obj) {
        this.fetchData()
      }
    },
    openIESPopup(row) {
      // if (row && row.id) {
      //   this.curObj = row
      // } else {
      //   this.curObj = {}
      // }
      // this.isIESShow = true
      if (row && row.id) {
        this.$router.push(`/news/indexEditSelf?id=${row.id}`)
      } else {
        this.$router.push(`/news/indexEditSelf`)
      }
    },
    closeIESPopup(obj) {
      this.isIESShow = false
      if (obj) {
        this.fetchData()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
.scoped-link {
  color: #2d8cf0;
  text-decoration: underline;
}
</style>

