<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">机械臂机台号</label>
        <el-input v-model="query.code" clearable placeholder="机械臂机台号" style="width: 185px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"/>
        <label class="el-form-item-label">ip地址</label>
        <el-input v-model="query.ip" clearable placeholder="ip地址" style="width: 185px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"/>
        <label class="el-form-item-label">地标点</label>
        <el-input v-model="query.modbusTcpPoint" clearable placeholder="地标点" style="width: 185px;"
                  class="filter-item" @keyup.enter.native="crud.toQuery"/>
        <rrOperation :crud="crud"/>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name"/>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="600px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="140px">
          <el-form-item label="机械臂机台号" prop="code">
            <el-input v-model="form.code" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="ip地址" prop="ip">
            <el-input v-model="form.ip" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="任务名称">
<!--            <el-input v-model="form.taskName" style="width: 370px;"/>-->
            <el-select v-model="form.taskName" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="端口号" prop="port">
            <el-input v-model="form.port" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="通信类型" prop="communicationType">
            <el-input v-model="form.communicationType" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="任务是否启用">
            <!--            <el-input v-model="form.modbusTcpTaskFinish" style="width: 370px;" />-->
            <el-switch
              v-model="form.modbusTcpTaskFinish"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item label="地标点">
            <el-input v-model="form.modbusTcpPoint" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="物料">
<!--            <el-input v-model="form.modbusTcpItem" style="width: 370px;"/>-->
            <el-select v-model="form.modbusTcpItem" placeholder="请选择">
              <el-option
                v-for="item in iteamOptionsData"
                :key="item.id"
                :label="item.code"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="从机ID">
            <el-input v-model="form.modbusTcpSlaveid" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="是否启用">
            <el-switch
              v-model="form.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
          <!--          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU" v-if="OKOnOff">确认</el-button>-->
          <!--          <el-button type="danger" v-if="!OKOnOff" @click="OKM(form)">确认</el-button>-->
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small"
                style="width: 100%;"
                height="68vh" @selection-change="crud.selectionChangeHandler">
        <el-table-column show-overflow-tooltip align="center" type="selection" width="55"/>
        <el-table-column show-overflow-tooltip align="center" width="100px" prop="code" label="机械臂机台号"/>
        <el-table-column show-overflow-tooltip align="center" width="100px" prop="ip" label="ip地址"/>
        <el-table-column prop="taskName" label="任务名称"/>
        <el-table-column :show-overflow-tooltip="true" prop="readValue" label="读取的值" />
        <el-table-column :show-overflow-tooltip="true" prop="errorTxt" label="异常日志" />
        <el-table-column show-overflow-tooltip align="center" width="180px" prop="modbusTcpTaskFinish"
                         label="任务是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.modbusTcpTaskFinish"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" width="180px" prop="modbusTcpPoint" label="地标点"/>
        <el-table-column show-overflow-tooltip align="center" width="180px" prop="modbusTcpItem" label="物料"/>
        <el-table-column prop="enabled" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" prop="port" label="端口号"/>
        <el-table-column show-overflow-tooltip align="center" prop="communicationType" label="通信类型"/>
        <el-table-column show-overflow-tooltip align="center" prop="address" label="地址"/>
        <el-table-column prop="modbusTcpSlaveid" label="从机ID"/>
        <!--        <el-table-column  show-overflow-tooltip align="center" prop="dataLength" label="数据长度" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="120px" prop="offsetA1" label="偏移量-A返回空车" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="120px" prop="offsetB1" label="偏移量-B返回空车" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="100px" prop="offsetA2" label="偏移量-A叫料" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="100px" prop="offsetB2" label="偏移量-B叫料" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="120px" prop="offsetA3" label="偏移量-A车有料" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="120px" prop="offsetB3" label="偏移量-B车有料" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="120px" prop="pointA1" label="A车的地标点" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" width="120px" prop="pointB1" label="B车的地标点" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" prop="itemA1" label="A车的物料" />-->
        <!--        <el-table-column  show-overflow-tooltip align="center" prop="itemB1" label="B车的物料" />-->

        <el-table-column show-overflow-tooltip align="center" prop="createBy" label="创建人"/>
        <el-table-column show-overflow-tooltip align="center" prop="updateBy" label="更新人"/>
        <el-table-column show-overflow-tooltip align="center" width="140px" prop="createTime" label="创建时间"/>
        <el-table-column show-overflow-tooltip align="center" width="140px" prop="updateTime" label="更新时间"/>
        <!--        <el-table-column show-overflow-tooltip  align="center" width="140px" prop="taskOffOnA" label="a车点位是否有任务" />-->
        <!--        <el-table-column show-overflow-tooltip  align="center" width="140px" prop="taskOffOnB" label="b车点位是否有任务" />-->
        <el-table-column v-if="checkPer(['admin','zcjxb:edit','zcjxb:del'])" label="操作" width="150px" align="center"
                         fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudZcjxb from '@/api/zcjxb'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getItemsList} from "@/api/item";

const defaultForm = {
  id: null,
  code: null,
  ip: null,
  port: null,
  communicationType: 'MODBUSTCP',
  address: null,
  dataLength: null,
  offsetA1: null,
  offsetB1: null,
  offsetA2: null,
  offsetB2: null,
  offsetA3: null,
  offsetB3: null,
  pointA1: null,
  pointB1: null,
  itemA1: null,
  itemB1: null,
  enabled: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  modbusTcpTaskFinish: null,
  modbusTcpPoint: null,
  modbusTcpItem: null,
  taskOffOnA: null,
  taskOffOnB: null,
  modbusTcpSlaveid: null,
  taskName: null,
  readValue: null, errorTxt: null
}
export default {
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({title: 'zcjxb', url: 'api/zcjxb', idField: 'id', sort: 'id,desc', crudMethod: {...crudZcjxb}})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'zcjxb:add'],
        edit: ['admin', 'zcjxb:edit'],
        del: ['admin', 'zcjxb:del']
      },
      rules: {
        code: [
          {required: true, message: '机械臂机台号不能为空', trigger: 'blur'}
        ],
        ip: [
          {required: true, message: 'ip地址不能为空', trigger: 'blur'}
        ],
        port: [
          {required: true, message: '端口号不能为空', trigger: 'blur'}
        ],
        communicationType: [
          {required: true, message: '通信类型不能为空', trigger: 'blur'}
        ],
        address: [
          {required: true, message: 'DB地址不能为空', trigger: 'blur'}
        ]
      },
      queryTypeOptions: [
        {key: 'ip', display_name: 'ip地址'},
        {key: 'code', display_name: '机械臂机台号'},
        {key: 'modbusTcpPoint', display_name: 'Modbus-tcp对应的地标点'}
      ],
      OKOnOff: false,
      iteamOptionsData:[],
      options: [{
        value: '返空车',
        label: '返空车'
      },{
        value: '无任务',
        label: '无任务'
      },{
        value: '叫料',
        label: '叫料'
      }]
    }
  },
  mounted() {
    //加载轴承卓的物料数据
    this.getZCItem();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    OKM(obj) {
      let string_ = null;
      let oo = {
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      };
      for (let k in obj) {
        string_ = string_ + k + "==>" + obj[k] + "\n";
      }
      oo.text = string_;
      const loading = this.$loading(oo);
      setTimeout(() => {
        loading.close();
      }, 2000);
      this.OKOnOff = true;
    },
    getZCItem() {
      getItemsList({name: '轴承座'}).then(res => {
        this.iteamOptionsData=res;
      }).catch(e => {
        this.$message.error('物料数据获取失败');
      })
    }
  }
}
</script>

<style scoped>

</style>
