<template>
  <div class="device-management">
    <!-- 设备管理 -->
    <div class="box">
      <div class="box_item" v-for="item in boxList" :key="item.id">
        <p>{{item.name}}</p>
        <p>{{item.value}}</p>
      </div>
    </div>
    <!-- 查询条件 -->
    <div class="conditions">
      <el-form ref="searchForm" inline :model="searchForm" size="mini">
        <el-form-item label="注册时间：" prop="registerStartTime">
          <!-- <el-input v-model="searchForm.registerTime" placeholder=""></el-input> -->
          <el-date-picker
            v-model="searchForm.registerStartTime"
            type="date"
            placeholder="选择日期"
            style="width: 130px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="省市县：" prop="countyName">
          <!-- 改用组件 -->
          <!-- FIXME: 等待组件，到底是 countName 还是对应的编码不清楚，但一定使用选择下拉框 -->
          <el-input v-model="searchForm.countyName" placeholder="地县/编码"></el-input>
        </el-form-item>
        <el-form-item label="牧场：" prop="farmCode">
          <!-- <el-input v-model="searchForm.farm" placeholder="牧场名/牧场编号"></el-input> -->
          <!-- FIXME: 牧场数据还没有，el-option 的具体内容也没修正  -->
          <el-select v-model="searchForm.farmCode" style="width: 100px">
            <el-option
              v-for="item in farmList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="栋舍：" prop="pigsty">
          <el-input v-model="searchForm.pigsty" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="网络通信状态：" prop="networkStatus">
          <el-select v-model="searchForm.networkStatus" style="width: 100px">
            <el-option
              v-for="item in networkList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采集状态：" prop="acqStatus">
          <el-select v-model="searchForm.acqStatus" style="width: 100px">
            <el-option
              v-for="item in acqList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注销状态：" prop="canStatus">
          <el-select v-model="searchForm.canStatus" style="width: 100px">
            <el-option
              v-for="item in canList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编码：" prop="deviceCode">
          <el-input v-model="searchForm.deviceCode" placeholder=""></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 20px">
          <el-button plain @click="init">查询</el-button>
          <el-button plain type="info" @click="resetForm('searchForm')">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 按钮栏 -->
    <div class="title">
      <div class="title-left">
        <i class="el-icon-s-unfold"></i>
        <span>设备列表</span>
        <el-button plain size="mini" class="button-margin">批量上传</el-button>
        <el-button type="primary" plain size="mini" @click="handleAddOrEdit">新增设备</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      height="700"
      border
      stripe
      :default-sort="{prop: 'date', order: 'descending'}"
      size="mini"
      v-loading="dataListLoading">
      <el-table-column
        type="selection"
        width="40"
        align="center">
      </el-table-column>
      <el-table-column
        prop="id"
        label="注册ID"
        width="60"
        align="center">
      </el-table-column>
      <el-table-column
        prop="registerTime"
        label="注册时间"
        width="90"
        align="center">
      </el-table-column>
      <el-table-column
        prop="deviceCode"
        label="设备编码"
        width="80"
        align="center">
      </el-table-column>
      <el-table-column
        prop="countyName"
        label="省市县"
        align="center">
      </el-table-column>
      <el-table-column
        prop="countyCode"
        label="省市县编码"
        align="center">
      </el-table-column>
      <el-table-column
        prop="farmName"
        label="牧场名称"
        width="90"
        align="center">
      </el-table-column>
      <el-table-column
        prop="farmCode"
        label="牧场编码"
        width="90"
        align="center">
      </el-table-column>
      <el-table-column
        label="阶段"
        sortable
        width="70"
        align="center">
        <template slot-scope="scope">
          <span>{{ stageList[scope.row.stage - 1].label }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="pigpenName"
        label="栋舍"
        width="100"
        align="center">
      </el-table-column>
      <el-table-column
        prop="serverIp"
        label="上传服务器地址"
        width="105"
        align="center">
      </el-table-column>
      <el-table-column
        prop="port"
        label="端口"
        width="60"
        align="center">
      </el-table-column>
      <el-table-column
        prop="lastTime"
        label="最后上传时间"
        sortable
        align="center">
      </el-table-column>
      <el-table-column
        label="网络通信状态"
        width="70"
        align="center">
        <template slot-scope="scope">
          <span :class="{ 'normal': scope.row.networkStatus === 1, 'abnormal': scope.row.networkStatus === 0}">{{ networkList[1 - scope.row.networkStatus].label }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="采集状态"
        width="70"
        align="center">
        <template slot-scope="scope">
          <span :class="{ 'normal': scope.row.acqStatus === 1, 'abnormal': scope.row.acqStatus === 2}">{{ acqList[scope.row.acqStatus - 1].label }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="注销状态"
        width="70"
        align="center">
        <template slot-scope="scope">
          <span :class="{ 'normal': scope.row.canStatus === 1, 'logout': scope.row.canStatus === 0}">{{ canList[1 - scope.row.canStatus].label }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleCheck(scope.row)">查看</el-button>
          <el-button type="text" size="small" @click="handleAddOrEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleLogout(scope.row.id)">注销</el-button>
          <el-button type="text" size="small">删除</el-button>
        </template>
        <!-- TODO: 按钮 -->
      </el-table-column>
    </el-table>
    <table-footer
      :totals="totalPages"
      :size="pageSize"
      @sizeChange="sizeChange"
      @pageChange="pageChange">
    </table-footer>
    <!-- 新增或者编辑设备弹出框 -->
    <add-or-update ref="addOrUpdate" @refreshData="init"></add-or-update>
  </div>
</template>

<script>
import TableFooter from "../../components/TableFooter";
import addOrUpdate from './addOrUpdate.vue'
import {mapState} from 'vuex'
export default {
  name: "deviceAdmin",
  data() {
    return {
      boxList: [ // 设备管理的方框
        {
          id: 1,
          name: '采集设备总数',
          value: 2500,
        },
        {
          id: 2,
          name: '正常设备总数',
          value: 2498
        },
        {
          id: 3,
          name: '异常设备总数',
          value: 1
        },
        {
          id: 4,
          name: '注销设备总数',
          value: 1
        }
      ],
      farmList: [ // 查询条件 - 牧场选择
        // 
      ],
      searchForm: { // 查询条件
        farmCode: '',
        registerStartTime: '',
        registerEndTime: '',
        deviceCode: '',
        countyName: '',
        // farmName: '',
        pigsty: '',
        networkStatus: '',
        acqStatus: '',
        canStatus: '',
        // sortord: ''
      },
      pageSizeList: [ // 表格显示条数
        {
          value: 0,
          label: '显示条数'
        },
        {
          value: 10,
          label: '10条/页'
        },
        {
          value: 20,
          label: '20条/页'
        },
        {
          value: 50,
          label: '50条/页'
        }
      ],
      pageSize: 20, // 表格显示条数
      tableData: [], // 表格数据
      totalPages: 0, // 共 n 条数据
      pageIndex: 1, // 表格当前页数
      dataListLoading: true,
      stageList: [ // 状态
        {
          value: 1,
          label: '配怀'
        },
        {
          value: 2,
          label: '分娩'
        },
        {
          value: 3,
          label: '后备'
        },
        {
          value: 4,
          label: '空怀'
        },
        {
          value: 5,
          label: '公猪'
        }
      ],
      networkList: [ // 网络通信状态 —— 查询条件 + 表格通用
        {
          value: 1,
          label: '正常'
        },
        {
          value: 0,
          label: '异常'
        }
      ],
      acqList: [ // 采集状态 —— 查询条件 + 表格通用
        {
          value: 1,
          label: '正常'
        },
        {
          value: 2,
          label: '异常'
        }
      ],
      canList: [ // 注销状态 —— 查询条件 + 表格通用
        {
          value: 1,
          label: '正常'
        },
        {
          value: 0,
          label: '注销'
        }
      ]
    }
  },
  components: {
    TableFooter,
    addOrUpdate
  },
  computed: {
    ...mapState(['baseUrl'])
  },
  methods: {
    // 初始化
    init () {
      this.$http({
        url: this.$http.adornUrl(`${this.baseUrl}/manager/collectorregister/list`),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'farmCode': this.searchForm.farmCode || undefined,
          'registerStartTime': this.searchForm.registerStartTime || undefined,
          'registerEndTime': this.searchForm.registerEndTime || undefined,
          'deviceCode': this.searchForm.deviceCode || undefined,
          'countyName': this.searchForm.countyName || undefined,
          'farmName': this.searchForm.farmName || undefined,
          'acqStatus': this.searchForm.acqStatus || undefined,
          'sortord': this.searchForm.sortord || undefined
        }, false)
      }).then(async({data}) => {
        console.log(data);
        this.tableData = data.page.list
        this.totalPages = data.page.totalPage
        this.dataListLoading = false
      }).catch(() => {
        this.dataListLoading = false
      })
    },
    // 显示条数改变
    sizeChange (val) {
      this.pageSize = val
    },
    // 页数改变
    pageChange (val) {
      this.pageIndex = val
    },
    // 清空 el-form 
    resetForm(form) {
      this.$refs[form].resetFields()
    },
    // 点击新增设备 or 编辑按钮
    handleAddOrEdit (row) {
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    },
    // 查看按钮
    handleCheck (row) {
      console.log(row)
      // 组件间传值`/deviceInfo/${row.id}`
      this.$router.push({
        name: 'deviceInfo',
        params: {
          id: row.id,
          deviceCode: row.deviceCode
        }
      })
    },
    // 注销按钮
    handleLogout (id) {
      console.log(id);
      // TODO: 明天再做.
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style scoped>
.box{
  width: 100%;
  height: 140px;
  background-color: #F9F9F9;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 10px;
}
.box_item {
  width: 198px;
  height: 78px;
  border: 1px solid #ddd;
  text-align: center;
}
.box_item p {
  height: 39px;
  line-height: 39px;
  margin: 0;
}

/** 选择条件 */
.conditions {
  width: 100%;
  background-color: #fff;
  margin: 30px 0px 10px;
  border: 1px solid #ddd;
  padding: 20px;
  padding-bottom: 10px;
}

/** 设备列表 */
.title {
  background-color: #f3f3f3;
  height: 50px;
  line-height: 50px;
  border: 1px solid #ddd;
  font-size: 12px;
}
.title-left {
  margin-left: 20px;
  display: inline-block;
  width: 300px;
}
.title-left span {
  margin-left: 2px;
}
.button-margin {
  margin: 0 5px 0 20px;
}
.title-right {
  float: right;
}

/* 正常 */
.normal {
  color: #37c20ce1;
}
.abnormal {
  color: #ff0000d3;
}
.logout {
  color: #ffd900;
}
</style>
