<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container"  v-loading="this.loading_add" element-loading-text="拼命加载中" element-loading-background="rgba(0, 0, 0, 0.1)">
      <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.srcAreaId" 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.dstAreaId" 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.status" 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-button
          slot="right"
          class="filter-item"
          type="success"
          size="mini"
          v-permission="permission.countCheckBtn"
          :disabled="crud.selections.length === 0"
          @click="countCheckBtn(crud.selections)"
        >
          数据审核
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          size="mini"
          v-permission="permission.countFinishBtn"
          :disabled="crud.selections.length === 0"
          @click="countFinishBtn(crud.selections)"
        >
          移位完成
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <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="80px">
          <el-form-item label="出库库区" prop="srcArea">
          <AreaSelect v-model="form.srcArea" prop="srcArea" value-key="id" style="width: 250px"   class="filter-item"   clearable filterable :bexb="null">
          </AreaSelect>
          </el-form-item>
          <el-form-item label="目标库区" prop="dstArea">
            <!--            <el-input v-model="form.area" style="width: 370px;"/>-->
            <el-select v-model="form.dstArea" prop="dstArea" value-key="id" style="width: 250px"  @change="getPointLists(form.dstArea.name)" clearable filterable>
              <el-option

                v-for="item in areaOptions"
                :key="item.id"
                :label="item.code"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="目标库位" prop="dstPoint">
            <el-select v-model="form.dstPoint" prop="dstPoint" value-key="id"  filterable
                       placeholder="请选择" style="width: 250px;"
            >
              <el-option
                v-for="item in pointLists"
                :key="item.id"
                :label="item.code"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.type" style="width: 250px;"  filterable placeholder="请选择">
              <el-option
                v-for="item in dict.count_type"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 250px;" />
          </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="id" label="序号" width="50"/>
        <el-table-column prop="code" label="代码" width="150" />
        <el-table-column prop="srcArea.code" label="移出库区" />
        <el-table-column prop="dstArea.code" label="目标库区" />
        <el-table-column prop="dstPoint.code" label="目标库位" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.count_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            {{ dict.label.count_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="orderQty" label="订单数量" />
        <el-table-column prop="countQty" label="移位数量" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column v-if="checkPer(['admin','countMove:edit','countMove:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button
                class="filter-item"
                type="primary"
                size="mini"
                style="float: right"
                @click="showCountMoveDetail(scope.row)"
            >
              明细
            </el-button>
            <udOperation
                :data="scope.row"
                :permission="permission"
            >
            </udOperation>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCountMove,{countFinish,countCheck} from '@/api/countMove'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'
import {queryAreaList} from "@/api/area";
import {queryPointList} from "@/api/point";
import {generatePrepFlag} from "@/api/pickInstruction";
import AreaSelect from "@/views/generic-component/AreaSelect.vue";

const defaultForm = { id: null, code: null, name: null, srcArea: null, dstArea: null, dstPoint: null,  type: null,  remark: null, sourceName: null, sourceId: null,}
export default {
  name: 'CountMove',
  components: {AreaSelect, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['count_status', 'count_type'],
  cruds() {
    return CRUD({ title: '部品移位单', url: 'api/countMove', idField: 'id', sort: 'id,desc', crudMethod: { ...crudCountMove }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'countMove:add'],
        edit: ['admin', 'countMove:edit'],
        del: ['admin', 'countMove:del'],
        countCheckBtn: ['admin', 'countMove:countCheckBtn'],
        countFinishBtn: ['admin', 'countMove:countFinishBtn']
      },
      rules: {
      },
      areaOptions: [],
      pointLists:[],
        loading_add:false,
      queryTypeOptions: [
        { key: 'code', display_name: '代码' },
        { key: 'srcAreaId', display_name: '移出库区' },
        { key: 'dstAreaId', display_name: '目标库区' },
        { key: 'status', display_name: '状态' }
      ]
    }
  },
  mounted() {
    //初始化数据
    this.getAreaOptionsAll();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    //获取库区数据
    getAreaOptionsAll() {
      queryAreaList({bexb:false,enabled:true}).then(res => {
        this.areaOptions = res
      })
    },
    getPointLists(areaName) {
      queryPointList({"areaName":areaName}).then(res => {
        this.pointLists = res
      })
    },
    countCheckBtn(data){
      this.loading_add=true;
      //选着的数量
      let data_ = [];
      for (const datum of data) {
        data_.push(datum.id)
      }
      countCheck(data_).then(res=>{
        this.crud.toQuery()
        this.loading_add=false;
        this.$message.success("审核完成")

      },e=>{
          this.loading_add=false;
      })
    },
    countFinishBtn(data){
      this.loading_add=true;
      //选着的数量
      let data_ = [];
      for (const datum of data) {
        data_.push(datum.id)
      }
      countFinish(data_).then(res=>{
        this.crud.toQuery()
        this.loading_add=false;
        this.$message.success("移位完成")
      },e=>{
          this.loading_add=false;
      })
    },
      showCountMoveDetail(datas){
          this.$router.push({path:'/business-inv/showCountMoveDetail',query: {ids:datas.id}})
      },
  }
}
</script>

<style scoped>

</style>
