<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">编号</label>
        <el-input v-model="query.code" clearable placeholder="请输入编号" style="width: 120px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"/>

        <label class="el-form-item-label">物料代码</label>
        <el-input v-model="query.itemCode" clearable placeholder="请输入物料代码" style="width: 120px;"
                  class="filter-item"
                  @keyup.enter.native="crud.toQuery"/>

        <label class="el-form-item-label">点位编码</label>
        <el-input
          v-model="query.pointCode"
          clearable
          placeholder="请输入点位编码"
          style="width: 120px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"/>

        <label class="el-form-item-label">是否启用</label>
        <el-select v-model="query.enabled" clearable placeholder="请选择" style="width: 120px;" class="filter-item"
                   @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="baseStatus in dict.base_staus"
            :key="baseStatus.id"
            :label="baseStatus.label"
            :value="baseStatus.value"
          />
        </el-select>


        <label class="el-form-item-label">库区</label>
        <el-select v-model="query.areaName" @focus="getArea"   style="width: 120px;" filterable  clearable placeholder="请选择" @keyup.enter.native="crud.toQuery" class="filter-item"  @change="crud.toQuery">
          <el-option
            v-for="area in areas"
            :key="area.id"
            :label="area.name"
            :value="area.name"
          />
        </el-select>
        <rrOperation :crud="crud"/>

      </div>

      <crudOperation :permission="permission" :tableKey="this.$options.name">


        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
          size="mini"
          v-show="true"
          :loading="crud.delAllLoading"
          @click="bindOrder"
        >
          预装按钮盒绑定
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
          size="mini"
          v-show="false"
          :disabled="crud.selections.length !== 1"
          :loading="crud.delAllLoading"
          @click="bindOrder"
        >
          按钮盒呼叫
        </el-button>
      </crudOperation>
      <!--      预装绑定按钮盒弹窗界面-->
      <BindOrderNumber ref="bindOrderNumber"/>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="90px">
          <el-form-item label="编号">
            <el-input v-model="form.code" style="width: 370px;"/>
          </el-form-item>

          <el-form-item label="名称">
            <el-input v-model="form.name" style="width: 370px;"/>
          </el-form-item>

          <el-form-item label="点位" prop="point">
            <el-select v-model="form.point" @focus="getPoint" filterable placeholder="请选点位" value-key="id"
                       style="width: 370px;">
              <el-option
                v-for="point in points"
                :key="point.id"
                :label="point.code"
                :value="point"
                :disabled="point.status!='FREE'"
              >
                <span style="float: left">{{ point.code }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ point.area.name }}</span>

              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="物料" >
            <el-select v-model="form.item" @focus="getItem" filterable placeholder="请选择物料" value-key="id"
                       style="width: 370px;">
              <el-option
                v-for="item in items"
                :key="item.id"
                :label="item.code"
                :value="item"
                :disabled="!item.enabled"
              >
              <span style="float: left">{{ item.code}}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工单号">
            <el-input v-model="form.orderNumber" style="width: 370px;"/>
          </el-form-item>
          <!-- 新增ip、指示灯、按钮盒 -->
          <el-form-item label="IP">
            <el-input v-model="form.iP" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="指示灯状态">
              <el-select v-model="form.lampStatus"  filterable placeholder="请选择物料" style="width: 370px;">
                <el-option
                  v-for="item in this.dict.lamp_status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
               />
              </el-select>
          </el-form-item>
          <el-form-item label="按钮盒编号">
<!--            <el-input v-model="form.lampCode" style="width: 370px;"/>-->
            <el-select v-model="form.lampCode" clearable filterable placeholder="请选择编号" style="width: 370px;">
              <el-option
                v-for="item in this.dict.lamp_code"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
        @select="selectOneMethod"
        @select-all="selectAllMethod"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="xh" label="序号" type="index" width="50"/>
        <el-table-column prop="code" label="编号"/>
        <el-table-column prop="name" label="名称"/>
        <el-table-column :show-overflow-tooltip="true" prop="itemName" label="物料编码">
          <template slot-scope="scope">
            <div>{{ scope.row.item==null?"":scope.row.item.code }}</div>
          </template>
        </el-table-column>

        <el-table-column :show-overflow-tooltip="true" prop="pointName" label="点位编码">
          <template slot-scope="scope">
            <div>{{ scope.row.point.code }}</div>
          </template>
        </el-table-column>

        <el-table-column  label="点位库区"  prop="areaName">
          <template slot-scope="scope">
            <div>{{ scope.row.point.area.name }}</div>
          </template>
        </el-table-column>



        <el-table-column prop="orderNumber" label="工单号"/>
        <el-table-column prop="iP" label="IP"/>
        <el-table-column prop="lampStatus" label="指示灯状态"/>
        <el-table-column prop="lampCode" label="按钮盒编号"/>

        <el-table-column prop="enabled" label="启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.enabled)"
            />
          </template>
        </el-table-column>

        <el-table-column v-if="checkPer(['admin','box:edit','box:del'])" label="操作" width="60px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :show-dle="false"
            />
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          width="60px">
          <template slot-scope="scope">
            <el-button @click="callBox(scope.row)" type="primary" align="center" size="small">呼叫</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudBox from '@/api/box'
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"
import pointUrl, {queryPointList} from "@/api/point"
import {getAres, queryAreaList} from "@/api/area"
import BindOrderNumber from "@/views/base-data/box/bindOrderNumber.vue"
import DonMessage from '@/utils/message'

const defaultForm = {
  name:null,
  code:null,
  id: null,
  item: null,
  point: null,
  orderNumber:null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  iP:null,
  lampStatus:null,
  lampCode:null
}
export default {
  name: 'Box',
  components: {pagination, crudOperation, rrOperation, udOperation, BindOrderNumber},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['base_staus','lamp_status','lamp_code'],
  cruds() {
    return CRUD({
      title: '按钮盒',
      url: 'api/box',
      idField: 'id',
      sort: 'name',
      crudMethod: {...crudBox},
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: true,
        download: true
      }

    })
  },
  data() {
    return {
      items: [],
      points: [],
      pointsUpdate:[],
      pointsInsert:[],
      areas: [],
      params: {
        type: 'BOX',
        status:'FREE',
        enabled:1
      },
      permission: {
        add: ['admin', 'box:add'],
        edit: ['admin', 'box:edit'],
        del: ['admin', 'box:del']
      },
      CallBox: {
        id: ''
      },
      rules: {
        orderNumber: [
          {required: true, message: '工单号必填', trigger: 'blur'}
        ],
        item: [
          {required: true, message: '物料必填', trigger: 'blur'}
        ],
        point: [
          {required: true, message: '点位必填', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
    //编辑下拉出现内容
    this.getPoint();
    this.getItem();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getItem() {
      getItemsList({}).then(res => {
        this.items = res;
      })
    },
    getPoint() {
      queryPointList(this.params).then(res => {
        this.points =res;
        this.pointsInsert=res;
      })
      queryPointList({
        type: 'BOX',
        enabled:1
      }).then(res => {
         this.pointsUpdate=res;
      })
    },
    getArea() {
      queryAreaList({ }).then(res => {
        this.areas= res;
      })
    },
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.base_staus[val] + '" ' + data.code + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudBox.edit(data).then(res => {
          this.crud.notify(this.dict.label.base_staus[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.enabled = !data.enabled
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    },
    bindOrder() {
      this.$refs.bindOrderNumber.dialogVisible = true
    },
    //呼叫按钮
    callBox(data) {
      this.CallBox.id=data.id
      crudBox.callBox(this.CallBox).then(res => {
        if(res.code){
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.SUCCESS);
          this.$parent.shuaxinContainerList()
          this.$refs['form'].resetFields();
        }else{
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR);
        }
      })
    },
    //单选时触发
    selectOneMethod(selection, row){
    if (selection.length===1){
      this.points=this.pointsUpdate;
    }else {
      this.points=this.pointsInsert;
    }
    },
    //全选时触发
    selectAllMethod(selection){
      this.points=this.pointsInsert;
    }
  }
}
</script>

<style scoped>

</style>
