<template>
  <ai-list class="AppMaterialLibrary">
    <template slot="title">
      <ai-title title="素材库" isShowBottomBorder>
        <template #sub>
          <span>网格员和居民触发的风险事件，将通过应用消息即时提醒相关人员进行跟进</span>
        </template>
      </ai-title>
    </template>
    <template #left>
      <div class="left">
        <div class="title">
          <h2>分组</h2>
          <el-button icon="iconfont iconAdd" size="mini">添加分组</el-button>
        </div>
        <el-scrollbar class="group-list">
          <div
            class="item"
            v-for="(item, index) in 20"
            :class="[currIndex === index ? 'active' : '']"
            :key="index"
            @click="currIndex = index">
            <h2>全部分类</h2>
            <el-dropdown class="avatar-container">
              <img v-if="currIndex !== index" src="https://cdn.cunwuyun.cn/dvcp/announce/menu-active.png"/>
              <img v-else src="https://cdn.cunwuyun.cn/dvcp/announce/menu.png"/>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>编辑</el-dropdown-item>
                <el-dropdown-item>删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-scrollbar>
      </div>
    </template>
    <template slot="content">
      <div class="tabs">
        <span>话术</span>
        <span>图片</span>
        <span>小程序</span>
        <span>话术</span>
        <span>话术</span>
      </div>
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button size="small" type="primary" icon="iconfont iconAdd" @click="toAdd('')">添加素材</el-button>
        </template>
        <template slot="right">
          <el-input
            v-model="search.taskTitle"
            size="small"
            v-throttle="() => { search.current = 1, getList() }"
            placeholder="请输入任务名称"
            clearable
            @clear="search.current = 1, search.taskTitle = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        v-loading="loading"
        style="margin-top: 6px; width: 100%;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="user" width="140px" label="创建人" align="center">
          <template slot-scope="{ row }">
            <div class="userinfo">
              <span>
                <ai-open-data type="userName" :openid="row.createUserId"></ai-open-data>
              </span>
              <span style="color: #999">
                <ai-open-data type="departmentName" :openid="row.createUserDept"></ai-open-data>
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column slot="options" width="140px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="remindExamine(row.id)" v-if="['0'].includes(row.status)">催办</el-button>
              <el-button type="text" @click="cancel(row.id)" v-if="['0'].includes(row.status)">撤回</el-button>
              <el-button type="text" @click="toDetail(row.id)">详情</el-button>
              <el-button type="text" @click="toAdd(row.id)" v-if="['1', '3'].includes(row.status)">编辑</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
  export default {
    name: 'List',

    props: {
      instance: Function,
      dict: Object
    },

    data() {
      return {
        search: {
          current: 1,
          size: 10,
          taskTitle: '',
          endTime: ''
        },
        currIndex: 0,
        tableData: [],
        loading: false,
        total: 0,
        colConfigs: [
          { prop: 'taskTitle', label: '任务名称' },
          { prop: 'typeName', label: '群发类型', align: 'center' },
          { slot: 'user', label: '创建人', openType: 'userName', align: 'center' },
          { prop: 'choiceTime', label: '群发时间', align: 'center' },
          {
            prop: 'status',
            align: 'center',
            label: '状态',
            render: (h, {row}) => {
              return h('span', {
                style: {
                  color: this.dict.getColor('mstStatus', row.status)
                }
              }, this.dict.getLabel('mstStatus', row.status))
            }
          },
          { prop: 'completionRate', label: '任务完成率', align: 'center', formart: v => v ? v === '0.0' ? '0%' : `${v}%` : '-' }
        ]
      }
    },

    created () {
      this.dict.load('mstStatus', 'mstSendType').then(() => {
        // this.getList()
      })
    },

    methods: {
      getList() {
        this.loading = true
        this.instance.post(`/app/appmasssendingtask/list`, null, {
          params: {
            ...this.search,
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records.map(v => {
              return {
                ...v,
                typeName: '群发居民群'
              }
            })
            this.total = res.data.total

            this.$nextTick(() => {
              this.loading = false
            })
          } else {
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
      },

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

      toAdd(id) {
      },

      toDetail (id) {
      }
    }
  }
</script>

<style lang="scss" scoped>
  .AppMaterialLibrary {
    height: 100%;

    ::v-deep .ai-list__content--left {
      margin-right: 0;
    }

    .left {
      width: 100%;
      height: 100%;
      overflow: hidden;
      background: #F5F5F9;

      .title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 40px;
        padding: 0 8px 0 16px;
        background: #FAFAFB;
        box-shadow: inset 0px -1px 0px 0px #E6ECF0;
        border-radius: 2px 0px 0px 0px;

        h2 {
          font-size: 14px;
          color: #222222;
        }

        .el-button {
          height: 28px;
        }
      }
      .group-list {
        height: calc(100% - 40px);
        padding-top: 8px;
        overflow-x: hidden;
        .item {
          display: flex;
          align-items: center;
          justify-content: space-between;
          height: 40px;
          padding: 0 16px 0 24px;
          cursor: pointer;
          background: transparent;
          transition: all ease 0.5s;

          h2 {
            font-size: 14px;
            color: #222;
            font-weight: 600;
          }

          &:hover, &.active {
            background: #E8EFFF;

            h2 {
              color: #2266FF;
            }
          }
        }
      }
    }
  }
</style>
