<!-- 接收人 -->
<template>
  <div class="app-container receiver">
    <div class="page-container receiverPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="receiveName">
            <el-input
              maxLength="30"
              v-model="queryParams.receiveName "
              placeholder="请输入接收人姓名"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="receiveType">
            <el-select v-model="queryParams.receiveType" placeholder="请选择模板类型" style="width: 200px">
              <el-option
                v-for="dict in optionTypeList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-add-common-style-button"
             style="float: right;"
             @click="tableButton(1)"
             v-hasPermiRouter="['iot:alarmReceive:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="类型" prop="receiveType"  show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.receiveType == 1 ? '用户' : (
                scope.row.receiveType == 2 ? '部门' : (
                scope.row.receiveType == 3 ? '标签' : '其他'))}}
            </template>
          </el-table-column>
          <el-table-column label="标识" prop="receiveCode" width="200" show-overflow-tooltip/>
          <el-table-column label="姓名" prop="receiveName" width="200" show-overflow-tooltip/>
          <el-table-column label="状态" prop="state" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch
                @click.native="tableButton(5,scope.row)"
                class="switch captcha-img"
                :active-value="true"
                :inactive-value="false"
                active-color="#0183FA"
                inactive-color="#999"
                v-model="scope.row.state"
                active-text="启用"
                inactive-text="停用"
                disabled
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="300" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['iot:alarmReceive:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['iot:alarmReceive:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!--新增弹窗-->
    <el-dialog class="iot-receiver-dialog" :title='dialogTitle' width="540px" append-to-body
               :visible.sync="dialogType" v-if="dialogType" @close="dialogOff()"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="dialogForm" ref="dialogForm" :inline="true" :rules="dialogRules" class="addCheckPage-min" label-width="120px">
        <el-form-item label="类型：" prop="receiveType">
          <el-select v-model="dialogForm.receiveType" placeholder="请选择接收人类型" style="width: 320px">
            <el-option
              v-for="item in optionTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标识：" prop="receiveCode">
          <el-input v-model="dialogForm.receiveCode" placeholder="请输入接收人标识" maxLength="50" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="姓名：" prop="receiveName">
          <el-input v-model="dialogForm.receiveName" placeholder="请输入接收人姓名" maxLength="50" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="状态：" prop="state">
          <el-radio-group v-model="dialogForm.state">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogOff()">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
    <!--<add-page :propsData="propsData" v-if="pageType === 2"></add-page>-->
  </div>
</template>
<script>
  import { iotAlarmReceiveList,iotAlarmReceiveAdd,iotAlarmReceiveUpdate,iotAlarmReceiveDelete } from "@/api/iotDevice/index";
  //import addPage from "./addPage.vue";
  export default {
    name: 'index',
    //components: {
    //  addPage
    //},
    data () {
      return {
        tableButtonType:this.hasPermiDom(['iot:alarmReceive:edit','iot:alarmReceive:del']),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          receiveName:"",
          receiveType :null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //弹窗数据
        dialogTitle:'',
        dialogType:false,
        dialogForm:{
          receiveType:'',
          receiveCode:'',
          receiveName:'',
          state:true,
        },
        dialogRules:{
          receiveType: [
            { required: true, message: "请选择类型", trigger: "blur" },
            { required: true, message: "请选择类型", validator: this.spaceJudgment, trigger: "blur" }
          ],
          receiveCode: [
            { required: true, message: "请输入标识", trigger: "blur" },
            { required: true, message: "请输入标识", validator: this.spaceJudgment, trigger: "blur" }
          ],
          receiveName: [
            { required: true, message: "请输入姓名", trigger: "blur" },
            { required: true, message: "请输入姓名", validator: this.spaceJudgment, trigger: "blur" }
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
            { required: true, message: "请选择状态", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
        optionTypeList:[
          {
            value:1,
            label:'用户'
          },
          {
            value:2,
            label:'部门'
          },
          {
            value:3,
            label:'标签'
          },
        ]
      }
    },
    created () {

    },
    mounted () {
      this.getList();
    },
    methods: {
      //弹窗关闭
      dialogOff(){
        this.$set(this,'dialogForm',{
          receiveType:'',
          receiveCode:'',
          receiveName:'',
          state:true,
          }
        );
        this.$set(this,'dialogType',false);
      },
      //弹窗提交
      dialogSubmit(){
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            let obj = JSON.parse(JSON.stringify(this.dialogForm))
            if(obj.receiveId){
              //编辑
              iotAlarmReceiveUpdate(obj).then(response => {
                this.msgSuccess(response.message)
                this.dialogOff();
                this.getList();
              });
            }else{
              //新增
              iotAlarmReceiveAdd(obj).then(response => {
                this.msgSuccess(response.message)
                this.dialogOff();
                this.getList();
              });
            }
          }
        })
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          receiveName:"",
          receiveType :null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        iotAlarmReceiveList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$set(this,'dialogTitle','新增');
          this.$set(this,'dialogType',true);
        }else if(type == 3){
          //编辑
          this.$set(this,'dialogTitle','编辑');
          this.$set(this,'dialogForm',row);
          this.$set(this,'dialogType',true);
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotAlarmReceiveDelete({receiveId:row.receiveId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //启用&停用
          let text = row.state  ? "停用" : "启用";
          this.$confirm('是否确认' + text + '?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            let obj = JSON.parse(JSON.stringify(row));
            obj.state = !obj.state;
            iotAlarmReceiveUpdate(obj).then(response => {
              this.msgSuccess(response.message)
              this.getList();
            });
          }).catch(() => {});
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
    },
  }
</script>
<style scoped lang="scss">
  .receiver{
    .receiverPage{

    }
  }
</style>
