<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" disabled style="width: 100%">
              <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label"
                :value="dict.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <h4 class="form-header h4">详情列表</h4>
    <el-row class="mb8" :gutter="10">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          size="mini"-->
      <!--          icon="el-icon-plus"-->
      <!--          @click="handleAdd"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          size="mini"-->
      <!--          icon="el-icon-delete"-->
      <!--          :disabled="!isMultiple"-->
      <!--          @click="handleDelete"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="info" plain size="mini" icon="el-icon-close" @click="handleClose">关闭
        </el-button>
      </el-col>
    </el-row>
    <el-table ref="receiveList" stripe style="width: 100%;" :data="receiveList" @selection-change="handleSelectChange">
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <!--      <el-table-column label="工作编号" prop="teacherCode" align="center"></el-table-column>-->
      <el-table-column label="姓名" prop="parentName" align="center"></el-table-column>
      <el-table-column label="关系" prop="relationship" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.relationship" :value="scope.row.relationship" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="性别" prop="sex" align="center"></el-table-column>-->
      <el-table-column label="电话" prop="telephone" align="center"></el-table-column>
      <!--      <el-table-column label="身份号" prop="idCard" align="center"></el-table-column>-->
      <el-table-column label="是否阅读" prop="status" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">已阅读</el-tag>
          <el-tag type="warning" v-else>未阅读</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <ExPagination v-show="total > 0" :total="total" :page.sync="queryForm.pageNum" :limit.sync="queryForm.pageSize"
      @pagination="getList"></ExPagination>

    <el-dialog :visible.sync="open" title="选择推送人" width="960px">
      <el-form :model="pushForm" inline size="small">
        <el-form-item label="姓名" porp="userName">
          <el-input v-model="pushForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="pushForm.phonenumber"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="el-icon-search" @click="handlePushQuery">搜索
          </el-button>
          <el-button plain size="small" icon="el-icon-refresh" @click="resetPushQuery">重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-table ref="pushList" stripe width="100%" v-loading="loading" :data="pushList"
        @selection-change="handleSelectPush" @row-click="handleRowClick">
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column label="用户姓名" prop="userName" align="center"></el-table-column>
        <el-table-column label="手机号" prop="phonenumber" align="center"></el-table-column>
      </el-table>

      <ExPagination v-show="total > 0" :total="total" :page.sync="queryForm.pageNum" :limit.sync="queryForm.pageSize"
        @paginaiton="getList"></ExPagination>

      <div slot="footer">
        <el-button :disabled="!isSimple" type="primary" size="small" @click="handleSelect">确 认</el-button>
        <el-button plain size="small" @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { addReceive, deleteReceive, getList } from "@/api/smartSchool/informationPush/parentsDetails";

export default {
  name: "ParentInfo",
  dicts: ['sys_notice_type', 'relationship'],
  data() {
    return {
      form: {
        title: '',
        type: ''
      },
      // 主键id
      pushId: '',
      total: 0,
      receiveList: [],
      loading: false,
      isSimple: false,
      isMultiple: false,
      ids: [],
      // 选中教师对话框、
      open: false,
      // 主页列表查询
      queryForm: {
        pageNum: 1,
        pageSize: 10
      },
      pushForm: {
        pageNum: 1,
        pageSize: 10
      },
      pushList: []
    }
  },
  created() {
    this.init();
  },
  methods: {
    /** 初始化参数数据、列表数据查询 */
    init() {
      const { title, type } = this.$route.query;
      this.pushId = this.$route.query && this.$route.query.id;
      this.form = { title, type };
      this.getList();
    },
    /** 获取列表数据 */
    getList() {
      this.loading = true;
      getList(this.pushId, this.queryForm).then(response => {
        this.loading = false;
        this.receiveList = response.rows;
        this.total = response.total;
      }).catch(error => {
        this.loading = false;
      })
    },

    /** 选中项发生变化 */
    handleSelectChange(selection) {
      this.ids = selection.map(item => item.id);
      this.isSimple = selection.length === 1;
      this.isMultiple = selection.length > 1;
    },

    /** 新增 */
    handleAdd() {
      this.open = true;
    },

    /** 删除 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中推送教师?').then(() => {
        this.$modal.loading('正在处理数据，请稍等...');
        return deleteReceive(this.pushId, ids);
      }).then(response => {
        this.$modal.closeLoading();
        this.$modal.msgSuccess('删除成功');
      }).catch(error => {
        this.$modal.closeLoading();
      })
    },
    /** 关闭 */
    handleClose() {
      this.$tab.closeOpenPage({
        path: '/gradeWork/jxgy/parentsInfoPush'
      });
    },

    /** 选中接收人 */
    handleSelect() {
      this.$modal.loading('正在上传数据，请稍等...');
      addReceive(this.pushId, this.pushIds).then(response => {
        this.$modal.closeLoading();
        this.$modal.msgSuccess('新增成功');
        this.getList();
      }).catch(error => {
        this.$modal.closeLoading();
      })
    },

    /** 关闭对话框 */
    closeDialog() {
      this.reset();
      this.open = false;
    },

    /** 选中推送人列表 */
    getPushList() {

    },

    /** 对话框模糊查询 */
    handlePushQuery() {
      this.getPushList();
    },

    /** 重置对话框表单 */
    resetPushQuery() {
      this.reset();
      this.handlePushQuery();
    },

    /** 重置对话框表单 */
    reset() {
      this.pushForm = {
        pageNum: 1,
        pageSize: 10,
        userName: '',
        phonenubmer: ''
      };
    },
    /** 选中发生变化时 */
    handleSelectPush(selections) {
      this.pushIds = selections.map(item => item.id);
      this.isDisabled = !(selections.length > 0);
    },

    /** 单击选中 */
    handleRowClick(row) {
      this.$refs.pushList.clearSelection();
      this.$refs.pushList.toggleRowExpansion(row, true);
    }
  }
}
</script>

<style scoped></style>
