<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="id"
      border
      default-expand-all>
      <el-table-column label="消息代码" align="center" prop="messageCode" />
      <el-table-column label="消息名称" align="center" prop="messageName" >
      </el-table-column>

      <el-table-column label="消息类型类型" align="center" prop="teamKind">
        <template slot-scope="{row}">
          <span v-if="row.messageKind == 0">不发送</span>
          <span v-else-if="row.messageKind == 1">站内信</span>
          <span v-else-if="row.messageKind == 2">即时信息</span>
          <span v-else-if="row.messageKind == 3">全部</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            正常
          </el-tag>
          <el-tag v-else-if="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="left" align="center" class-name="small-padding fixed-width" width="335">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button
            size="mini"
          @click="$router.push({path:'/system/noticeMessageUser/'+row.id ,query:{idNoticeMessage:row.id,messageName:row.messageName}})">订阅用户</el-button>
          <el-button @click="editStatus(row,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row,'0')" v-else-if="row.voidFlag==1" size="mini">
            启用
          </el-button>
          <el-button size="mini" @click="testMassage(row)" type="warning">测试发送</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="dialogTitle" :visible.sync="dialogVisible" width="600px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="消息代码" prop="dictLabel">
          <el-input v-model="form.messageCode" :disabled="dialogStatus == 2" placeholder="请输入消息类型名称" />
        </el-form-item>
        <el-form-item label="消息名称" prop="dictLabel">
          <el-input v-model="form.messageName" placeholder="请输入消息类型名称" />
        </el-form-item>

        <el-form-item label="消息类型类型">
          <el-select v-model="form.messageKind">
            <el-option
              v-for="(item,index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
    <el-dialog :close-on-click-modal="false" title="选择负责人" :visible.sync="userInfoVisible" width="70%">
      <user :is-choose="true" @chooseUser='chooseUser'></user>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" title="测试发送消息" :visible.sync="testMassageVisible" width="500px">
      <el-form label-width="100px" label-position="right">
        <el-form-item label="消息内容" prop="dictLabel">
          <el-input v-model="messageForm.messageContent" type="textarea" placeholder="请输入消息内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="testMassageVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendMessage">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getNoticeMessageList, addNoticeMessage, getNoticeMessageInfo, updateNoticeMessage, testNoticeMessage } from '@/api/system/noticeMessage/noticeMessage'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import User from '@/views/system/user/user'

const form = {
  messageCode: undefined,
  messageName: undefined,
  messageKind: undefined,
}

export default {
  components: {
    User,
    Cancledialog, Pagination
  },
  data() {
    return {
      tempRoute: {},
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,

      innerVisible: false,

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

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

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      options: [
        {
          label: '不发送',
          value: 0
        },
        {
          label: '站内信',
          value: 1
        },
        {
          label: '即时信息',
          value: 2
        },
        {
          label: '全部',
          value: 3
        },
      ],
      userInfoVisible: false,
      testMassageVisible: false,
      messageForm: {
        messageCode:'',
        messageContent:''
      }

    }
  },
  mounted() {
    this.getList()
  },
  methods: {


    // 获取列表
    getList() {
      const _that = this
      _that.listLoading = true
      const params = _that.search
      const data = {
        'params': JSON.stringify(params)
      }
      // console.log(data);
      getNoticeMessageList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    openChoose() {
      var _that = this
      _that.userInfoVisible = true
    },
    chooseUser(row) {
      console.log(row)
      var _that = this
      _that.userInfoVisible = false
      _that.form.idManagerUser = row.id
      _that.form.managerUserName = row.userName
    },

    // 增加消息类型
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)

      _that.form.idType = _that.typeId
      _that.dialogTitle = '增加消息类型'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 增加档案
    addBtn() {
      const _that = this
      const info = JSON.stringify(_that.form)
      const data = {
        'noticeMessage': info
      }
      // console.log(data);
      // return

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

    // 唤起修改
    handleUpdate(row) {
      const _that = this

      _that.dialogTitle = '修改消息类型'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      const data = {
        messageId: row.id
      }
      // console.log(data);
      getNoticeMessageInfo(data).then(res => {
        console.log(res)
        const data = res.data

        _that.form = {
          id: data.id,
          messageCode: data.messageCode,
          messageName: data.messageName,
          messageKind: data.messageKind
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改档案
    editBtn() {
      const _that = this
      const info = JSON.stringify(_that.form)
      const data = {
        'noticeMessage': info
      }
      // console.log(data);
      // return;

      updateNoticeMessage(data).then(res => {
        console.log(res)
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

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

    // 启用禁用
    editStatus(row, status) {
      const _that = this
      const printerTypeInfo = {
        id: row.id,
        voidFlag: status
      }
      const params = {
        'noticeMessage': JSON.stringify(printerTypeInfo)
      }

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

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getList()
    },

    // 测试发送消息
    testMassage(row) {
      const _that = this
      _that.testMassageVisible = true
      _that.messageForm = {
        messageCode: row.messageCode,
        messageContent: ''
      }
    },
    sendMessage() {
      const _that = this
      const data = _that.messageForm
      // console.log(data);
      // return

      testNoticeMessage(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.testMassageVisible = false
          _that.$message({
            message: '发送成功',
            type: 'success'
          })
          // _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.testMassageVisible = true
      })
    }

  }
}
</script>

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