<template>
  <ai-list class="AppAnnounce">
    <template slot="title">
      <ai-title title="群发居民群" isShowBottomBorder>
        <template #sub>
          <span>管理员统一创建宣发任务，选择要发送的居民群后通知群主发送，群主确认后即可群发到居民群。群主向同一个居民群每天最多可群发10条消息。</span>
        </template>
      </ai-title>
    </template>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button size="small" type="primary" icon="iconfont iconAdd" @click="toAdd('')">创建宣发</el-button>
          <ai-select
            v-model="search.status"
            @change="search.current = 1, getList()"
            placeholder="任务状态"
            :selectList="dict.getDict('mstStatus')">
          </ai-select>
          <el-date-picker
            v-model="search.startTime"
            type="date"
            size="small"
            value-format="yyyy-MM-dd"
            @change="search.current = 1, getList()"
            placeholder="选择群发开始日期">
          </el-date-picker>
          <el-date-picker
            v-model="search.endTime"
            type="date"
            size="small"
            value-format="yyyy-MM-dd"
            @change="search.current = 1, getList()"
            placeholder="选择群发结束日期">
          </el-date-picker>
          <ai-wechat-selecter :instance="instance" @change="onUserChange" :isMultiple="false" v-model="user">
            <div class="userSelcet">
              <span style="color: #606266;" v-if="search.createUserId">{{ name }}</span>
              <span v-else>创建人</span>
              <i class="el-icon-arrow-up"  v-if="!search.createUserId"></i>
              <i class="el-icon-circle-close" v-if="search.createUserId" @click.stop="user = [], search.createUserId = '', name = '', search.current = 1, getList()"></i>
            </div>
          </ai-wechat-selecter>
        </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>{{ row.createUserName }}</span>
              <span style="color: #999">{{ row.createUserDeptName }}</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,
          status: '',
          createUserId: '',
          taskTitle: '',
          startTime: '',
          endTime: ''
        },
        name: '',
        user: [],
        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: {
      onUserChange (e) {
        if (e.length) {
          this.name = e[0].name
          this.search.createUserId = e[0].id
        } else {
          this.search.createUserId = ''
          this.name = ''
        }

        this.search.current = 1
        this.getList()
      },

      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
        })
      },

      remindExamine (id) {
        this.$confirm('确认再次通知任务审核人员？').then(() => {
          this.instance.post(`/app/appmasssendingtask/remindExamine?id=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('催办成功！')
              this.getList()
            }
          })
        })
      },

      cancel (id) {
        this.$confirm('确认撤回该群发任务？').then(() => {
          this.instance.post(`/app/appmasssendingtask/cancel?id=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('撤回成功！')
              this.getList()
            }
          })
        })
      },

      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) {
        this.$emit('change', {
          type: 'Add',
          params: {
            id
          }
        })
      },

      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .AppAnnounce {
    height: 100%;
    .userinfo {
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      line-height: 1;

      span:first-child {
        margin-bottom: 4px;
      }
    }

    .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: #26f;
      }

      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>
