<template>
  <div style="padding: 0 20px;">
    <div ref="lxHeader">
      <lx-header icon="md-apps" text="攻略信息管理" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
        <div slot="content"></div>
        <slot>
          <div>
            <Input style="width: 200px; margin-right: 10px" v-model="select.keywords" placeholder="关键字搜索" />
            <Button type="primary" @click="getStrategies">查询</Button>
            <Button type="primary" style="margin-left: 10px" @click="$refs['addStrategy'].type = 'add',$refs['addStrategy'].isShow = true">新增</Button>
          </div>
        </slot>
      </lx-header>
    </div>

    <xy-table
      :total="total"
      :list="list"
      :table-item="table"
      @pageSizeChange="e => select.pageSize = e"
      @pageIndexChange="pageChange"
      @editor="editorClick"
      @delete="deleteStrategy"></xy-table>

    <addStrategy ref="addStrategy" @refresh="getStrategies"></addStrategy>
  </div>
</template>

<script>
import {index,destroy} from '@/api/strategy'
import {parseTime,deepCopy} from '@/utils'

import addStrategy from './components/strategy/addStrategy'
import { Message } from 'element-ui'
export default {
  components:{
    addStrategy
  },
  data() {
    return {
      select:{
        pageIndex:1,
        pageSize:10,
        keywords:'',
      },

      total:0,
      list:[],
      table:[
        {
          label:'攻略类型',
          prop:'strategy_type.name',
          width:160
        },
        {
          label:'标题',
          prop:'title',
          align:'left',
          minWidth: 240
        },
        {
          label:'副标题',
          prop:'subheading',
          align:'left',
          minWidth: 200
        },
        {
          label:'状态',
          width: 120,
          customFn:(row)=>{
            return (<div style={{'color':row.state == 1 ? 'green' : 'red'}}>{row.state == 1 ? '启动' : '未启用'}</div>)
          }
        },
        {
          label:'创建时间',
          prop:'created_at',
          width:180,
          formatter:(cell,value,data,index)=>{
            return parseTime(new Date(data),'{y}-{m}-{d}')
          }
        }
      ]
    }
  },
  methods: {
    async getStrategies(){
      const res = await index({
        page:this.select.pageIndex,
        page_size:this.select.pageSize,
        keyword:this.select.keywords
      })
      this.total = res.total
      this.list = res.rows
      console.log(this.list)
    },

    pageChange(e){
      this.select.pageIndex = e
    },

    editorClick(row){
      this.$refs['addStrategy'].detail = deepCopy(row)
      this.$refs['addStrategy'].id = row.id
      this.$refs['addStrategy'].type = 'editor'
      this.$refs['addStrategy'].isShow = true
    },
    deleteStrategy(row){
      destroy({
        id:row.id
      }).then(res => {
        Message({
          type:'success',
          message:'删除攻略成功'
        })
        this.getStrategies()
      })
    }
  },
  mounted() {
    this.getStrategies()
  }

}
</script>

<style scoped lang="scss">
</style>
