<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加人员
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="userId"
      border
      default-expand-all>
      <el-table-column
        prop="userName"
        label="人员名称">
      </el-table-column>
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="操作" fixed="left" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="editStatus(row.idUser)" type="danger" size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getList" />

    <el-dialog :close-on-click-modal="false" title="选择订阅人" append-to-body :visible.sync="userInfoVisible" width="70%">
      <user :is-choose="true" @chooseUser='chooseUser'></user>
    </el-dialog>

    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { getNoticeMessageUserList, addUserNoticeMessage } from '@/api/system/noticeMessage/noticeMessageUser'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import User from '@/views/system/user/user'

const form = {
  idNoticeMessage: undefined,
  idUser: undefined,
  userName: undefined
}

export default {
  name: 'WorkTeamPerson',
  components: {
    User,
    Cancledialog
  },
  data() {
    return {
      idNoticeMessage: '',
      messageName: '',
      activeName: 'first',
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',

      workTeamVisible: false,
      productVisible: false,

      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),

      confirmdialog: false,
      listLoading: false,
      tableData: [],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },

      userInfoVisible: false,
      // 添加班组档案
      workTeamSearch: {
        keyWord: ''
      },
      workTeamList: [],
      checkedTeam: [],
      checkedTeamList: [],

      // 选择产品
      productList: [],
      productSearch: {
        keyWord: ''
      },
      productListData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      checkedProduct: [],
      checkedProductList: []
    }
  },
  created() {
    this.idNoticeMessage = this.$route.query.idNoticeMessage
    this.messageName = this.$route.query.messageName
    this.tempRoute = Object.assign({}, this.$route)
    this.setTagsViewTitle()
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 设置标签名称
    setTagsViewTitle() {
      const title = '订阅用户'

      // console.log("tempRoute2",this.tempRoute);
      const route = Object.assign({}, this.tempRoute, { title: `${this.messageName}-${title}` })
      // console.log("route",route)
      this.$store.dispatch('tagsView/updateVisitedView', route)
    },
    // 获取列表
    getList() {
      const _that = this
      _that.listLoading = true
      const params = _that.search
      const data = {
        'idNoticeMessage': _that.idNoticeMessage,
        'params': JSON.stringify(params)
      }
      // console.log(data);
      getNoticeMessageUserList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    // 增加订阅人员
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)
      // _that.dialogTitle = '增加订阅人员'
      // _that.dialogStatus = '1'
      _that.userInfoVisible = true
    },
    // 增加档案
    addBtn() {
      const _that = this
      _that.form.idNoticeMessage = _that.idNoticeMessage
      const info = JSON.stringify(_that.form)
      const data = {
        'userNoticeMessage': info
      }
      // console.log(data);
      // return

      addUserNoticeMessage(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.userInfoVisible = true
      })
    },

    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getList()
    },

    // 启用禁用
    editStatus(id) {
      const _that = this
      const teamPerson = {
        idUser: id,
        idNoticeMessage: _that.idNoticeMessage
      }
      const params = {
        'userNoticeMessage': JSON.stringify(teamPerson)
      }

      const url = '/system/deleteUserNoticeMessage'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getList()
    },
    // 选择人员
    openChooseUser() {
      this.userInfoVisible = true
    },
    chooseUser(row) {
      console.log(row)
      var _that = this
      _that.userInfoVisible = false
      _that.form.idUser = row.id
      // _that.form.userName = row.userName
      _that.addBtn()
    },

  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
  .tableItemInput{
    width: 30%;
  }
</style>
