<template>
  <div class="app-container">


        <div class = "search-bx"  >
        <div class="search">
          <el-row :gutter="0">
            <el-col :span="21">


              <el-input v-model="tableObj.getdataListParm.parammaps.cname" placeholder="消息名词" style="width: 180px;" class="filter-item" clearable />


              <el-select v-model="tableObj.getdataListParm.parammaps.statusName" placeholder="发送方式" class="filter-item" style="width: 120px;" clearable>
                  <el-option v-for="item in statusNameList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>


            </el-col>
            <el-col :span="3">
                <el-button class="successBorder" @click="form_search">查询</el-button>
                <el-button class="successBorder" @click="form_clear">重置</el-button>
            </el-col>
          </el-row>
        </div>

    </div>
    开发中……


    <div class="table"  style="margin-top:140px">
      <el-table :key="tableObj.tableKey" v-loading="tableObj.listLoading" element-loading-text="给我一点时间" :data="tableObj.list" border fit highlight-current-row style="width: 100%;" :row-style="rowStyle" :cell-style="cellStyle" class="elTable table-fixed">

        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (tableObj.pageNum-1) * tableObj.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="消息名称" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.cname }}</span>
          </template>
        </el-table-column>
        <el-table-column label="触发条件" min-width="300px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发送方式" min-width="300px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>


        <el-table-column label="提醒对象" min-width="300px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>


        <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button v-if="isRoleEdit" class="miniSuccess" @click="form_edit(row)">设置</el-button>


          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="tableObj.total>=0" :total="tableObj.total" :page.sync="tableObj.getdataListParm.offset" :limit.sync="tableObj.getdataListParm.pagecount" @pagination="get_table_data" />
    </div>

    <!-- 新增/编辑 -->
    <el-dialog :title="textMap[create.dialogStatus]" :visible.sync="create.dialogFormVisible" :close-on-click-modal="false" width="40%">
      <div class="app-add">
        <el-form ref="temp" :rules="create.rules" :model="create.temp" label-position="right" label-width="150px" style="width: 80%;margin:0 auto 50px">

          <el-form-item label="消息名称：" prop="cname">
            <el-input ref="cname" v-model="create.temp.cname" disabled/>
          </el-form-item>

          <el-form-item label="触发条件：" prop="remark">
            <el-input ref="remark" v-model="create.temp.remark" />
          </el-form-item>

          <el-form-item label="发送方式：" prop="remark">
            <el-select v-model="create.temp.remark" placeholder="发送方式"  class="filter-item" style="width: 100%;"clearable>
              <el-option v-for="item in statusNameList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="发送机制：" prop="remark">
            <el-select v-model="create.temp.remark" placeholder="发送机制"  class="filter-item" style="width: 100%;"clearable>
              <el-option v-for="item in sendMechineList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="间隔周期：" prop="remark">
            <el-select v-model="create.temp.remark" placeholder="间隔周期"  class="filter-item" style="width: 100%;"clearable>
              <el-option v-for="item in statusNameList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelClose" @click="create.dialogFormVisible = false;get_table_data()">关闭</el-button>
          <el-button v-if="create.dialogStatus==='create' || create.dialogStatus==='update'" class="success" @click="create.dialogStatus==='create'?add_dialog_save():form_edit_save()">确认</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetDataByName, PostDataByName, failproccess, ExecDataByConfig, GetDataByNames, checkButtons, calculativeWidth } from '@/api/common'
import { isIntegerZero } from '@/utils/validate'
import Cookies from 'js-cookie'
import { parseTime } from '@/utils/index.js'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
export default {
  name: 'NoticeRemind',
  components: { Pagination },
  data() {
    return {

      isRoleEdit: [],

         // 下拉框 - 出差单
      statusNameList: [
        { "name": "立刻", "id": "立刻" },
        { "name": "延时", "id": "延时" },
        { "name": "指定周期", "id": "指定周期" }
      ],

      sendMechineList: [
        { "name": "单次", "id": "单次" },
        { "name": "指定循环次数", "id": "指定循环次数" },
        { "name": "循环至任务结束", "id": "循环至任务结束" }
      ],

      //表格请求参数~
      tableObj: {
        getdataListParm: {
          name: 'getCategory',
          page: 1,
          offset: 1,
          pagecount: 10,
          returntype: 'Map',
          parammaps: {
            cname: '',
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },

      // 新增/编辑
      create: {
        dialogFormVisible: false,
        dialogStatus: '',
        temp: {
          cname: '',
          remark: '',
          sort: '',
          enable: 1
        },
        rules: {
          cname: [{ type: 'string', required: true, message: '必填', trigger: 'blur' }],
          sort: [{ validator: isIntegerZero, trigger: 'blur' }]

        },
      },
      textMap: {
        create: '添加',
        update: '编辑'
      },

      requestParam: {},

      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },

  created() {
    this.get_table_data()

    // this.get_auto_buttons()
  },

  methods: {
    //获取按钮权限
    get_auto_buttons() {
      const Edit = 'DungScores'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },

    //加载表格
    get_table_data() {
      this.tableObj.listLoading = true
      console.log("this.tableObj.getdataListParm======>", this.tableObj.getdataListParm)
      GetDataByName(this.tableObj.getdataListParm).then(response => {
        console.log('table数据======>', response.data.list)
        if (response.data.list !== null) {
          for (var i = 0; i < response.data.list.length; i++) {
            response.data.list[i].enable = parseInt(response.data.list[i].enable)
          }

          this.tableObj.list = response.data.list
          this.tableObj.pageNum = response.data.pageNum
          this.tableObj.pageSize = response.data.pageSize
          this.tableObj.total = response.data.total
        } else {
          this.tableObj.list = []
        }
        setTimeout(() => {
          this.tableObj.listLoading = false
        }, 100)
      })
    },
    form_search() {
      console.log('点击了查询')

      this.tableObj.getdataListParm.offset = 1
      this.get_table_data()
    },
    form_clear() {
      console.log('点击了重置')
      this.tableObj.getdataListParm.parammaps.cname = ''
      this.tableObj.getdataListParm.offset = 1
      this.get_table_data()
    },

    // 添加
    form_reset() {
      this.create.temp = { cname: '', remark: '', sort: '', enable: 1 }
    },


    // 编辑
    form_edit(row) {
      console.log('点击了编辑', row)
      row.barid = String(row.barid)

      this.create.temp = Object.assign({}, row)
      this.create.temp.id = row.id
      this.create.dialogStatus = 'update'
      this.create.dialogFormVisible = true
    },

    // 编辑保存
    form_edit_save() {
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.requestParam.name = 'updateCategory'
          this.requestParam.parammaps = this.create.temp
          PostDataByName(this.requestParam).then(response => {
            console.log('编辑保存发送参数', this.requestParam)
            if (response.msg !== 'fail') {
              this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
              this.create.dialogFormVisible = false
              this.get_table_data()
            } else {
              this.$notify({ title: '失败', message: '保存失败', type: 'error', duration: 2000 })
            }
          })
        }
      })
    },


    // 导出
    form_export(item) {
      if (item == 1) {
        console.log('点击了导出模板')
      } else {
        console.log('点击了导出数据')
      }
    },

    // 导入
    form_inport() {
      console.log('点击了导入')
    }

  }
}
</script>
<style lang="scss" scoped>
.search {
  padding-top: 10px;
  clear: both;
}
.table {
  margin-top: 10px;
}
</style>
