<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.ddbh" 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.fzh" 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.itemCode" clearable placeholder="品番" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker
          v-model="query.nrDate"
          start-placeholder="nrDateStart"
          end-placeholder="nrDateStart"
          class="date-item"
        />
        <date-range-picker
          v-model="query.createTime"
          start-placeholder="createTimeStart"
          end-placeholder="createTimeStart"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-upload
          class="upload-demo"
          ref="upload"
          slot='right'
          style="float: right;padding-left: 5px"
          :action="baseApi+'/api/importData/importXppPlan'"
          :file-list="fileList"
          :on-success=handleSuccess
          :on-error="handleError"
          :headers="headers"
          :show-file-list="true">
          <el-button size="mini" type="success"  v-permission="permission.import" icon="el-icon-upload">导入</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="80px">
          <el-form-item label="纳入数">
            <el-input v-model="form.nrs" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="定单编号">
            <el-input v-model="form.ddbh" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="分支号">
            <el-input v-model="form.fzh" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="创建日期">
            <el-date-picker v-model="form.nrDate" type="datetime" 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="id" label="ID" />
        <el-table-column prop="areaCode" label="工厂" />
        <el-table-column prop="itemCode" label="品番" />
        <el-table-column prop="ddbh" label="定单编号" />
        <el-table-column prop="fzh" label="分支号" />
        <el-table-column prop="nrs" label="纳入数" />
        <el-table-column prop="zf" label="受入受領ＮＯ" />
        <el-table-column prop="cjDate" label="创建时间" />
        <el-table-column prop="cjr" label="端末ＮＯ" />
        <el-table-column prop="nrDate" label="更新时间" />
        <el-table-column prop="createTime" label="导入时间" />
        <el-table-column v-if="checkPer(['admin','xppPlan:del'])" 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 crudXppPlan from '@/api/xppPlan'
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 { getToken } from '@/utils/auth'
import DonMessage from "@/utils/message";
import {mapGetters} from "vuex";

const defaultForm = { id: null, itemId: null, ac: null, nrs: null, ddbh: null, zf: null, fzh: null, nrDate: null, shQuantity: null, cjr: null, bhr: null, fhr: null, contents: null, relaSupplier: null, relaDoor: null, relaCode: null, relaType: null, relaQuantity: null, areaCode: null, itemCode: null, deptId: null, description: null, createBy: null, updateBy: null, createTime: null, updateTime: null, ewm: null }
export default {
  name: 'XppPlan',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'xppPlan', url: 'api/xppPlan', idField: 'id', sort: 'id,desc', crudMethod: { ...crudXppPlan }, optShow: {
        add: false,
        edit: false,
        del: true,
        reset: true,
        download: true
      }})
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      fileList: [],
      permission: {
        add: ['admin', 'xppPlan:add'],
        edit: ['admin', 'xppPlan:edit'],
        del: ['admin', 'xppPlan:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'ddbh', display_name: '定单编号' },
        { key: 'fzh', display_name: '分支号' },
        { key: 'itemCode', display_name: '品番' }
      ]
    }
  },
  methods: {
    handleErr(err, file, fileList) {

      let myError = err.toString()//转字符串
      myError = myError.replace('Error: ', '') // 去掉前面的" Error: "
      myError = JSON.parse(myError)//转对象
      DonMessage.error('' + myError['message'])
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    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()
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
      this.loading = false
    }
  }
}
</script>

<style scoped>

</style>
