<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">
        <!-- 搜索 -->
        <el-input v-model="query.shAreaCode" 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 size="mini" type="success" slot="right" class="filter-item"  v-permission="permission.batchHy"
                   @click="batchHyBtn(crud.selections)"
        >自动耗用
        </el-button>
        <el-upload
          class="upload-demo"
          slot='right'
          style="float: right;padding-left: 5px"
          :headers="headers"
          :action="baseApi +'/api/automaticPlanning/importAutomaticPlanning'"
          :data="importData"
          :file-list="fileList"
          :on-error="handleErr"
          :on-success=" (response, file, fileList) => { return handleSuccess(response, file, fileList,crud);}"
          :show-file-list="true">
          <el-button size="mini" v-permission="permission.import" type="success" icon="el-icon-upload2">导入</el-button>
        </el-upload>
      </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="130px">
          <el-form-item label="指示纳所" prop="shArea">
            <!--            <el-input v-model="form.shArea" style="width: 370px;" />-->
            <el-select v-model="form.shArea" filterable placeholder="请选择指示纳所" style="width: 250px;" value-key="id">
              <el-option
                v-for="item in shAreaListoOptions"
                :key="item.id"
                :label="item.name"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计划日期">
          <el-date-picker v-model="form.jhDate" type="date" placeholder="选择日期">
          </el-date-picker>
          </el-form-item>
          <el-form-item label="上线耗用数量" >
            <el-input v-model="form.sxQuantity" style="width: 250px;"/>
          </el-form-item>
          <el-form-item label="手输IDNO">
            <el-checkbox v-model="form.isEnd"/>
          </el-form-item>
          <el-form-item label="结束IDNO" v-if="form.isEnd==true">
            <el-input v-model="form.endNum" style="width: 250px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.description"  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" size="small" style="width: 100%;"  show-summary
                :height="crud.tableHeight"   @selection-change="crud.selectionChangeHandler" border
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="序号" width="60px"/>
        <el-table-column prop="shArea.workingStation" label="工位" width="100px">
        </el-table-column>
        <el-table-column prop="shArea.name" label="指示纳所" width="200px">
        </el-table-column>
        <el-table-column prop="shArea.posX" label="排序号" width="60px" v-if="false"/>
        <el-table-column prop="sxQuantity" label="上线耗用数量" width="100px"/>
        <el-table-column prop="jhQuantity" label="实际耗用数量" width="100px"/>
        <el-table-column prop="jhDate" label="计划日期">
          <template slot-scope="scope">
            {{getFormatDate(scope.row.jhDate)}}
          </template>
        </el-table-column>
        <el-table-column prop="startNum" label="开始IDNO"  width="100px"/>
        <el-table-column prop="endNum" label="结束IDNO"  width="100px"/>
        <el-table-column prop="status" label="状态"/>
        <el-table-column prop="updateBy" label="更新人"/>
        <el-table-column :show-overflow-tooltip="true" prop="updateTime" label="修改日期" width="180px"/>
        <el-table-column v-if="checkPer(['admin','automaticPlanning:edit','automaticPlanning:del'])" label="操作"
                         width="150px" align="center"
        >
          <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 crudAutomaticPlanning, {batchHy, batchOrder} from '@/api/automaticPlanning'
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 { getAreas } from '@/api/area'
import crudProductionPlan from '@/api/productionPlan'
import { formatDate } from '@/utils/commonUtils'
import {hySpareParts} from "@/api/spareparts";
import {getToken} from "@/utils/auth";
import {mapGetters} from "vuex";

const defaultForm = {
  id: null,
  shArea: null,
  endNum:null,
  hyEndNum:null,
  fxQuantity: 0,
  sxQuantity: 0,
  jhQuantity: 0,
  hyQuantity: 0,
  batchNum:0,
  deptId: null,
  enabled: false,
  description: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  jhDate:null,
  isSx:false,
  isEnd:false
}
export default {
  name: 'ZdhyPlanning',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '耗用计划',
      url: 'api/automaticPlanning',
      idField: 'id',
      sort: 'shArea.posX,asc',
      crudMethod: { ...crudAutomaticPlanning },
      query: { isSx: false }
    })
  },
  data() {
    return {
      importData: {
        isSx: false
      },
      fileList: [],
      headers: { 'Authorization': getToken() },
      permission: {
        add: ['admin', 'zdhyPlanning:add'],
        edit: ['admin', 'zdhyPlanning:edit'],
        del: ['admin', 'zdhyPlanning:del'],
        batchHy: ['admin', 'zdhyPlanning:batchHy'],
        import: ['admin', 'zdhyPlanning:import']
      },
      selectIds: {
        ids: []
      },
      areaOptions: [],
      loading_add:false,
      rules: {
        shArea: [
          { required: true, message: '指示纳所不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'shAreaCode', display_name: '指示纳所' },
      ],
      shAreaListoOptions:[]
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  mounted() {
    this.areaCSH()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    areaCSH() {
      getAreas(1).then(res => {
        this.shAreaListoOptions=res;
      }).catch(e => {

      })
    },
    //批量叫料
    batchOrderBtn(data){
      this.loading_add=true;
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      batchOrder(data_.ids).then(res=>{
        this.$message.success(res);
        this.loading_add=false;
        this.crud.toQuery()
      },e=>{
        this.loading_add=false;
      })

    },
    //批量耗用
    batchHyBtn(data){
      this.loading_add=true;
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      batchHy(data_.ids).then(res=>{
        this.$message.success(res);
        this.loading_add=false;
        this.crud.toQuery()
      },e=>{
        this.loading_add=false;
      })

    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
    // 导入
    handleSuccess(response, file, fileList) {
      this.crud.notify('上传成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      this.$refs.upload.clearFiles()
      this.crud.status.add = CRUD.STATUS.NORMAL
      this.crud.resetForm()
      this.crud.toQuery()
    },
    handleErr(err, file, fileList) {
      let myError = err.toString()// 转字符串
      myError = myError.replace('Error: ', '') // 去掉前面的" Error: "
      myError = JSON.parse(myError)// 转对象
      this.crud.notify(myError['message'], CRUD.NOTIFICATION_TYPE.ERROR)
    },
  }
}
</script>

<style scoped>

</style>
