<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      title="选择用户"
      width="60%"
    >
      <div>
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form inline onsubmit="return false" :label-width="80">
              <label class="label-sign" />
              <Form-item label="账号:" style="width: 300px">
                <el-input v-model="formObj.username" placeholder="请输入账号" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item label="手机号码:" style="width: 300px">
                <el-input v-model="formObj.mobile" placeholder="请输入手机号码" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-btn">
                <el-button type="primary" @click="_formSearch">
                  搜索
                </el-button>
              </Form-item>
            </Form>
          </el-col>
          <!-- 表格 -->
          <el-col :span="24">
            <element-table
              ref="pageTable"
              :page-columns="pageColumns"
              :table-height="400"
              :get-page="getPage"
              :show-check-box="true"
              @on-result-change="_tableResultChange"
            >
              <el-table-column
                v-for="item in pageColumns"
                :key="item.key"
                show-overflow-tooltip
                sortable
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <span v-if="item.dateTime">
                    {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM'):'' }}
                  </span>
                  <span v-else-if="item.sex">
                    {{ scope.row[item.key] === 0 ?'男':'女' }}
                  </span>
                  <span v-else>
                    {{ scope.row[item.key] }}
                  </span>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 发送消息
 */
import { message, sysUser } from '../../../api'

export default {
  data() {
    return {
      showModal: false,
      selectIds: [],
      pageColumns: [
        { title: '账号', key: 'username' },
        { title: '性别', key: 'sex', width: 70, sex: true },
        { title: '手机号码', key: 'mobile' },
        { title: '电子邮箱', key: 'email' },
        { title: 'QQ', key: 'qq' }
      ],
      getPage: {},
      formObj: {
        messageId: '',
        username: '',
        mobile: ''
      },
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '发送', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '发送':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open(id) {
      this.showModal = true
      this.formObj = this.$resetFields(this.formObj)
      this.$nextTick(() => {
        this.formObj.messageId = id
        this._formSearch()
        this._hideLoading()
      })
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysUser.pageNotSendMessage(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },
    _ok() {
      if (this.selectIds.length === 0) {
        this.$message.warning('请至少选中一个用户！')
      } else {
        this._toSend()
      }
    },
    _toSend: async function() {
      const result = await message.send({
        id: this.formObj.messageId,
        userIds: this.selectIds
      })
      this._resultChange(result, '发送成功!')
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _cancel() {
      this.showModal = false
    }
  }
}
</script>
