<template>
  <d2-container>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="工序单元">
        <el-select
           v-model="formInline.workingSubclass"
           filterable
           clearable
           placeholder="工序单元"
           @change="getCodesByWorkingSubclass(formInline.workingSubclass)">
          <el-option
            v-for="workingSubclass in workingSubclasses"
            :key="workingSubclass"
            :value="workingSubclass">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="节点编码">
        <el-select
          v-model="formInline.code"
          filterable
          clearable
          placeholder="节点编码">
          <el-option
            v-for="code in codes"
            :key="code"
            :value="code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData">查询</el-button>
      </el-form-item>
    </el-form>
    <c-grid
      :data="data">
      <c-grid-column
        field="id"
        width="5%">
      序号
      </c-grid-column>
      <c-grid-column
        field="name"
        width="16.25%">
      节点名称
      </c-grid-column>
      <c-grid-column
        field="value"
        width="16.25%"
        sort="true">
      值
      </c-grid-column>
      <c-grid-column
        field="device_code"
        width="16.25%"
        sort="true">
      设备
      </c-grid-column>
      <c-grid-column
        field="batch"
        width="16.25%"
        sort="true">
      批次
      </c-grid-column>
      <c-grid-column
        field="capture_time"
        width="30%"
        sort="true">
      采集时间
      </c-grid-column>
    </c-grid>
  </d2-container>
</template>

<script>
export default {
  data () {
    return {
      formInline: {
        workingSubclass: '',
        code: ''
      },
      workingSubclasses: [],
      codes: [],
      data: []
    }
  },
  methods: {
    async getworkingSubclasses () {
      try {
        this.workingSubclasses = await this.$api.QUERY_WORKING_SUBCLASSES()
      } catch (e) {
        console.log(e)
      }
    },
    async getCodesByWorkingSubclass (workingSubclass) {
      try {
        this.codes = await this.$api.QUERY_CODES(workingSubclass)
      } catch (e) {
        console.log(e)
      }
    },
    async getData () {
      try {
        this.data = await this.$api.QUERY_NODE_DATA(this.formInline.workingSubclass, this.formInline.code)
      } catch (e) {
        console.log(e)
      }
    }
  },
  mounted () {
    this.getworkingSubclasses()
  }
}
</script>
