<template>
  <ai-list class="app-archives_list">
    <template slot="title">
      <ai-title title="门店档案" isShowBottomBorder>
      </ai-title>
    </template>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-input placeholder="请输入门店名称" size="small" clearable></el-input>
          <ai-select
              v-model="search.applyItemId"
              @change="(search.current = 1), getList()"
              placeholder="是否户主"
              :selectList="dictList">
          </ai-select>
          <ai-select
              v-model="search.applyItemId"
              @change="(search.current = 1), getList()"
              placeholder="门店等级"
              :selectList="dictList">
          </ai-select>
          <el-date-picker
              v-model="search.createTimeStart"
              type="date"
              size="small"
              value-format="yyyy-MM-dd"
              placeholder="选择开始日期"
              @change="search.current = 1, getList()">
          </el-date-picker>
          <el-date-picker
              v-model="search.createTimeEnd"
              type="date"
              size="small"
              value-format="yyyy-MM-dd"
              placeholder="选择结束日期"
              @change="search.current = 1, getList()">
          </el-date-picker>
          <ai-select
              v-model="search.status"
              @change="(search.current = 1), getList()"
              placeholder="经营类型"
              :selectList="$dict.getDict('appIntegralApplyEventStatus')">
          </ai-select>
          <ai-select
              v-model="search.status"
              @change="(search.current = 1), getList()"
              placeholder="所属片区"
              :selectList="$dict.getDict('appIntegralApplyEventStatus')">
          </ai-select>
        </template>
        <template #right>
          <el-input
              v-model="search.createUserName"
              class="search-input"
              size="small"
              v-throttle="() => {search.current = 1, getList()}"
              placeholder="请输入身份证、姓名、联系电话"
              clearable
              @clear="search.current = 1, search.createUserName = '', getList()"
              suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button type="primary" icon="iconfont iconAdd" @click="handleAdd">添加</el-button>
          <el-button icon="iconfont iconDelete">删除</el-button>
        </template>
        <template #right>
          <ai-import :instance="instance" :dict="dict" type="appintegraluser" name="门店档案"
                     @success="getList()">
            <el-button icon="iconfont iconImport">导入</el-button>
          </ai-import>
          <ai-download :instance="instance" url="/app/appintegraluser/girdIntegralExport" :params="search" fileName="门店档案"
                       :disabled="tableData.length == 0">
          </ai-download>
        </template>
      </ai-search-bar>
      <ai-table
          :tableData="tableData"
          :col-configs="colConfigs"
          :total="total"
          style="margin-top: 6px;"
          :current.sync="search.current"
          :size.sync="search.size"
          @getList="getList">
        <el-table-column
            label="门店照片"
            slot="pic"
            align="left">
          <template v-slot="{ row }">
            <img :src="row.picUrl" alt="" v-viewer>
          </template>
        </el-table-column>

        <el-table-column slot="options" width="180px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="toDetail(row)">查看</el-button>
              <el-button type="text" @click="handleEdit(row)">修改</el-button>
              <el-button type="text" @click="handleDelete(row)">删除</el-button>
              <el-button type="text" >生成二维码</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
import { MessageBox } from 'element-ui'
import { mapState } from 'vuex'

export default {
  name: 'List',
  props: {
    instance: Function,
    dict: Object
  },

  data () {
    return {
      search: {
        current: 1,
        size: 10,
        areaId: '',
        applyItemId: '',
        girdId: '',
        createTimeStart: '',
        createTimeEnd: '',
        girdName: ''
      },
      userList: [],
      dictList: [],
      total: 10,
      colConfigs: [
        {type: "selection"},
        { slot: 'pic', align: 'center' },
        { prop: 'integralUserName',  label: '门店名称', align: 'center' },
        { prop: 'areaName',  label: '经营者姓名', align: 'center' },
        { prop: 'girdName',  label: '联系电话', align: 'center' },
        { prop: 'createTime',  label: '录入时间', align: 'center' },
        { prop: 'status',  label: '门店地址', align: 'center'},
        { prop: 'auditUserName',  label: '经营类型', align: 'center' },
        { prop: 'pushStatus',  label: '门店评分', align: 'center'}
      ],
      tableData: [],
      dateList: []
    }
  },
  computed: {
    ...mapState(['user']),

    hideLevel () {
      return this.user.info.areaList?.length || 0
    }
  },

  created () {
    this.search.areaId = this.user.info.areaId
    this.$dict.load('appIntegralApplyEventStatus', 'appIntegralApplyEventPushStatus').then(() => {
      this.getList()
      this.getRulesList()
    })
  },

  methods: {
    handleAdd(){
      this.$emit('change', {
        type: 'Add',
        params: {}
      })
    },
    getRulesList () {
      this.instance.post(`/app/appintegralrule/listByAppletFD?current=1&size=3000`).then((res) => {
        if (res.code === 0) {
          this.dictList = res.data.records.map(v => {
            return {
              dictName: v.ruleName,
              dictValue: v.id
            }
          })
        }
      })
    },

    onGridChange (e) {
      if (e.length) {
        this.search.girdId = e[0].id
        this.search.girdName = e[0].girdName
        this.search.current = 1
        this.getList()
      }
    },

    getList () {
      this.instance.post(`/app/appintegraluserapply/list`, null, {
        params: {
          ...this.search
        }
      }).then(res => {
        if (res.code == 0) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },

    changeArea () {
      this.search.current = 1

      this.$nextTick(() => {
        this.getList()
      })
    },

    push (id) {
      MessageBox.confirm('是否将精选内容对全体居民公开，选则否将只对本村/社区居民公开。', '推送精选', {
        distinguishCancelAndClose: true,
        confirmButtonText: '是',
        type: 'warning',
        closeOnClickModal: false,
        center: true,
        customClass: 'AiConfirm',
        cancelButtonText: '否'
      }).then(() => {
        this.instance.post(`/app/appintegraluserapply/pushById?id=${id}&status=1`).then(res => {
          if (res.code == 0) {
            this.getList()
            this.$message.success('推送成功')
          }
        })
      }).catch((e) => {
        e === 'cancel' && this.instance.post(`/app/appintegraluserapply/pushById?id=${id}&status=0`).then(res => {
          if (res.code == 0) {
            this.getList()
            this.$message.success('推送成功')
          }
        })
      })
    },

    remove (id) {
      this.$confirm('确定删除该帖子？').then((e) => {
        this.instance.post(`/app/appintegraluserapply/delete?id=${id}`).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },

    toDetail ({id}) {
      this.$emit('change', {
        type: 'Detail',
        params: {
          id: id || ''
        }
      })
    },

    handleEdit({id}){
      this.$emit('change', {
        type: 'Add',
        params: {
          id: id || ''
        }
      })
    },

    handleDelete({id}){
      this.$confirm('确定删除该数据？').then(() => {
        console.log(id)
      })
    },

    toAdd(id) {
      this.$emit('change', {
        type: 'Add',
        params: {
          id: id || ''
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-archives_list {
  .userSelcet {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 215px;
    height: 32px;
    line-height: 32px;
    border-radius: 4px;
    border: 1px solid #d0d4dc;
    overflow: hidden;
    cursor: pointer;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);

    &:hover {
      border-color: $placeholderColor;
    }

    i {
      display: flex;
      position: relative;
      align-items: center;
      justify-content: center;
      width: 30px;
      height: 100%;
      line-height: 32px;
      font-size: 14px;
      text-align: center;
      color: #d0d4dc;
      transform: rotateZ(180deg);
    }

    .el-icon-circle-close:hover {
      opacity: 0.6;
    }

    span {
      flex: 1;
      padding: 0 15px;
      font-size: 12px;
      color: $placeholderColor;
    }
  }
}
</style>
