<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 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['addConfinementClub'].type = 'add',$refs['addConfinementClub'].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">
            <Poptip trigger="hover" transfer placement="bottom">
              <Button ghost size="small" type="primary" style="margin: 0 6px 4px 0">编辑</Button>

              <div slot="content" style="display:flex;flex-direction: column">
                <Button  size="small" type="primary" @click="editorClub(scope.row)" style="margin: 0 6px 4px 0">信息编辑</Button>
                <Button  size="small" type="primary" @click="showInfo(scope.row)" style="margin: 0 6px 4px 0;">商户简介</Button>
                <Button  size="small" type="primary" @click="showPic(scope.row)" style="margin: 0 6px 4px 0;">商户附件</Button>
              </div>
            </Poptip>

            <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>

    <addConfinementClub ref="addConfinementClub" @refresh="getClubs"></addConfinementClub>

    <addClubInfo ref="addClubInfo"></addClubInfo>

    <addClubPic ref="addClubPic"></addClubPic>
  </div>
</template>

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

import addConfinementClub from '@/views/productService/components/confinement/addConfinementClub'
import addClubInfo from '@/views/productService/components/confinement/addClubInfo'
import addClubPic from '@/views/productService/components/confinement/addClubPic'
export default {
  components:{
    addConfinementClub,
    addClubInfo,
    addClubPic
  },
  data() {
    return {
      select:{
        page:1,
        page_size:10,
        keywords:'',
        type:''
      },

      total:0,
      list:[],
      table:[
        {
          prop:'name',
          label:'名称',
          minWidth:220,
          align:'left'
        },
        {
          prop:'reach',
          label:'是否到店',
          width: 160,
          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: 120,
          prop:'sort_number'
        },
        {
          label:'提交人',
          prop:'admin.name',
          width:180
        },
        {
          label:'提交日期',
          prop:'created_at',
          width: 200,
          formatter:(cell,data,value)=>{
            return parseTime(new Date(value),'{y}-{m}-{d}')
          }
        }
      ],

    }
  },
  methods: {
    async getClubs(){
      const res = await index({
        page:this.select.page,
        page_size:this.select.page_size,
        keyword:this.select.keywords
      })
      console.log(res)
      this.list = res.rows
      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['addConfinementClub'].type = 'editor'
      this.$refs['addConfinementClub'].id = row.id
      this.$refs['addConfinementClub'].isShow = true
    },
    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()
  }
}
</script>

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