<template>
  <div class="app-container">
    <el-card >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="180px">
        <el-form-item label="" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入短信名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>



        <el-form-item label="" prop="status">
          <el-select v-model="queryParams.status" style="width: 150px" placeholder="短信模版状态">
            <el-option
              v-for="dict in dict.type.message_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['setting:message:add']"
          >新增短信模板</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="messageId" label="短信模板ID"></el-table-column>
        <el-table-column prop="name" label="短信名称"></el-table-column>
        <el-table-column prop="content" label="短信模板内容"></el-table-column>
<!--        <el-table-column prop="image" label="短信图片">-->
<!--          <template slot-scope="scope">-->
<!--            <ImagePreview :src="scope.row.image"></ImagePreview>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column prop="deptNames" label="适用范围"></el-table-column>
<!--        <el-table-column prop="roleIds" label="接收角色"></el-table-column>-->

        <el-table-column prop="status" label="短信状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==0">
              停用
            </div>
            <div v-else>
              启用
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createName" label="创建人"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="edit(scope.row)"
              v-hasPermi="['setting:message:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="del(scope.row)"
              v-hasPermi="['setting:message:delete']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </el-card>

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="35%" append-to-body>
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">

        <el-form-item label="短信模板ID" prop="messageId">
          <el-input v-model="form.messageId" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="短信名称" prop="name">
          <el-input v-model="form.name" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="适用范围" prop="deptIdList">
          <el-select v-model="form.deptIdList" multiple placeholder="请选择所属机构" style="width: 300px" >
            <el-option
              v-for="item in deptListSelect"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="form.status"
            active-value="1"
            inactive-value="0">
          </el-switch>

        </el-form-item>

<!--        <el-form-item label="适用角色" prop="categoryName">-->
<!--          <el-select v-model="form.roleIds" style="width: 300px" placeholder="全部">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.role_ids"-->
<!--              :key="dict.label"-->
<!--              :label="dict.label"-->
<!--              :value="dict.label"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="短信内容" prop="content"  >
          <el-input type="textarea" v-model="form.content" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

  </div>
</template>

<script>

import {deleteApi, getListApi, saveApi, updateApi} from "@/api/setting/message";
import { getToken } from '@/utils/auth'
import {selectChildByDeptId} from "@/api/system/Site/site";

export default {
  name: "printIndex",
  dicts: ['dept_ids','message_status'],
  data() {
    return {
      tableData: [],
      form: {},
      deptListSelect:[],
      systemPositionList:[{
        value: '首页',
        label: '首页'
      }, {
        value: '系统首页',
        label: '系统首页'
      },],
      // 是否显示弹出层
      open: false,
      title:"",
      total:0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        bannerName:'',
        systemPosition:'',
        bannerStatus:'',
      },

      // 上传地址
      uploadAction: process.env.VUE_APP_BASE_API + 'backendApi/file/upload',
      uploadHeader: { 'Access-Token' : getToken() },  // 上传文件列表
      uploadFiles: [
        {name:"nihao",
          url:'http://localhost:8008/static/uploadImages/20231103/ffbbe7d3ee1441fdaf706802fa0f176a.png'}
      ],
      // 图片根目录
      imagePath: process.env.VUE_APP_BASE_API,
      // 隐藏上传
      hideUpload: false,
      // 表单校验
      rules: {
        messageId: [
          { required: true, message: "短信模板id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "短信名称不能为空", trigger: "blur" }
        ],
        deptIdList: [
          { required: true, message: "适用范围不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "短信介绍不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList()
    this.selectChildByDeptIdApi()
  },
  methods: {
    selectChildByDeptIdApi() {
      selectChildByDeptId().then(res => {
        this.deptListSelect = res.data

      })
    },
    getList(){
      getListApi(this.queryParams).then(res=>{
        this.tableData = res.data.records;
        this.total = res.data.total
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 表单验证通过，可以提交数据
          this.submitDeviceInfo();
        } else {
          // 表单验证失败，不执行任何操作
          return false;
        }
      });
    },
    submitDeviceInfo() {
      // 在这里调用接口提交设备信息数据
      console.log('提交设备信息数据:', this.form);
      if (this.form.id) {
        updateApi(this.form).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }
        })
      }else {
        saveApi(this.form).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }
        })
      }
    },
    changeStatus(data){
      updateApi(data).then(res=>{
        if(res.code === 200) {
          this.$message({
            type: 'success',
            message: '修改状态成功!'
          });
          this.getList()
        }

      })
    },
    // 上传封面
    handleUploadSuccessCover(file) {
      this.form.productImage = file.data.fileName;
      this.form.productImage = file.data.fileName;
    },
    handleQuery(){
      this.getList()
    },
    resetQuery(){
      this.queryParams = {
        page: 1,
        pageSize: 10,
        deviceName:'',
      },
        this.getList()
    },
    handleAdd(e) {
      this.clean()
      this.open = true;
      this.title = "新增短信"
    },
    edit(e) {
      this.clean()
      this.open = true;
      this.title = "修改短信"
      this.form = e
    },
    del(e) {
      this.$confirm('此操作将永久删除该短信, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteApi(e.id).then(res=>{
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    clean() {
      this.form= {
        id: null,            // 自增id
        bannerName: '',     // Banner名称
        productImage: '',           // 短信图片
        systemPosition: '',      // 系统位置
        sortOrder: null,        // 排序号
        bannerLink: '',          // Banner链接
        bannerStatus: false,       // Banner状态
        note: '',    // 备注
        createByName: '', //
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
.d-dialog >>> .el-upload--picture-card {
  width: 100px;
  height: 100px;
  line-height: 100px;
}

.d-dialog >>> .avatar-uploader .el-upload {
  width: 100px;
  height: 100px;
}
.d-dialog >>> .el-upload-list--picture-card .el-upload-list__item {
  width: 100px;
  height: 100px;
}
.list-img{
  width: 98px;
  height: 98px;
}
</style>

