<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">所属库区</label>
        <el-input v-model="query.area" 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="120px">
          <el-form-item label="编码" prop="code">
            <el-input v-model="form.code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="库区" prop="area">
            <el-select v-model="form.area" value-key="id" filterable placeholder="请选择" style="width: 370px;">
              <el-option
                  v-for="item in areas"
                  :key="item.id"
                  :label="item.name"
                  :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="上料接驳口数" prop="upNumber">
            <el-input v-model.number="form.upNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="上料接驳口" prop="upPoint">

            <el-select
                v-model="form.upPoint"
                value-key="id"
                style="width: 370px;"
                multiple
                filterable
                remote
                reserve-keyword
                placeholder="请选择"
                :remote-method="remoteMethod"
                :loading="loading">
              <el-option
                  v-for="item in points"
                  :key="item.id"
                  :label="item.code"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="下料接驳口数" prop="downNumber">
            <el-input v-model.number="form.downNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="下料接驳口" prop="downPoint">
            <el-select
                v-model="form.downPoint"
                value-key="id"
                style="width: 370px;"
                multiple
                filterable
                remote
                reserve-keyword
                placeholder="请选择"
                :remote-method="remoteMethod"
                :loading="loading">
              <el-option
                  v-for="item in points"
                  :key="item.id"
                  :label="item.code"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="接驳口车辆数">
            <el-input v-model="form.agvNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="输送线最大容量">
            <el-input v-model="form.maxNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="输送线返库料箱数">
            <el-input v-model="form.backNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="最大料箱任务数">
            <el-input v-model="form.maxStockNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description" :rows="3" type="textarea" style="width: 370px;" />
          </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>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" :height="crud.tableHeight" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="code" label="编码" />
        <el-table-column prop="areaCode" label="所属库区">
          <template slot-scope="scope">
            {{ scope.row.area.code}}
          </template>
        </el-table-column>
        <el-table-column prop="upNumber" label="上料接驳口数" />
        <el-table-column prop="upPointCode" :show-overflow-tooltip="false" label="上料接驳口">
          <template slot-scope="scope">
            <div class="tag-group">
              <el-tag
                  v-for="item in scope.row.upPoint"
                  key="item.code"
                  :type="info"
                  effect="plain">
                {{ item.code }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="downPointCode" :show-overflow-tooltip="false" label="下料接驳口">
          <template slot-scope="scope">
            <div class="tag-group">
              <el-tag
                  v-for="item in scope.row.downPoint"
                  key="item.code"
                  :type="info"
                  effect="plain">
                {{ item.code }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="agvNumber" label="接驳口车辆数" />
        <el-table-column prop="maxNumber" label="输送线最大容量" />
        <el-table-column prop="backNumber" label="输送线返库料箱数" />
        <el-table-column prop="maxStockNumber" label="最大料箱任务数" />
        <el-table-column prop="description" label="描述" />
        <el-table-column v-if="checkPer(['admin','workPoint:edit','workPoint:del'])" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudWorkPoint from '@/api/workPoint'
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 {queryAreaList} from "@/api/area";
import {getPoints} from "@/api/point";

const defaultForm = { id: null, code: null, name: null, area: null, upNumber: null, upPoint: [], downNumber: null, downPoint: [], agvNumber: null, maxNumber: null, backNumber: null, maxStockNumber: null, deptId: null, description: null, enabled: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'WorkPoint',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['base_staus'],
  cruds() {
    return CRUD({ title: '工作站', url: 'api/workPoint', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWorkPoint }})
  },
  data() {
    return {
      areas: [],
      points: [],
      permission: {
        add: ['admin', 'workPoint:add'],
        edit: ['admin', 'workPoint:edit'],
        del: ['admin', 'workPoint:del']
      },
      rules: {
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '所属库区不能为空', trigger: 'blur' }
        ],
        upNumber: [
          { required: true, message: '上料接驳口数格式错误', trigger: 'blur', type: 'number' }
        ],
        upPoint: [
          { required: true, message: '上料接驳口不能为空', trigger: 'blur' }
        ],
        downNumber: [
          { required: true, message: '下料接驳口数格式错误', trigger: 'blur', type: 'number' }
        ],
        downPoint: [
          { required: true, message: '下料接驳口不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'code', display_name: '编码' },
        { key: 'area', display_name: '所属库区' }
      ]
    }
  },
  mounted() {
    this.getArea()
    this.getPoint()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getArea() {
      queryAreaList({ enabled: true }).then(res => {
        this.areas = res
      })
    },
    getPoint() {
      getPoints(null).then(res => {
        this.points = res
      })
    },
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          getPoints(query).then(res => {
            this.points = res
          })
        }, 200);
      } else {
        this.points = [];
      }
    }
  }
}
</script>

<style scoped>

</style>
