<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="getTypes">查询</Button>
            <Button type="primary" style="margin-left: 10px" @click="$refs['addStrategyType'].type = 'add',$refs['addStrategyType'].isShow = true">新增</Button>
          </div>
        </slot>
      </lx-header>
    </div>

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

    <addStrategyType ref="addStrategyType" @refresh="getTypes"></addStrategyType>
  </div>
</template>

<script>
import {index,destroy} from "@/api/strategyType"
import {parseTime} from "@/utils"

import addStrategyType from '@/views/productService/components/strategy/addStrategyType'
import { Message } from 'element-ui'
export default {
  components:{
    addStrategyType
  },
  data() {
    return {
      select:{
        pageIndex:1,
        pageSize:10,
        name:''
      },

      total:0,
      list:[],
      table:[
        {
          prop:'name',
          label:'名称',
          minWidth:220
        },
        {
          prop:'call_logo',
          label:'调用标识',
          width:180
        },
        {
          label:'状态',
          width: 180,
          customFn:(row) => {
            return (
              <div style={{'color': row.state ? 'green' : 'red'}}>{row.state ? '启用' : '未启用'}</div>
            )
          }
        },
        {
          label:'排序',
          width: 180,
          prop:'sort_number'
        },
        {
          label:'创建时间',
          width: 220,
          prop:'created_at',
          formatter:(cell,data,value)=>{
            return parseTime(new Date(value),'{y}-{m}-{d}')
          }
        },
        {
          label:'创建人',
          width:160,
          prop:'admin.name'
        }
      ],
    }
  },
  methods: {
    async getTypes(){
      const res = await index({
        page:this.select.pageIndex,
        page_size:this.select.pageSize,
        name:this.select.name
      })
      this.list = res.rows
      this.total = res.total
    },

    pageChange(e){
      this.select.pageSize = e
      this.getTypes()
    },

    editor(row){
      this.$refs['addStrategyType'].detail = row
      this.$refs['addStrategyType'].id = row.id
      this.$refs['addStrategyType'].type = 'editor'
      this.$refs['addStrategyType'].isShow = true
    },
    deleteType(row){
      destroy({
        id:row.id
      }).then(res => {
        Message({
          type:'success',
          message:'删除攻略类型成功'
        })
        this.getTypes()
      })
    }
  },
  mounted() {
    this.getTypes()
  }

}
</script>

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