<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入发送的手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item label="准考证号" prop="ticketId">
          <el-input v-model="queryParams.ticketId" placeholder="请输入准考证号" />
        </el-form-item>
        <el-form-item label="发送状态" prop="status">
          <el-select v-model="queryParams.status"  placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in roleOptionsgt"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sendSms:sendSms:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sendSms:sendSms:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sendSms:sendSms:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sendSms:sendSms:export']"
        >导出</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入中考录取信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImports"
        >导入中考通知信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
         type="info"
         plain
         icon="el-icon-upload2"
         size="mini"
          @click="handleImportss"
        >导入多线下发中考通知</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImportss"
        >下发短信</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sendSmsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发送的手机号" align="center" prop="phone" />
      <el-table-column label="状态" align="center" prop="status"  />
      <el-table-column label="发送时间" align="center" prop="createTime" >
        <!-- parseTime(scope.row.createTime, '{y}-{m}-{d}') -->
        <template slot-scope="scope">
          <span>{{scope.row.createTime  }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sendSms:sendSms:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sendSms:sendSms:remove']"
          >删除</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row >
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入发送的手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改时间" prop="modifyDate">
              <el-date-picker
                    v-model="form.modifyDate"
                    type="datetime"
                    placeholder="选择时间"
                    clearable
                    value-format="yyyy-MM-dd HH:mm:ss"
                    format="yyyy-MM-dd HH:mm:ss"
                    style="width:100%"
                    >
                  </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="发送内容">
              <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>

      </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="titles" :visible.sync="opens" width="800px" append-to-body>
      <el-form ref="forms" :model="forms" :rules="ruless" label-width="100px">
        <el-row  >
          <el-col :span="24">
            <el-form-item label="短信模板" prop="templateName" >
              <el-select v-model="forms.templateName"  placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.id"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="forms.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="forms.phone" placeholder="请输入发送的手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="准考证号" prop="ticketId">
              <el-input v-model="forms.ticketId" placeholder="请输入准考证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="录取结果" prop="result">
              <el-input v-model="forms.result" placeholder="请输入录取结果" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForms">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 用户导入对话框 -->
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="uploads.title" :visible.sync="uploads.open" width="400px" append-to-body>
      <el-upload
        ref="uploads"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="uploads.headers"
        :action="uploads.url"
        :disabled="uploads.isUploading"
        :on-progress="handleFileUploadProgresss"
        :on-success="handleFileSuccesss"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplates">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForms">确 定</el-button>
        <el-button @click="uploads.open = false">取 消</el-button>
      </div>
    </el-dialog>



    <el-dialog :close-on-click-modal="false" :title="uploadss.title" :visible.sync="uploadss.open" width="400px" append-to-body>
      <el-upload
        ref="uploadss"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="uploadss.headers"
        :action="uploadss.url"
        :disabled="uploadss.isUploading"
        :on-progress="handleFileUploadProgressss"
        :on-success="handleFileSuccessss"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplatess">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileFormss">确 定</el-button>
        <el-button @click="uploadss.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSendSms, getSendSms, delSendSms, addSendSms, updateSendSms,addSendSmsxf,listSendSmsg } from "@/api/sendSms/sendSms";
import Editor from '@/components/Editor';
import { getToken } from "@/utils/auth";
export default {
  components: {
    Editor
  },
  name: "SendSms",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      titles:'',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中考下发短信日志表格数据
      sendSmsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        status: null,
      },
      // 表单参数
      form: {},
      forms:{},
      // 表单校验
      rules: {
        phone:[
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '格式不正确' }
        ]
      },
      ruless: {
        phone:[
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '格式不正确' }
        ],
        name:[
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        templateName:[
          { required: true, message: '请选择短信模板', trigger: 'blur' },
        ],
        ticketId:[
          { required: true, message: '请输入准考证号', trigger: 'blur' },
        ],
        result:[
          { required: true, message: '请输入录取结果', trigger: 'blur' },
        ]
      },
      typeOptionshuuj:[
        {
        	dictValue: 'Y',
        	dictLabel: '是'
        },
        {
        	dictValue: 'N',
        	dictLabel: '否'
        }
      ],
      roleOptionsgt:[
        {
        	dictValue: '发送失败',
        	dictLabel: '发送失败'
        },
        {
        	dictValue: '发送成功',
        	dictLabel: '发送成功'
        }
      ],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/boman-web-core/sendSms/importData"
      },
      uploads: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/boman-web-core/sendSms/importDataNotice"
      },
      uploadss: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/boman-web-core/sendSms/importDataNoticeMore"
      },
      roleOptions:[]
    };
  },
  created() {
    this.getList();
    this.getDicts("smsTemplate").then(response => {
      this.roleOptions = response.data;
    });
  },
  methods: {
    /** 查询中考下发短信日志列表 */
    getList() {
      this.loading = true;
      listSendSms(this.queryParams).then(response => {
        this.sendSmsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      if(this.title == "下发短信"){
       // this.resets()
      this.$set(this.forms, 'phone', null)
      this.$set(this.forms, 'ticketId', null)
      this.$set(this.forms, 'name', null)
      this.$set(this.forms, 'result', null)
      this.$set(this.forms, 'templateName', null)
      }else{
        this.reset()
      }

    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        content: null,
        modifyDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null

      };
      this.resetForm("form");
    },
    // 表单重置
    resets() {
      this.forms = {
        phone: null,
        ticketId:null,
        name:null,
        result:null
      };
      this.resetForm("forms");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams.status)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加中考下发短信日志";
    },
    // 下发
    handleAddf(){
      // this.reset();
      this.$set(this.forms, 'phone', null)
      this.$set(this.forms, 'ticketId', null)
      this.$set(this.forms, 'name', null)
      this.$set(this.forms, 'result', null)
      this.$set(this.forms, 'templateName', null)
      this.opens = true;
      this.titles = "下发短信";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSendSms(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改中考下发短信日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSendSms(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSendSms(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 下发短信
    submitForms() {
      this.$refs["forms"].validate(valid => {
        if (valid) {
            addSendSmsxf(this.forms).then(response => {
              this.msgSuccess("操作成功");
              this.opens = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.confirm('是否确认删除中考下发短信日志编号为"' + ids + '"的数据项？').then(function() {
        return delSendSms(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sendSms/sendSms/export', {
        ...this.queryParams
      }, `sendSms_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入数据";
      this.upload.open = true;
    },
    handleImports(){
      this.uploads.title = "导入数据";
      this.uploads.open = true;
    },
    handleImportss(){
      this.uploadss.title = "导入数据";
      this.uploadss.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      // this.download('system/user/importTemplate', {
      //   ...this.queryParams
      // }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplates() {
      // this.download('system/user/importTemplate', {
      //   ...this.queryParams
      // }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplatess() {
      // this.download('system/user/importTemplate', {
      //   ...this.queryParams
      // }, `user_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传中处理
    handleFileUploadProgresss(event, file, fileList) {
      this.uploads.isUploading = true;
    },
    // 文件上传中处理
    handleFileUploadProgressss(event, file, fileList) {
      this.uploadss.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 文件上传成功处理
    handleFileSuccesss(response, file, fileList) {
      this.uploads.open = false;
      this.uploads.isUploading = false;
      this.$refs.uploads.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 文件上传成功处理
    handleFileSuccessss(response, file, fileList) {
      this.uploadss.open = false;
      this.uploadss.isUploading = false;
      this.$refs.uploadss.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 提交上传文件
    submitFileForms() {
      this.$refs.uploads.submit();
    },
    // 提交上传文件
    submitFileFormss() {
      this.$refs.uploadss.submit();
    },

  }
};
</script>
