<template>
  <div class="total-wrapper">
    <div class="tools-wrapper">
      <el-button type="primary" @click="addInfoDialogShow">增加</el-button>
      <!-- <el-button type="warning" :disabled="isEditable" @click="editInfo">编辑</el-button> -->
      <!-- <el-button type="danger" :disabled="isDeleteable" @click="deleteInfo">删除</el-button> -->
    </div>
    <div class="tool-search-wrapper">
      <el-date-picker
        v-model="inputDate"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button type="primary" style="margin-left: 40px;" @click="searchInfo">查询</el-button>
    </div>
    <div class="info-total-wrapper">
      <el-table
        :data="infoList"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >

        <el-table-column
          prop="title"
          label="标题"
          width="1200"
        />
        <el-table-column
          prop="time"
          label="时间"
        />
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="curPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalColumn"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      title="新增消息通知"
      :visible.sync="addFormVisible"
    >
      <el-form
        ref="addform"
        :model="formInfo"
        :rules="rules"
      >
        <el-form-item label="标题" prop="title" :label-width="'120px'">
          <el-input v-model="formInfo.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="发布时间" prop="time" :label-width="'120px'">
          <el-date-picker
            v-model="formInfo.time"
            type="datetime"
            placeholder="选择发布时间"
          />
        </el-form-item>
      </el-form>
      <div class="way-to-publish">
        <el-checkbox v-model="miniProWay" style="margin-left: 120px;">小程序</el-checkbox>
        <el-checkbox v-model="shortMessage" style="margin-left: 30px;">短信</el-checkbox>
      </div>
      <div slot="footer">
        <el-button type="primary" @click="publishInfo">发布</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑消息通知"
      :visible.sync="editFormVisible"
    >
      <el-form
        ref="editform"
        :model="editFormInfo"
        :rules="rules"
      >
        <el-form-item label="标题" prop="title" :label-width="'120px'">
          <el-input v-model="editFormInfo.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="发布时间" prop="time" :label-width="'120px'">
          <el-date-picker
            v-model="editFormInfo.time"
            type="datetime"
            placeholder="选择发布时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitModify">提交</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="查询结果"
      :visible.sync="searchTableVisible"
    >
      <el-table
        :data="searchInfoList"
        style="width: 100%"
        border
      >

        <el-table-column
          prop="title"
          label="标题"
          width="300"
        />
        <el-table-column
          prop="time"
          label="时间"
        />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      infoList: [
        {
          title: '[隐患预警]贵公司"重大隐患" 一 "联动台紧急停止按钮无效"整改即将到期，请及时整改',
          time: '2020-05-19 19:25'
        },
        {
          title: '[隐患预警]贵公司"一般隐患" 一 "力矩限位不灵敏"整改即将到期，请及时整改',
          time: '2020-05-19 19:24'
        }
      ],
      searchInfoList: [
        {
          title: '[隐患预警]贵公司"重大隐患" 一 "联动台紧急停止按钮无效"整改即将到期，请及时整改',
          time: '2020-05-19 19:25'
        },
        {
          title: '[隐患预警]贵公司"一般隐患" 一 "力矩限位不灵敏"整改即将到期，请及时整改',
          time: '2020-05-19 19:24'
        }
      ],
      multipleSelection: [],
      isEditable: true,
      isDeleteable: true,
      addFormVisible: false,
      editFormVisible: false,
      searchTableVisible: false,
      formInfo: {
        title: '',
        time: ''
      },
      editFormInfo: {
        title: '',
        time: ''
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }
        ],
        time: [
          {
            required: true,
            message: '请选择发布时间',
            trigger: 'change'
          }
        ]
      },
      inputDate: '',
      miniProWay: false,
      shortMessage: false,
      currentPage: 1,
      curPageSize: 10,
      totalColumn: 40
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.isEditable = true
      this.isDeleteable = true
      if (this.multipleSelection.length === 1) {
        this.isEditable = false
      }
      if (this.multipleSelection.length > 0) {
        this.isDeleteable = false
      }
    },
    publishInfo() {
      this.$refs['addform'].validate((valid) => {
        if (valid) {
          // 调用 valid
        } else {
          this.$message.error('请输入信息')
        }
      })
    },
    addInfoDialogShow() {
      // addInfo
      this.addFormVisible = true
    },
    editInfo() {
      console.log(this.multipleSelection, '112233')
      this.editFormVisible = true
      this.editFormInfo = { ...this.multipleSelection[0] }
    },
    deleteInfo() {
      console.log(this.multipleSelection, '445566')
      this.$confirm('此操作将删除选中消息', '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    submitModify() {
      // axios
      this.editFormInfo = false
    },
    searchInfo() {
      // 查询
      // 这边可能要弹出 dialog
      this.searchTableVisible = true
    },
    handleSizeChange(val) {
      // 每页多少条
      this.curPageSize = val
    },
    handleCurrentChange(val) {
      // 当前是多少页
      this.currentPage = val
    }
  }
}
</script>

<style lang="scss">
  .tool-search-wrapper {
    margin: 22px 0;
  }
</style>
