<template>
  <div class="app-container">
    <search-form
      :list-loading="listLoading"
      @change="searchHandle"
    />
    <table-list
      :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/ncplan.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/Other'
import PopupEdit from './components/popup/OtherEdit'
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.admncplanlist',
      searchForm: null,
      isDtlShow: false,
      isIESShow: false,
      curObj: {}
    }
  },
  computed: {
    tableData2() {
      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)
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '浏览数', prop: 'view_count' },
        { label: '标题', prop: 'title', fullShow: true, minWidth: 200 }, 
        { label: '分类', prop: 'plan_type', type: 'flag', flags: arrToObj(this.$dictData.plan_type) }, 
        { 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) }, 
        { label: '创建时间', prop: 'create_at' },
        { label: '更新人', prop: 'update_by' },
        { label: '更新时间', prop: 'update_at' },
        { label: '操作', width: 120, type: 'handle2', operations:
          [
            { label: '编辑', func: this.openPopup, btnType: 'primary' },
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    delHandle(row) {
      this.$msg(`您确定要删除该文章吗？`, 'confirm', () => {
        this.$api.house.admncplandel({
          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/otherEdit?id=${row.id}`)
      } else {
        this.$router.push(`/news/otherEdit`)
      }
    },
    closeIESPopup(obj) {
      this.isIESShow = false
      if (obj) {
        this.fetchData()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
.scoped-link {
  color: #2d8cf0;
  text-decoration: underline;
}
</style>

