<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="messageTitle">-->
<!--        <el-input-->
<!--          v-model="queryParams.messageTitle"-->
<!--          placeholder="请输入消息标题"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="仪器ID" prop="instrumentId">-->
<!--        <el-input-->
<!--          v-model="queryParams.instrumentId"-->
<!--          placeholder="请输入仪器ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="仪器名字" prop="instrumentName">-->
<!--        <el-input-->
<!--          v-model="queryParams.instrumentName"-->
<!--          placeholder="请输入仪器名字"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="跳转链接" prop="link">-->
<!--        <el-input-->
<!--          v-model="queryParams.link"-->
<!--          placeholder="请输入跳转链接"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="跳转链接参数" prop="linkParams">-->
<!--        <el-input-->
<!--          v-model="queryParams.linkParams"-->
<!--          placeholder="请输入跳转链接参数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="外链小程序appid" prop="redirectAppid">-->
<!--        <el-input-->
<!--          v-model="queryParams.redirectAppid"-->
<!--          placeholder="请输入外链小程序appid"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="外链小程序url" prop="redirectUrl">-->
<!--        <el-input-->
<!--          v-model="queryParams.redirectUrl"-->
<!--          placeholder="请输入外链小程序url"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="视频号" prop="videoNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.videoNo"-->
<!--          placeholder="请输入视频号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="视频号feedId" prop="feedId">-->
<!--        <el-input-->
<!--          v-model="queryParams.feedId"-->
<!--          placeholder="请输入视频号feedId"-->
<!--          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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:scriptMessage: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="['system:scriptMessage: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="['system:scriptMessage: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="['system:scriptMessage:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="消息类型" align="center" prop="messageType" >
        <template slot-scope="scope">
          <span v-show="scope.row.messageType == 1">用户注册</span>
          <span v-show="scope.row.messageType == 2">提交留言</span>
          <span v-show="scope.row.messageType == 3">仪器绑定</span>
          <span v-show="scope.row.messageType == 4">完善用户信息</span>
        </template>
      </el-table-column>
      <el-table-column label="消息标题" align="center" prop="messageTitle" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-show="scope.row.status == 1">关闭</span>
          <span v-show="scope.row.status == 0">开启</span>
        </template>
      </el-table-column>
      <el-table-column label="消息内容" align="center" prop="messageContent" />
      <el-table-column label="更新时间" align="center" prop="createTime"  width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime,'{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"  width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime,'{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:scriptMessage:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:scriptMessage:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            @click="closeStatus(scope.row, 0)"
            v-if="scope.row.status==1"
            v-hasPermi="['system:siteInfo:edit']"
          >开启弹窗</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            v-if="scope.row.status==0"
            @click="closeStatus(scope.row, 1)"
            v-hasPermi="['system:siteInfo:edit']"
          >关闭弹窗</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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span = "12">
            <el-form-item label="消息类型" prop="messageType">
              <el-select v-model="form.messageType">
                <el-option label="用户注册" :value="1" :key="1"></el-option>
                <el-option label="提交留言" :value="2" :key="2"></el-option>
                <el-option label="仪器绑定" :value="3" :key="3"></el-option>
                <el-option label="完善用户信息" :value="4" :key="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span = "12">
            <el-form-item label="消息是否开启" prop="status" label-width="120px">
              <el-switch
                v-model="form.status"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="仪器名称" prop="instrumentId" v-if="form.messageType == 3">
          <el-select v-model="form.instrumentId">
<!--            <el-option label="仪器1" :value="0" :key="0"></el-option>-->
<!--            <el-option label="仪器2" :value="1" :key="1"></el-option>-->
<!--            <el-option label="仪器3" :value="2" :key="2"></el-option>-->
            <el-option
              v-for="instrument in instrumentList"
              :key="instrument.id"
              :label="instrument.name"
              :value="instrument.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="标题" prop="messageTitle">
          <el-input v-model="form.messageTitle" placeholder="请输入消息标题"  maxlength="20"/>
        </el-form-item>
        <el-form-item label="消息内容" prop="messageContent">
          <el-input type="" v-model="form.messageContent"type="textarea" :rows="5" maxlength="50"/>
        </el-form-item>
        <el-row>
          <el-col :span="24">
            <el-form-item label="跳转类型" prop="type">
              <el-select v-model="form.type">
                <el-option label="无跳转" :value="0" :key="0"></el-option>
                <el-option label="跳转内部链接" :value="1" :key="1"></el-option>
                <el-option label="跳转小程序" :value="4" :key="4"></el-option>
                <el-option label="跳转外部链接" :value="3" :key="3"></el-option>
                <el-option label="导向视频号" :value="5" :key="5"></el-option>
                <el-option label="导向视频号直播间" :value="6" :key="6"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.type == 1">
            <el-form-item label="内部链接" prop="link">
              <el-input v-model="form.link" placeholder="请输入内部链接" />
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="form.type == 1">
            <el-form-item label="跳转参数" prop="linkParams">
              <el-input v-model="form.linkParams"  placeholder="请输入跳转参数" />
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="form.type == 3">
            <el-form-item label="外部链接" prop="link">
              <el-input v-model="form.link"  placeholder="请输入外部链接" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.type == 4">
            <el-form-item label="APPID" prop="redirectAppid">
              <el-input v-model="form.redirectAppid" placeholder="请输入小程序APPID" />
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="form.type == 4" label-width="90px">
            <el-form-item label="小程序页面" prop="redirectUrl">
              <el-input v-model="form.redirectUrl"  placeholder="请输入小程序页面" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.type == 5 || form.type == 6">
            <el-form-item label="视频号ID" prop="videoNo">
              <el-input v-model="form.videoNo" placeholder="请输入视频号ID" />
            </el-form-item>
          </el-col>
          <el-col :span="24"  v-if="form.type == 5" label-width="90px">
            <el-form-item label="feedId" prop="feedId">
              <el-input v-model="form.feedId"  placeholder="请输入视频号feedId" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">重 置</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, getMessage, delMessage, addMessage, updateMessage } from "@/api/system/scriptMessage";
import { listInstrument } from "@/api/system/instrument";

export default {
  name: "Message",
  data() {
    return {
      // 仪器表
      instrumentList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息模版表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageType: null,
        messageTitle: null,
        instrumentId: null,
        instrumentName: null,
        messageContent: null,
        status: null,
        type: null,
        link: null,
        linkParams: null,
        redirectAppid: null,
        redirectUrl: null,
        videoNo: null,
        feedId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        messageType: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        messageTitle: [
          { required: true, message: "消息标题不能为空", trigger: "blur" }
        ],
        messageContent: [
          { required: true, message: "消息内容不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "跳转类型不能为空", trigger: "blur" }
        ],
        instrumentId: [
          { required: true, message: "请选择仪器"}
        ],
        link: [
          { required: true, message: "参数不能为空", trigger: "blur" }
        ],
        redirectAppid: [
          { required: true, message: "APPID不能为空", trigger: "blur" }
        ],
        linkParams: [
          { required: true, message: "跳转参数不能为空", trigger: "blur" }
        ],
        redirectUrl: [
          { required: true, message: "小程序页面不能为空", trigger: "blur" }
        ],
        videoNo: [
          { required: true, message: "视频号ID不能为空", trigger: "blur" }
        ],
        feedId: [
          { required: true, message: "视频号feedId不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.queryInstrumentList();
  },
  methods: {
    // 查询仪器
    queryInstrumentList(){
      listInstrument(this.queryParams).then(response => {
        this.instrumentList = response.rows;
      });
    },
    /** 查询消息模版列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        messageType: null,
        messageTitle: null,
        instrumentId: null,
        instrumentName: null,
        messageContent: null,
        status: null,
        type: null,
        link: null,
        linkParams: null,
        redirectAppid: null,
        redirectUrl: null,
        videoNo: null,
        feedId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 关闭开启状态
    closeStatus(row, status){
      row.status = status;
      updateMessage(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        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 = "新建";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        if(this.form.status == 1) {
          this.form.status = false;
        } else {
          this.form.status = true;
        }
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.status) {
            this.form.status = 0;
          } else {
            this.form.status = 1;
          }
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消息模版编号为"' + ids + '"的数据项？').then(function() {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/scriptMessage/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
