<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input maxlength="50" v-model="queryParams.title" placeholder="请输入标题" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="类型" prop="type">
            <el-select style="width: 100%;" v-model="queryParams.type" placeholder="请选择类型">
              <el-option v-for="dict in dict.type.message_type" :key="dict.value" :label="dict.label"
                         :value="`${dict.value}`"></el-option>
            </el-select>
          </el-form-item>
      <!-- <el-form-item label="类型" prop="type">
        <el-input v-model="queryParams.sendStutas" placeholder="选择" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['core:send:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sendList">
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="类型" align="center" key="type" prop="type" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.message_type" :value="scope.row.type" />
        </template>
      </el-table-column>

      <el-table-column label="发送状态" align="center" key="sendStatus" prop="sendStatus" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.send_status" :value="scope.row.sendStatus" />
        </template>
      </el-table-column>


      <el-table-column label="发送时间" align="center" prop="sendTime" width="150">
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.sendStatus == 2" icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)" v-hasPermi="['core:send:edit']">编辑</el-button>

          <el-button size="mini" type="text" v-if="scope.row.sendStatus == 2" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['core:send:remove']">删除</el-button>

          <el-button size="mini" type="text" v-if="scope.row.sendStatus == 1" icon="el-icon-search"
            @click="handleSelect(scope.row)" v-hasPermi="['core:send:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" maxlength="50" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" style="width: 100%" placeholder="请选择类型">
            <el-option v-for="dict in dict.type.message_type" :key="dict.value" :label="dict.label"
              :value="`${dict.value}`"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" :rows="10" maxlength="2000" placeholder="输入内容" />
        </el-form-item>

        <el-form-item label="发送目标" prop="sendUser">
          <el-select mode="multiple" style="width: 100%;" v-model="form.sendUser" placeholder="按用户选择">
            <el-option v-for="item in userOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>

          <el-select mode="multiple" style="width: 100%;" v-model="form.sendRole" placeholder="按角色选择">
            <el-option v-for="item in roleOptions" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发送时间" prop="sendTimeType">
          <el-radio-group v-model="form.sendTimeType">
            <el-radio v-for="dict in dict.type.send_time_type" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="sendTime" v-if="form.sendTimeType == 2" label="指定时间">
          <el-date-picker style="width: 100%;" v-model="form.sendTime" @change="startDateChanged"
            :picker-options="startDatepickerOptions" align="right" type="datetime" placeholder="选择指定时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="yes" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input readonly v-model="form.title" maxlength="50" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" style="width: 100%" placeholder="请选择类型" :disabled="true">
            <el-option v-for="dict in dict.type.message_type" :key="dict.value" :label="dict.label"
              :value="`${dict.value}`" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input readonly v-model="form.content" type="textarea" :rows="10" maxlength="2000" placeholder="输入内容" />
        </el-form-item>

        <el-form-item label="发送目标" prop="sendUser">
          <el-select mode="multiple" style="width: 100%;" v-model="form.sendUser" placeholder="按用户选择" :disabled="true">
            <el-option v-for="item in userOptions" :key="item.id" :label="item.name" :value="item.id"
              :disabled="true"></el-option>
          </el-select>

          <el-select mode="multiple" style="width: 100%;" v-model="form.sendRole" placeholder="按角色选择" :disabled="true">
            <el-option v-for="item in roleOptions" :key="item.id" :label="item.roleName" :value="item.id"
              :disabled="true"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发送时间" prop="sendTimeType">
          <el-radio-group v-model="form.sendTimeType">
            <el-radio v-for="dict in dict.type.send_time_type" :key="dict.value" :label="dict.value" :disabled="true">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="sendTime" v-if="form.sendTimeType == 2" label="指定时间" :disabled="true">
          <el-date-picker readonly style="width: 100%;" v-model="form.sendTime" @change="startDateChanged"
            :picker-options="startDatepickerOptions" align="right" type="datetime" placeholder="选择指定时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import { listSend, getSend, delSend, addSend, updateSend } from "@/api/core/send/send";
import { listAllUser } from "@/api/system/user";
import { allRole } from "@/api/system/role"
export default {
  name: "Send",
  dicts: ['send_time_type', 'message_type', 'send_status'],
  data() {
    return {
      startDatepickerOptions: {
        disabledDate(time) {
          const date = new Date();
          date.setTime(date.getTime());
          return time.getTime() < date;
        },
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      //全部用户
      userOptions: [],
      //全部角色
      roleOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      sendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      yes: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        type: null,
        content: null,
        sendUser: null,
        sendRole: null,
        sendTimeType: null,
        sendTime: null,
        sendStutas: null
      },
      // 表单参数
      form: {
        title: null,
        type: null,
        content: null,
        sendUser: null,
        sendRole: null,
        sendTimeType: null,
        sendTime: null,
        sendStutas: null
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'change' },
        ],
        sendUser: [
          { required: true, message: '请选择用户', trigger: 'change' },
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'change' },
        ],
        sendTime: [
          { required: true, message: '请选择发送时间', trigger: 'change' },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAllUser();
    this.getAllRole();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listSend(this.queryParams).then(response => {
        this.sendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAllUser() {
      listAllUser().then(response => {
        this.userOptions = response.data;
        this.form.sendRole = null;
      })
    },
    getAllRole() {
      allRole().then(response => {
        this.roleOptions = response.data;
        this.form.sendUser = null;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        type: null,
        content: null,
        sendUser: null,
        sendRole: null,
        sendTimeType: "1",
        sendTime: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        sendStutas: null
      };
      this.resetForm("form");
    },
    startDateChanged(time) {
      if (this.form.sendTime > this.form.endTime) {
        this.form.endTime = this.form.sendTime;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.title)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSend(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑消息";
      });
    },
    /**
  * 详情按钮操作
  */
    handleSelect(row) {
      const id = row.id
      getSend(id).then(response => {
        this.form = response.data;
        this.yes = true;
        this.title = "详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.title || this.names;
      this.$modal.confirm('是否确认删除标题为"' + names + '"的数据项？').then(function () {
        return delSend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/send/export', {
        ...this.queryParams
      }, `send_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
