<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-news__group}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataList()">
        <el-form-item label="所属机构">
          <el-cascader v-model="ids"
                       :options="options"
                       :props="{ checkStrictly: true }"
                       clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="dataForm.state"
                     clearable
                     placeholder="请选择">
            <el-option v-for="item in stateOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                @selection-change="dataListSelectionChangeHandle"
                style="width: 100%;">
        <el-table-column type="index"
                         width="50"
                         align="center"
                         label="序号"></el-table-column>
        <el-table-column prop="groupName"
                         label="社群名称"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="groupCategory"
                         label="性质"
                         header-align="center"
                         :formatter="formatGroupCategory"
                         align="center"></el-table-column>
        <el-table-column prop="createdTime"
                         label="创建时间"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="nickname"
                         label="群主"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="state"
                         label="状态"
                         header-align="center"
                         align="center"
                         :formatter="formatState"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button v-if="scope.row.state === 0"
                       type="text"
                       size="small"
                       @click="verify(scope.row.id)">{{ $t('verify') }}</el-button>
            <el-button v-if="scope.row.state != 0"
                       type="text"
                       size="small"
                       @click="look(scope.row.id)">{{ $t('look') }}</el-button>
            <el-button v-if="scope.row.state === 10"
                       type="text"
                       size="small"
                       @click="ban(scope.row.id)">{{ $t('ban') }}</el-button>
            <el-button v-if="scope.row.state === 15"
                       type="text"
                       size="small"
                       @click="liftTheBan(scope.row.id)">{{ $t('liftTheBan') }}</el-button>
            <el-button v-if="(scope.row.state === 10 || scope.row.state === 15) && scope.row.groupCategory !=='0'"
                       type="text"
                       size="small"
                       @click="handDisband(scope.row.id)">{{ $t('disband') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :page-size="limit"
                     :total="total"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <group-detail v-if="groupDetailVisible"
                    ref="groupDetail"
                    @refreshDataList="getDataList"></group-detail>
      <group-approve v-if="groupApproveVisible"
                     ref="groupApprove"
                     @refreshDataList="getDataList"></group-approve>

      <!-- 解散原因弹框 -->
      <el-dialog :visible.sync="processingOpinionsVisible"
                 v-if="processingOpinionsVisible"
                 title="解散社群">
        <el-form :model="postForm" :rules="dataRule" ref="postForm" :label-width="$i18n.locale === 'en-US' ? '150px' : '100px'">
          <el-form-item label="社群ID:" v-if="false">
            <el-input v-model="postForm.id" type="textarea" placeholder="不超过500字">
          </el-input>
          </el-form-item>
          <el-form-item label="社群状态:" v-if="false">
            <el-input v-model="postForm.state" type="textarea" placeholder="不超过500字">
          </el-input>
          </el-form-item>
          <el-form-item label="解散原因:" prop="processingOpinions">
            <el-input v-model="postForm.processingOpinions" type="textarea" placeholder="不超过500字">
          </el-input>
          </el-form-item>
        </el-form>
        <template slot="footer">
          <el-button @click="cancelDisBand">{{ $t('cancel') }}</el-button>
          <el-button type="primary" @click="confirmDisBand">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import GroupDetail from './group-detail'
import GroupApprove from './group-approve'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/group/group/page',
        getDataListIsPage: true
      },
      groupDetailVisible: false,
      groupApproveVisible: false,
      processingOpinionsVisible: false, // 解散原因弹框显示标识
      dataForm: {
        id: '',
        streetId: '',
        communityId: '',
        gridId: '',
        state: '0'
      },
      postForm: {
        id: '',
        state: 0,
        processingOpinions: ''// 解散原因
      },
      ids: [],
      options: [],
      stateOptions: [{
        id: '0',
        name: '待审核'
      }, {
        id: '5',
        name: '审核不通过'
      }, {
        id: '10',
        name: '正常'
      }, {
        id: '15',
        name: '禁言'
      }, {
        id: '20',
        name: '已解散'
      }]
    }
  },
  computed: {
    dataRule () {
      return {
        processingOpinions: [
          { required: true, message: '解散原因不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    GroupDetail,
    GroupApprove
  },
  created: function () {
    this.getOptions()
  },
  watch: {
    'ids': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 1) {
        this.dataForm.streetId = this.ids[0]
      }
      if (val.length === 2) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = this.ids[1]
      }
      if (val.length === 3) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = this.ids[1]
        this.dataForm.gridId = this.ids[2]
      }
    }
  },
  methods: {
    formatState: function (row, column) {
      if (row.state === 0) {
        return '待审核'
      }
      if (row.state === 5) {
        return '审核不通过'
      }
      if (row.state === 10) {
        return '正常'
      }
      if (row.state === 15) {
        return '禁言'
      }
      if (row.state === 20) {
        return '已解散'
      }
    },
    formatGroupCategory: function (row, column) {
      return row.groupCategory === '0' ? '党员群' : row.groupCategory === '1' ? '自建群' : '未知'
    },
    ban (id) {
      this.$confirm('确定禁言社群？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.postForm.state = 15
        this.postForm.id = id
        this.$http['post']('/group/group/operate', this.postForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.getDataList()
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500
          })
        }).catch(() => { })
      }).catch(() => { })
    },
    liftTheBan (id) {
      this.$confirm('确定解禁社群？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.postForm.state = 10
        this.postForm.id = id
        this.$http['post']('/group/group/operate', this.postForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.getDataList()
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500
          })
        }).catch(() => { })
      }).catch(() => { })
    },
    disband (id) {
      this.$confirm('确定解散社群？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.postForm.state = 20
        this.postForm.id = id
        this.$http['post']('/group/group/operate', this.postForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.getDataList()
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500
          })
        }).catch(() => { })
      }).catch(() => { })
    },
    look (id) {
      this.groupDetailVisible = true
      this.$nextTick(() => {
        this.$refs.groupDetail.dataForm.groupId = id
        this.$refs.groupDetail.init()
      })
    },
    verify (id) {
      this.groupApproveVisible = true
      this.$nextTick(() => {
        this.$refs.groupApprove.dataForm.id = id
        this.$refs.groupApprove.init()
      })
    },
    getOptions () {
      this.$http.get(`/sys/user/deptOptions/getByLoginUser`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      }).catch(() => { })
    },
    handDisband (id) {
      this.postForm.state = 20
      this.postForm.id = id
      this.postForm.processingOpinions = ''
      this.processingOpinionsVisible = true
    },
    confirmDisBand () {
      if (this.postForm.processingOpinions == null || this.postForm.processingOpinions === '') {
        return this.$message.error('请填写解散原因')
      }
      if (this.postForm.processingOpinions.length > 500) {
        return this.$message.error('解散原因不能超过500字')
      }
      this.$http['post']('/group/group/operate', this.postForm).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.processingOpinionsVisible = false
        this.getDataList()
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500
        })
      }).catch(() => { })
    },
    cancelDisBand () {
      console.log(this.$refs['postForm'])
      this.$refs['postForm'].resetFields()
      this.processingOpinionsVisible = false
    }
  }
}
</script>
