<template>
  <div style="padding: 0 20px">
    <div ref="lxHeader">
      <lx-header icon="md-apps" :text="pageTitle" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
        <div slot="content"></div>
        <slot>
          <div style="display: flex">
            <Input clearable v-model="select.keywords" style="width: 200px; margin-right: 10px" placeholder="关键字搜索" />


            <Button type="primary" @click="getClubs">查询</Button>
            <Button type="primary" style="margin-left: 10px" @click="$refs['addCommonService'].type = 'add',$refs['addCommonService'].isShow = true">新增</Button>
          </div>
        </slot>
      </lx-header>
    </div>

    <xy-table
      :total="total"
      :list="list"
      :table-item="table"
      @pageSizeChange="e => select.pageSize = e"
      @pageIndexChange="pageChange">
      <template v-slot:btns>
        <el-table-column fixed="right" label="操作" :width="130" align="left" header-align="center">
          <template slot-scope="scope">
                <Button  size="small" type="primary" @click="editorClub(scope.row)" style="margin: 0 6px 4px 0">编辑</Button>
            <Poptip confirm transfer title="确认要删除吗" @on-ok="clubDelete(scope.row)">
              <Button ghost size="small" type="primary" style="margin: 0 6px 4px 0">删除</Button>
            </Poptip>

          </template>
        </el-table-column>
      </template>
    </xy-table>

    <addCommonService ref="addCommonService" @refresh="getClubs"></addCommonService>
  </div>
</template>

<script>
import {index,destroy} from '@/api/productService/commonService'
import {parseTime} from '@/utils'
import { Message } from 'element-ui'

import addCommonService from '@/views/productService/components/common/addCommonService'
export default {
  components:{
    addCommonService
  },
  data() {
    return {
      pageTitle:"",
      select:{
        page:1,
        page_size:10,
        keywords:'',
        type:'maternity'
      },
      total:0,
      list:[],
      table:[
        {
          prop:'name',
          label:'名称',
          minWidth:220,
          align:'left'
        },
        {
          prop:'reach',
          label:'是否到店',
          width: 120,
          customFn:(row)=>{
            return (
              <div style={{'color': row.reach === 1 ? 'green' : 'red'}}>{row.reach === 1 ? '已到店' : '未到店'}</div>
            )
          }
        },
        {
          label:'特色标签',
          width: 260,
          align:'left',
          prop:'featured_label',
          customFn:(row) => {
            return (
              <div>
                {
                  row.featured_label ?
                  row.featured_label?.split(',').map(item => {
                    return <el-tag type="border" style={{'margin':'0 6px 4px 0'}}>{item}</el-tag>
                  }) :  <div style={{'text-align':'center'}}>无</div>
                }
              </div>
            )
          }
        },
        {
          label:'排序',
          width: 90,
          prop:'sort_number'
        },
        {
          label:'提交人',
          prop:'admin.name',
          width:180
        },
        {
          label:'提交日期',
          prop:'created_at',
          width: 140,
          formatter:(cell,data,value)=>{
            return parseTime(new Date(value),'{y}-{m}-{d}')
          }
        }
      ],

    }
  },
  methods: {
    async getClubs(){
      const res = await index(this.select)
      console.log(res)
      this.list = res.data
      this.total = res.total
    },
    pageChange(e){
      this.select.page = e
      this.getClubs()
    },

    clubDelete(row){
      destroy({
        id:row.id
      }).then(res => {
        Message({
          type:'success',
          message:'删除成功'
        })
        this.getClubs()
      })
    },
    editorClub(row){
      this.$refs['addCommonService'].type = 'editor'
      this.$refs['addCommonService'].id = row.id
      this.$refs['addCommonService'].isShow = true
      this.$refs['addCommonService'].form.type= this.select.type


    },
    showInfo(row){
      this.$refs['addClubInfo'].id = row.id
      this.$refs['addClubInfo'].isShow = true
    },
    showPic(row){
      this.$refs['addClubPic'].id = row.id
      this.$refs['addClubPic'].isShow = true
    }
  },
  mounted() {
    this.getClubs();

    this.pageTitle=this.$route.meta.title;
  }
}
</script>

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