<template>
  <div class="app-container">
    <router-view />
    <div class="header boxShadow">
      <div class="header-title">协议管理</div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="协议类型">
          <!-- <el-select v-model="formInline.type" placeholder="活动区域" clearable>
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select> -->
          <el-input v-model="formInline.protocolType" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="qos类型">
          <el-select v-model="formInline.protocolQos" placeholder="请选择" clearable>
            <el-option label="至多一次" :value="0"></el-option>
            <el-option label="至少一次" :value="1"></el-option>
            <el-option label="仅有一次" :value="2"></el-option>
          </el-select>
          <!-- <el-input v-model="formInline.protocolQos" placeholder="请输入" clearable></el-input> -->
        </el-form-item>
        <el-form-item class="fr">
          <el-button @click="search">查询</el-button>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <van-list>
      <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
      <div class="list boxShadow" v-for="(item,index) in list" :key="index">
        <div class="list-main">

          <div class="list-content">
            <div class="list-title">{{ item.title}}</div>
            <van-row class="content-1">
              <van-col span="6">协议类型: <span>{{item.protocolType}}</span></van-col>
              <van-col span="6">链接地址: <span>{{item.protocolTopic}}</span></van-col>
              <van-col span="6">qos:
                <span v-if="item.protocolQos == 0">至多一次</span>
                <span v-if="item.protocolQos == 1">至少一次</span>
                <span v-if="item.protocolQos == 2">仅有一次</span>
                <!-- <span>{{item.protocolQos}} </span> -->
              </van-col>
            </van-row>
            <van-row class="content-1">
              <van-col span="6">创建人: <span>{{item.buildUser}} </span></van-col>
              <van-col span="6">创建时间: <span>{{item.buildTime}} </span></van-col>
              <van-col span="6"></van-col>
            </van-row>
          </div>
          <div class="list-btns">
            <!-- <el-button type="primary" @click="handleDetail">详情</el-button> -->
            <el-button type="primary" @click="handleEdit(item)">修改</el-button>
            <el-button type="primary" @click="handleDelete(item)">删除</el-button>
          </div>
        </div>

      </div>
    </van-list>
    <div class="pagination-container">
      <el-pagination class="right" background :current-page.sync="listQuery.page" :page-size="listQuery.limit" :page-sizes="[5, 10, 20, 30]" :total="listQuery.total" @size-change="sizeChange" @current-change="pageChange" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div>

    <el-dialog :title="diaTitle" width="30%" :visible.sync="dialogFormVisible" center>
      <el-form :model="diaForm" :label-width="formLabelWidth" ref="diaForm">
        <el-form-item label="协议类型" prop="protocolType">
          <el-input v-model="diaForm.protocolType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="协议链接" prop="protocolTopic">
          <el-input v-model="diaForm.protocolTopic" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="qos" prop="protocolQos">
          <el-select v-model="diaForm.protocolQos" placeholder="请选择" style="width:100%">
            <el-option label="至多一次" :value="0"></el-option>
            <el-option label="至少一次" :value="1"></el-option>
            <el-option label="仅有一次" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="protocolDesc">
          <el-input v-model="diaForm.protocolDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save()" v-if="isAdd">保 存</el-button>
        <el-button type="primary" @click="update()" v-if="!isAdd">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProtocolList, deleteProtocolItem, addProtocolItem, updateProtocolItem } from '@/api/equipConfig/protocolMannagement/index'

export default {
  filters: {

  },
  data () {
    return {
      notice: false,
      formInline: {
        protocolType: '',
        protocolQos: '',
      },
      list: [
        {
          title: '通风设备标签01', protocolType: '1001', protocolTopic: '45', buildTime: '2021-01-12', buildUser: '蓝牙'
        },

      ],
      loading: 'false',
      finished: 'false',
      isAdd: false,
      diaTitle:'新建协议',
      dialogFormVisible: false,
      diaForm: {
        orgCode: JSON.parse(localStorage.getItem("userInfo")).orgCode,
        buildUser: JSON.parse(localStorage.getItem("userInfo")).userName,
        protocolType: '',
        protocolTopic: '',
        protocolQos: '',
        protocolDesc: ''
      },
      formLabelWidth: '100px',
      listQuery: {
        condition: "",
        page: 1,
        limit: 10,
        total: 10,
      },
    }
  },
  created () {

  },
  mounted () {
    this.getList()
  },
  methods: {
    search () {
      this.getList()
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
    },
    getList () {
      let condition = {
        protocolType: this.formInline.protocolType.trim(),
        protocolQos: this.formInline.protocolQos,
      }
      let data = {
        pageIndex: this.listQuery.page,
        pageSize: this.listQuery.limit,
        condition: JSON.stringify(condition)
      }
      getProtocolList(data).then(res => {
        console.log(res, 'getProtocolList')
        if (res.code == 200) {
          this.list = res.data.records
          this.listQuery.limit = res.data.size
          this.listQuery.total = res.data.total
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleDelete (data) {
      console.log(data, 'data')
      this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProtocolItem(data.id).then(res => {
          // console.log(res, 'delete')
          if (res.code == 200) {
            // this.loading = false
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          } else {
            // this.loading = false
          }
        })
      }).catch(() => {
        // this.loading = false
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    save () {
      let data = JSON.parse(JSON.stringify(this.diaForm))
      addProtocolItem(data).then(res => {
        console.log(res, 'addProtocolItem')
        if (res.code == 200) {
          this.dialogFormVisible = false
          this.getList()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    update() {
      console.log(this.diaForm,'this.diaForm')
      if(!this.diaForm.buildUser){
        this.diaForm.buildUser = JSON.parse(localStorage.getItem("userInfo")).userName
      }
      let data = JSON.parse(JSON.stringify(this.diaForm))
      updateProtocolItem(data).then(res => {
        console.log(res,'updateProtocolItem')
        if(res.code == 200) {
          this.dialogFormVisible = false
          this.getList()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleAdd () {
      console.log(this.diaForm,'this.diaForm-add')

      if (this.diaForm.id) {
        console.log('修改后重置')
        this.$refs["diaForm"].resetFields()
        /* this.$nextTick(() => {
          this.$refs["diaForm"].resetFields()
        }) */
      }
      if(this.diaTitle != '新建协议') {
        this.diaTitle = '新建协议'
      }
      
      this.isAdd = true
      this.dialogFormVisible = true
    },
    
    handleDetail () {

    },
    handleEdit (data) {
      this.isAdd = false
      this.diaTitle = '修改协议'
      this.diaForm = JSON.parse(JSON.stringify(data))
      this.dialogFormVisible = true
    },
    sizeChange (v) {
      this.listQuery.limit = v
      console.log(`每页 ${v} 条`);
      this.list.splice(v, this.list.length - 1)
      this.getList()
    },
    pageChange (v) {
      this.listQuery.page = v
      console.log(`当前页: ${v}`);
      this.getList()
    },
  }
}
</script>
<style lang='scss' scoped>
.boxShadow {
  box-shadow: 5px -5px 10px -4px #efe3e3, -5px 5px 10px -4px #efe3e3;
}
::v-deep .van-notice-bar {
  font-weight: 600;
  padding: 0;
  cursor: pointer;
}
.header {
  background-color: #fff;
  border-radius: 15px;
  padding: 15px;
  // height: 160px;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .header-title {
    font-size: 14px;
    font-weight: 600;
    padding-left: 10px;
    border-left: 4px solid #5283e7;
  }
  .van-notice-bar {
    padding: 10px 0 10px 0;
  }
  .el-form {
    .el-form-item {
      margin-bottom: 0;
    }
    .fr {
      float: right;
    }
  }
}
.list {
  background-color: #fff;
  border-radius: 15px;
  margin-top: 10px;
  .list-main {
    padding-left: 15px;
    margin-top: 15px;
    display: flex;
    justify-content: space-between;
    .list-title {
      font-size: 15px;
      font-weight: 700;
      padding: 10px 0;
    }
    .list-content {
      width: 78%;
      padding: 10px 10px 10px 5px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .content-1 {
        font-size: 13px;
        display: flex;
        justify-content: space-between;
        margin-top: 10px;
        // padding: 5px 0;
        strong {
          color: #d24848;
        }
        .text-colorc {
          color: #b8b8b8;
        }
      }
      .content-1:nth-last-child(1) {
        padding-bottom: 10px;
      }
    }
    .list-btns {
      width: 22%;
      background-color: #edf2fd;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
    }
  }
}
</style>
