<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="getdataListParm.parammaps.pastureName" style="width: 140px;" placeholder="牧场" class="filter-item">
        <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-select v-model="getdataListParm.parammaps.pastureNameIN" style="width: 140px;" clearable placeholder="调入牧场" class="filter-item">
        <el-option v-for="item in findAllPasture2" :key="item.id" :label="item.pastureName" :value="item.name" />
      </el-select>
      <el-input v-model="getdataListParm.parammaps.applyCode" placeholder="调拨单号" style="width: 200px;" class="filter-item" />
      <el-select v-model="getdataListParm.parammaps.statue" style="width: 140px;" clearable placeholder="调拨状态" class="filter-item">
        <el-option v-for="item in statues" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-date-picker ref="inputDatetime" v-model="getdataListParm.parammaps.inputDatetime" class="inputDatetime" type="datetimerange" style="width: 250px;top:-3px;" format="yyyy-MM-dd" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="form_search1">搜索</el-button>
      <div>

        <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="form_add">新增</el-button>
        <el-button v-waves class="filter-item" type="success" icon="el-icon-upload2" @click="handleDownload">导出</el-button>

        <el-button    class="filter-item" style="margin-left: 10px;" type="success" icon="el-icon-upload2" @click="sapUpload">SAP上传</el-button>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      element-loading-text="给我一点时间"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :row-style="rowStyle"
      :cell-style="cellStyle"
      class="elTable"
      @selection-change="change_table_selection"
      @current-change="handleCurrentChange"
      :max-height="myHeight"
    >
    <el-table-column type="selection" :selectable="checkboxInit" align="center" width="50" />
      <el-table-column label="序号" align="center" type="index" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + (pageNum-1) * pageSize + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调拨单号" min-width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.applyCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调出牧场" min-width="90px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.pastureName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调入牧场" min-width="80px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.pastureNameIN }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调拨人" min-width="80px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.emp }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调拨日期" sortable prop="creatTime" min-width="80px" align="center" />
      <el-table-column label="调拨状态" min-width="80px" align="center" :formatter="statue" />

      <el-table-column   prop="sapStatus" label="sap上传状态" min-width="80px" align="center">
              <template slot-scope="scope">
                <span v-show ="scope.row.sapStatus == 2">上传失败</span>
                <span v-show = "scope.row.sapStatus == 1">上传成功</span>
                <span v-show = "scope.row.sapStatus == 0 || typeof(scope.row.sapStatus)== 'undefined'">未上传</span>
              </template>
            </el-table-column>
      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="form_see(row)">查看</el-button>
          <el-button v-if="row.statue==2 && (row.pastureId == loginPastureid)" type="success" size="mini" style="width:70px;display:inline-block" @click="handleReturn(row)">确认退回</el-button>
          <el-button v-else type="success" size="mini" style="width:70px;display:none" @click="handleReturn(row)">确认退回</el-button>
          <el-button v-if="row.statue==4 && (row.inPastureId == loginPastureid)" type="success" size="mini" style="display:inline-block" @click="handleReject(row)">驳回</el-button>
          <el-button v-else type="success" size="mini" style="display:none" @click="handleReject(row)">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="get_table_data" />
    <!-- 弹出层新增or修改 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="90%">
      <div class="app-container">
        <el-form ref="createTemp" :rules="rules" :model="createTemp" label-position="right" label-width="100px" style="width: 90%;margin:0 auto;">
          <el-row>
            <el-col :span="8">
              <el-form-item label="调拨单号:" prop="applyCode">
                <el-input ref="applyCode" v-model="createTemp.applyCode" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="调出牧场：" prop="pastureId">
                <el-select v-model="createTemp.pastureId" placeholder="调出牧场" class="filter-item" style="width:100%">
                  <el-option
                    v-for="item in findAllPasture"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="调入牧场：" prop="inPastureId">
                <el-select v-model="createTemp.inPastureId" placeholder="调入牧场" class="filter-item" style="width:100%">
                  <el-option
                    v-for="item in findAllPasture2"
                    :key="item.id"
                    :label="item.pastureName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="调拨人：" prop="empId">
                <el-select v-model="createTemp.empId" placeholder="调拨人" class="filter-item" style="width:100%" @change="changeEmp">
                  <el-option
                    v-for="item in findAllEmploye"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="调拨日期：" prop="applyDate">
                <el-date-picker
                  v-model="createTemp.applyDate"
                  :picker-options="pickerOptions"
                  type="date"
                  placeholder="调拨日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  style="width:100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购组:" prop="groupName">
                <el-input ref="groupName" v-model="createTemp.groupName" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="备件：" prop="partCode">
                <el-autocomplete
                  v-model="createTemp.partCode"
                  value-key="name"
                  class="inline-input"
                  :fetch-suggestions="sparePartSearch"
                  placeholder="请输入备件编号或备件名称或备件规格"
                  style="width:100%"
                  @select="handleSelectSparePart"
                >
                  <template slot-scope="{ item }">
                    <b>备件编号：</b><div class="name" style="display: inline;">{{ item.partCode }}</div>&nbsp;
                    |  &nbsp;<b>备件名称：</b><span class="addr">{{ item.partName }}</span>&nbsp;
                    |  &nbsp;<b>备件规格：</b><span class="addr">{{ item.specification }}</span>
                  </template>
                </el-autocomplete>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          element-loading-text="给我一点时间"
          :data="listAdd"
          border
          fit
          highlight-current-row
          style="width: 100%;margin-bottom:30px"
          :row-style="rowStyle"
          :cell-style="cellStyle"
          class="elTable"
        >
          <el-table-column label="序号" align="center" type="index" width="50px" />
          <el-table-column label="备件编号" prop="partCode" align="center" min-width="90">
            <template slot-scope="scope">
              <span>{{ scope.row.partCode }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备件名称" prop="partName" align="center" min-width="90">
            <template slot-scope="scope">
              <span>{{ scope.row.partName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备件规格" prop="specification" min-width="80px" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.specification }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备件图片" prop="picpath" min-width="110px" align="center">
            <template slot-scope="scope">
              <el-popover placement="right" title="" trigger="hover">
                <img v-if="scope.row.picpath !== ''" :src="scope.row.picpath">
                <img v-if="scope.row.picpath !== ''" slot="reference" :src="scope.row.picpath" :alt="scope.row.srcpath" style="height: 100px;width:100px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="备件品牌" prop="brand" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.brand }}</span>
            </template>
          </el-table-column>
          <el-table-column label="计量单位" prop="unit" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.unit }}</span>
            </template>
          </el-table-column>
          <el-table-column label="供应商" prop="providerName" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.providerName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="现有库存" prop="reportery" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.reportery }}</span>
            </template>
          </el-table-column>
          <el-table-column label="调拨数量" min-width="120px" align="center" valign="middle">
            <template slot-scope="scope">
              <el-form :model="scope.row" :rules="rules">
                <el-form-item prop="amount">
                  <el-input v-model="scope.row.amount" style="margin-top:15px" />
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="单价" min-width="110px" align="center" valign="middle">
            <template slot-scope="scope">
              <span>{{ scope.row.price }}</span>
            </template>
          </el-table-column>
          <el-table-column label="总价" min-width="110px" align="center" valign="middle">
            <template slot-scope="scope">
              <span>{{ (scope.row.amount * scope.row.price) | keepTwoNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" min-width="110px" align="center" valign="middle">
            <template slot-scope="scope">
              <el-input v-model="scope.row.note" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="{row}">
              <a class="del" @click="partDelete(row)">删除</a>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer" style="bottom:10px">
          <el-button type="primary" :disabled="isokDisable" @click="dialogStatus==='create'?add_dialog_save():add_dialog_save()">保存并关闭</el-button>
          <el-button @click="dialogFormVisible = false;get_table_data()">取消并关闭</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisibleSee" :close-on-click-modal="false" width="90%">
      <div class="app-containerSee">
        <el-form ref="seeTemp" :rules="rules" :model="seeTemp" label-position="right" label-width="100px" style="width: 90%;margin:0 auto;">
          <el-row>
            <el-col :span="8">
              <el-form-item label="调拨单号:" prop="applyCode">
                <el-input ref="applyCode" v-model="seeTemp.applyCode" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="调出牧场：" prop="pastureName">
                <el-input ref="pastureName" v-model="seeTemp.pastureName" disabled />
                <!-- <el-select v-model="seeTemp.pastureId" disabled placeholder="调出牧场" class="filter-item" style="width:100%">
                  <el-option
                    v-for="item in findAllPasture"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select> -->
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="调入牧场：" prop="inPastureId">
                <el-select v-model="seeTemp.inPastureId" disabled placeholder="调入牧场" class="filter-item" style="width:100%">
                  <el-option
                    v-for="item in findAllPasture2"
                    :key="item.id"
                    :label="item.pastureName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="调拨人：" prop="emp">
                <el-input ref="emp" v-model="seeTemp.emp" disabled />
                <!-- <el-select v-model="seeTemp.empId" placeholder="调拨人" disabled class="filter-item" style="width:100%" @change="changeEmp">
                  <el-option
                    v-for="item in findAllEmploye"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select> -->
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="调拨日期：" prop="applyDate">
                <el-date-picker
                  v-model="seeTemp.applyDate"
                  :picker-options="pickerOptions"
                  type="date"
                  disabled
                  placeholder="调拨日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  style="width:100%"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table
          :key="tableKey"
          v-loading="listLoadingSee"
          element-loading-text="给我一点时间"
          :data="listSee"
          border
          fit
          highlight-current-row
          style="width: 100%;margin-bottom:30px"
          :row-style="rowStyle"
          :cell-style="cellStyle"
          class="elTable"
          @sort-change="tableSort"
        >
          <el-table-column label="序号" align="center" type="index" width="50px" />
          <el-table-column label="备件编号" prop="partCode" align="center" min-width="90">
            <template slot-scope="scope">
              <span>{{ scope.row.partCode }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备件名称" prop="partName" align="center" min-width="90">
            <template slot-scope="scope">
              <span>{{ scope.row.partName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备件规格" prop="specification" min-width="80px" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.specification }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备件图片" prop="picpath" min-width="110px" align="center">
            <template slot-scope="scope">
              <el-popover placement="right" title="" trigger="hover">
                <img v-if="scope.row.picpath !== ''" :src="scope.row.picpath">
                <img v-if="scope.row.picpath !== ''" slot="reference" :src="scope.row.picpath" :alt="scope.row.srcpath" style="height: 100px;width:100px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="备件品牌" prop="brandName" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.brandName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="计量单位" prop="unit" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.unit }}</span>
            </template>
          </el-table-column>
          <el-table-column label="供应商" prop="providerName" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.row.providerName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="调拨数量" sortable prop="amount" min-width="120px" align="center" valign="middle">
            <template slot-scope="scope">
              <span>{{ scope.row.amount }}</span>
            </template>
          </el-table-column>
          <el-table-column label="单价" sortable prop="price" min-width="110px" align="center" valign="middle">
            <template slot-scope="scope">
              <span>{{ scope.row.price }}</span>
            </template>
          </el-table-column>
          <el-table-column label="总价" sortable prop="sumPrice" min-width="110px" align="center" valign="middle">
            <template slot-scope="scope">
              <span>{{ scope.row.sumPrice }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" min-width="110px" align="center" valign="middle">
            <template slot-scope="scope">
              <span>{{ scope.row.note }}</span>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer" style="bottom:5px;">
          <el-button @click="dialogFormVisibleSee = false">关闭</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// / 引入
import { GetDataByName, GetDataByNames, PostDataByName, ExecDataByConfig, checkButtons, failproccess, SapTrans } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import { parseTime, sortChange } from '@/utils/index.js'
// eslint-disable-next-line no-unused-vars
import { validateEMail } from '@/utils/validate.js'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { MessageBox } from 'element-ui'
import Cookies from 'js-cookie'
import { getToken } from '@/utils/auth'
export default {
  name: 'Allot',
  components: { Pagination },
  directives: { waves },
  filters: {
    keepTwoNum(value) {
      value = Number(value)
      return value.toFixed(2)
    }
  },
  data() {
    return {
      myHeight:document.documentElement.clientHeight - 85- 200,
      isSap:1,
      isSrm:1,
      selectList: [],
      isAllotsap: [],
      currentRow:{},
      loginPastureid: Cookies.get('pastureid'),
      isokDisable: false,
      findAllProvider: [],
      findAllPasture: [],
      findAllPasture2: [],
      findAllDepart: [],
      findAllEmploye: [],
      statues: [{ id: '0', name: '调拨中' }, { id: '1', name: '调拨完成' }, { id: '2', name: '驳回中' }, { id: '3', name: '退回完成' }],
      requestParams: [
        { name: 'findAllProvider', offset: 0, pagecount: 0, params: [] },
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }},
        { name: 'getPastureList', offset: 0, pagecount: 0, returntype: 'Map', parammaps: {}},
        { name: 'findAllDepart', offset: 0, pagecount: 0, parammaps: { 'pastureId': Cookies.get('pastureid'), 'eId': Cookies.get('employeid') }},
        { name: 'findAllEmploye', offset: 0, pagecount: 0, parammaps: { 'pastureId': Cookies.get('pastureid') }}
      ],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()// 当天之前的时间可选
        }
      },
      textMap: {
        create: '新增',
        see: '查看'
      },
      rules: {
        inPastureId: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      getdataListParm: {
        name: 'getBigPartTransferList',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          pastureName: Cookies.get('pasturename'),
          applyCode: '',
          statue: '',
          pastureNameIN: '',
          inputDatetime: '',
          startTime: '',
          stopTime: ''
        }
      },
      tableKey: 0,
      listLoading: true,
      total: 0,
      list: [],
      dialogStatus: '',
      dialogFormVisible: false,
      dialogFormVisibleSee: false,
      listLoadingSee: true,
      listSee: [],
      totalSee: 0,
      seeTemp: {},
      getdataListSee: {
        name: 'getPartTransferbybigid',
        returntype: 'Map',
        parammaps: {}
      },
      createTemp: {
        pastureId: this.$store.state.user.pastureid,
        departmentId: this.$store.state.user.departmentid,
        empId: this.$store.state.user.employeid,
        employeId: this.$store.state.user.employeid,
        applyDate: parseTime(new Date(), '{y}-{m}-{d}'),
        groupName:''
      },
      requestSparePart: {
        name: 'getPartsListLY',
        page: 1,
        offset: 1,
        pagecount: 20,
        returntype: 'Map',
        parammaps: {
          pastureId: Cookies.get('pastureid')
        }
      },
      getParmCreateNumber: {
        name: 'autoCreatCode',
        page: 0,
        offset: 0,
        pagecount: 0,
        returntype: 'Map',
        parammaps: {
          pastureId: Cookies.get('pastureid'),
          codeType: 'DB'
        }
      },
      // getParmCreateAmount: {
      //   name: 'getPartsAmountList',
      //   page: 0,
      //   offset: 0,
      //   pagecount: 0,
      //   returntype: 'Map',
      //   parammaps: {}
      // },
      listAdd: [],
      postDataPramas: {},
      requestParam: {},
      dialogFormVisible_Examine: false,
      examineTemp: {
        statue: 1
      },
      statueReason: false,
      activeList: [],
      active: 3
    }
  },

  created() {
    this.get_select_list()
    this.get_table_data()
  },

  watch: {

    listAdd: {
      deep: true,
      handler: function (item) {
          this.listAdd.forEach(function(i){
            if(parseInt(i.amount) > (parseInt(i.reportery) )){
              i.amount = parseInt(i.reportery)
            }

          })
      }
    },
  },

  methods: {

    handleCurrentChange(val) {
        this.currentRow = val;
        console.log("currentRow",this.currentRow)
    },

    checkboxInit(row,index){
      // console.log(row)


        var data1 = new Date(row.creatTime)
        var data2 = new Date('2022-10-25')
        if(data1 > data2){ return 1 } else {return 0}

    },

    //监听 - 表格 - 勾选
    change_table_selection(val) {
      this.selectList = val
      console.log('勾选数据selectList', this.selectList)
    },
    sapUpload(){
      const that = this
      console.log("currentRow ProofCode",this.currentRow.ProofCode)
      var send_data = this.selectList

      if(send_data.length == 0){
        that.$notify({ title: '失败', message: '请勾选数据！'  , type: 'error', duration: 2000 })
        return false
      }


      console.log(that.isSap)

      if(that.isSap == 1){

         that.isSap = 0
         SapTrans(send_data).then(response => {
              console.log('response', response)

              if (response.msg !== 'fail') {
                that.$notify({ title: '成功', message: '上传成功', type: 'success', duration: 2000 })
                that.selectList = []
                that.get_table_data()
                } else {
                  that.$notify({ title: '失败', message: '上传失败'  , type: 'error', duration: 2000 })
                }
            })


      } else {

        that.$notify({ title: '失败', message: '正在请求中，请稍等几秒钟后再点击'  , type: 'error', duration: 2000 })
      }

      setTimeout(() => {
              that.isSap = 1
            }, 3000)




      //  if(true){
      //sapStatus 0 或没有 未同步，1 同步成功，2同步失败
      // if(this.currentRow.sapStatus){
      //    console.log("currentRow sapStatus 这个值说明已经上传了 传")

      //    if(this.currentRow.sapStatus == 1){

      //    } else {
      //     var send_data = {
      //             "code":applyCode,
      //             "pastureid":Cookies.get('pastureid')

      //       }
      //       SapTrans(send_data).then(response => {
      //         console.log('response', response)

      //         if (response.msg !== 'fail') {
      //           that.$notify({ title: '成功', message: '上传成功', type: 'success', duration: 2000 })
      //           that.get_table_data()
      //           } else {
      //             that.$notify({ title: '失败', message: '上传失败'  , type: 'error', duration: 2000 })
      //           }
      //       })
      //    }
      //   //如果有这个值说明已经上传了，不用上传
      // } else {
      //   console.log("currentRow sapStatus没有这个值")



      //     console.log("applyCode",applyCode)
      //       var send_data = {
      //             "code":applyCode,
      //             "pastureid":Cookies.get('pastureid')

      //       }
      //       SapTrans(send_data).then(response => {
      //         console.log('response', response)

      //         if (response.msg !== 'fail') {
      //           that.$notify({ title: '成功', message: '上传成功', type: 'success', duration: 2000 })
      //           that.get_table_data()
      //           } else {
      //             that.$notify({ title: '失败', message: '上传失败'  , type: 'error', duration: 2000 })
      //           }
      //       })

      // }

    },

    tableSort(column) {
      sortChange(column, this.listSee)
    },
    get_table_data() {
      this.listLoading = true
      if (this.$refs['inputDatetime'] !== undefined && this.$refs['inputDatetime'].value !== null) {
        this.getdataListParm.parammaps.startTime = this.$refs['inputDatetime'].value[0]
        this.getdataListParm.parammaps.stopTime = this.$refs['inputDatetime'].value[1]
      } else {
        this.getdataListParm.parammaps.startTime = ''
        this.getdataListParm.parammaps.stopTime = ''
      }
      GetDataByName(this.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        this.list = response.data.list
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        if (response.data.total) {
          this.total = response.data.total
        }
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    statue: function(cellValue) {
      // console.log(cellValue.isZeroStock)
      if (cellValue.statue == 0) {
        return '调拨中'
      } else if (cellValue.statue == 1) {
        return '调拨完成'
      } else if (cellValue.statue == 2) {
        return '驳回中'
      } else if (cellValue.statue == 3) {
        return '退回完成'
      } else if (cellValue.statue == 4) {
        return '调拨中'
      }
    },
    form_search1() {
      console.log('点击了table搜索')
      if (this.getdataListParm.parammaps.inputDatetime === null) {
        this.getdataListParm.parammaps.inputDatetime = ''
      }
      this.listLoading = true
      this.getdataListParm.offset = 1
      this.get_table_data()
    },
    resetCreateTemp() {
      this.createTemp.pastureId = this.$store.state.user.pastureid
      this.createTemp.departmentId = this.$store.state.user.departmentid
      this.createTemp.employeId = this.$store.state.user.employeid
      this.createTemp.empId = this.$store.state.user.employeid
      this.createTemp.createTime = parseTime(new Date(), '{y}-{m}-{d}')
      this.createTemp.applyCode = ''
      this.createTemp.groupName = ''
      this.listAdd = []
    },
    form_add() {
      this.resetCreateTemp()
      this.createTemp.applyCode = ''
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.getCreateNumber()
    },
    getCreateNumber() {
      GetDataByName(this.getParmCreateNumber).then(response => {
        this.$nextTick(() => {
          console.log('新增申购单号', response.data.list[0].orderCode)
          this.createTemp.applyCode = response.data.list[0].orderCode
          this.$forceUpdate()
        })
      })
    },
    changeEmp(val) {
      this.createTemp.emp = this.findAllEmploye.find(obj => obj.id == val).name
    },
    sparePartSearch(queryString, cb) {
      console.log('备件模糊查询输入值', queryString)
      this.requestSparePart.parammaps['partCode'] = queryString
      GetDataByName(this.requestSparePart).then(response => {
        console.log('备件模糊查询搜索data', response.data.list)
        if (response.data.list == null) {
          cb([])
        } else {
          cb(response.data.list)
        }
      })
    },
    handleSelectSparePart(item) {
      console.log('备件模糊查询选中值', item)
      console.log('备件模糊查询选中值', item.id)
      console.log(this.listAdd)

      if (this.listAdd.length > 0) {
        // eslint-disable-next-line no-redeclare
        if (this.listAdd.find(obj => obj.id === item.id)) {
          this.$message({
            type: 'warning',
            message: '此备件已存在，请重新选择备件'
          })
        } else {
          if (item.srcpath !== null && item.picpath !== null && item.srcpath !== undefined && item.picpath !== undefined) {
            this.$set(item, 'srcpath', process.env.VUE_APP_BASE_API + item.srcpath)
            this.$set(item, 'picpath', process.env.VUE_APP_BASE_API + item.picpath)
          } else {
            item.srcpath = ''
            item.picpath = ''
          }
          this.listAdd.unshift(item)
          // this.getParmCreateAmount.parammaps.partCode = item.partCode
          // GetDataByName(this.getParmCreateAmount).then(response => {
          //   this.$nextTick(() => {
          //     console.log('新增根据编号获取备件库存数量', response.data.list[0].reporteryNum)
          //     item.reportery = response.data.list[0].reporteryNum
          //     this.listAdd.unshift(item)
          //     this.$forceUpdate()
          //   })
          // })
        }
      } else {
        if (item.srcpath !== null && item.picpath !== null && item.srcpath !== undefined && item.picpath !== undefined) {
          this.$set(item, 'srcpath', process.env.VUE_APP_BASE_API + item.srcpath)
          this.$set(item, 'picpath', process.env.VUE_APP_BASE_API + item.picpath)
        } else {
          item.srcpath = ''
          item.picpath = ''
        }
        this.listAdd.unshift(item)
        // this.getParmCreateAmount.parammaps.partCode = item.partCode
        // GetDataByName(this.getParmCreateAmount).then(response => {
        //   this.$nextTick(() => {
        //     console.log('新增根据编号获取备件库存数量', response.data.list[0].reporteryNum)
        //     item.reportery = response.data.list[0].reporteryNum
        //     this.listAdd.unshift(item)
        //     this.$forceUpdate()
        //   })
        // })
      }
      this.$forceUpdate()
    },
    add_dialog_save() {
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          console.log(this.listAdd)
          if (this.createTemp.applyCode == '' || this.createTemp.applyCode == null || this.createTemp.applyCode == undefined) {
            GetDataByName(this.getParmCreateNumber).then(response => {
              this.$nextTick(() => {
                console.log('新增申购单号', response.data.list[0].orderCode)
                this.createTemp.applyCode = response.data.list[0].orderCode
                this.$forceUpdate()
                this.add_dialog_saveSave()
              })
            })
          } else {
            this.add_dialog_saveSave()
          }
        }
      })
    },
    add_dialog_saveSave() {
      if (this.listAdd.length !== 0) {
        for (var j = 0; j < this.listAdd.length; j++) {
          console.log(this.listAdd[j].amount)
          // eslint-disable-next-line use-isnan
          if (this.listAdd[j].amount == null || this.listAdd[j].amount === '') {
            this.$message({
              type: 'warning',
              message: '请检查调拨数量是否未填写',
              duration: 2000
            })
            return false
          } else if (parseFloat(this.listAdd[j].amount) > parseFloat(this.listAdd[j].reportery)) {
            this.$message({
              type: 'error',
              message: '调拨数量不可大于现有库存数量',
              duration: 2000
            })
            return false
          } else {
            var rulesAmount = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
            if (!rulesAmount.test(parseFloat(this.listAdd[j].amount))) {
              this.$message({
                type: 'error',
                message: '调拨数量请输入正数，且最多保留两位小数',
                duration: 2000
              })
              return false
            }
          }
        }
        this.postDataPramas.common = { 'returnmap': '0' }
        this.postDataPramas.data = []
        this.postDataPramas.data[0] = { 'name': 'insertBigPartTransfer', 'type': 'e', 'parammaps': {
          pastureId: this.createTemp.pastureId,
          inPastureId: this.createTemp.inPastureId,
          applyCode: this.createTemp.applyCode,
          empId: this.createTemp.empId,
          emp: this.createTemp.emp,
          applyDate: this.createTemp.applyDate,
          groupName:this.createTemp.groupName
        }}
        // eslint-disable-next-line no-irregular-whitespace
        this.postDataPramas.data[1] = { 'name': 'insertSpotList', 'resultmaps': { 'list': this.listAdd }}
        this.postDataPramas.data[1].children = []
        this.postDataPramas.data[1].children[0] = { 'name': 'insertParttTransfer', 'type': 'e', 'parammaps': {
          bigId: '@insertBigPartTransfer.LastInsertId',
          pastureId: '@insertSpotList.pastureId',
          partId: '@insertSpotList.partId',
          partCode: '@insertSpotList.partCode',
          partName: '@insertSpotList.partName',
          specification: '@insertSpotList.specification',
          price: '@insertSpotList.price',
          unit: '@insertSpotList.unit',
          brandId: '@insertSpotList.brandId',
          brandName: '@insertSpotList.brand',
          amount: '@insertSpotList.amount',
          providerName: '@insertSpotList.providerName',
          providerId: '@insertSpotList.providerId',
          note: '@insertSpotList.note',
          partRepId: '@insertSpotList.partRepId'
        }}
        ExecDataByConfig(this.postDataPramas).then(response => {
          console.log('新增保存发送参数', this.postDataPramas)
          if (response.msg === 'fail') {
            this.$notify({
              title: '保存失败',
              message: response.data,
              type: 'warning',
              duration: 2000
            })
          } else {
            this.dialogFormVisible = false
            this.getdataListParm.parammaps.inputDatetime = ''
            this.get_table_data()
            this.$notify({
              title: '',
              message: '保存成功',
              type: 'success',
              duration: 2000
            })
          }
        })
        return true
      } else {
        this.$message({
          title: '',
          message: '请选择备件',
          type: 'warning',
          duration: 2000
        })
        return false
      }
    },
    partDelete(row) {
      console.log(this.listAdd)
      MessageBox.confirm('设备名称：' + row.partName, '确认删除？', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // console.log(this.list2)
        for (var i = 0; i < this.listAdd.length; i++) {
          console.log(this.listAdd[i])
          if (this.listAdd[i].id === row.id) {
            var listAddIndex = this.listAdd.indexOf(this.listAdd[i])
          }
          if (listAddIndex > -1) {
            this.listAdd.splice(listAddIndex, 1)
            return
          }
        }
      })
    },
    // 查看
    form_see(row) {
      this.dialogStatus = 'see'
      this.dialogFormVisibleSee = true
      row.pastureId = String(row.pastureId)
      console.log(row.pastureId)
      this.seeTemp = Object.assign({}, row)
      console.log('查看上方数据(从table读取)', this.seeTemp)
      this.getdataListSee.parammaps.id = this.seeTemp.id
      this.getSeeList()
    },
    getSeeList() {
      GetDataByName(this.getdataListSee).then(response => {
        console.log('查看下方table数据', response.data.list)
        this.listSee = response.data.list
        if (response.data.list !== null) {
          for (let i = 0; i < response.data.list.length; i++) {
            if (response.data.list[i].srcpath !== null && response.data.list[i].picpath !== null && response.data.list[i].srcpath !== undefined && response.data.list[i].picpath !== undefined) {
              this.listSee[i].srcpath = process.env.VUE_APP_BASE_API + response.data.list[i].srcpath
              this.listSee[i].picpath = process.env.VUE_APP_BASE_API + response.data.list[i].picpath
            } else {
              this.listSee[i].srcpath = ''
              this.listSee[i].picpath = ''
            }
          }
        }
        this.seeTemp.providerName = response.data.list.providerName
        this.pageNumSee = response.data.pageNum
        this.pageSizeSee = response.data.pageSize
        if (response.data.total) {
          this.totalSee = response.data.total
        }
        setTimeout(() => {
          this.listLoadingSee = false
        }, 100)
      })
    },

    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        this.findAllProvider = response.data.findAllProvider.list
        this.findAllPasture = response.data.findAllPasture.list
        this.findAllPasture2 = response.data.getPastureList.list
        this.findAllDepart = response.data.findAllDepart.list
        this.findAllEmploye = response.data.findAllEmploye.list
      })
    },

    handleDownload() {
      alert('点击了导出')
    },
    handleReturn(row) {
      console.log('点击了确认退回')
      this.seeTemp = Object.assign({}, row)
      this.getdataListSee.parammaps.id = this.seeTemp.id
      this.getSeeList()
      MessageBox.confirm('是否确认将调拨单备件退回至库存', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.postDataPramas.common = { 'returnmap': '0' }
        this.postDataPramas.data = []
        this.postDataPramas.data[0] = { 'name': 'updateBigPartTransfer', 'type': 'e', 'parammaps': {
          id: row.id,
          statue: 3
        }}
        this.postDataPramas.data[1] = { 'name': ' getPartTransferbybigid', 'type': 'e', 'parammaps': {
          id: row.id,
          statue: 3
        }}
        this.postDataPramas.data[2] = { 'name': 'insertSpotList', 'resultmaps': { 'list': this.listSee }}
        this.postDataPramas.data[2].children = []
        this.postDataPramas.data[2].children[0] = { 'name': 'updatePartRepertoryDB', 'type': 'e', 'parammaps': {
          amount: '@insertSpotList.amount',
          partRepId: '@insertSpotList.partRepId'
        }}
        ExecDataByConfig(this.postDataPramas).then(response => {
          this.get_table_data()
          this.$notify({
            title: '成功',
            message: '退回成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消确认退回'
        })
      })
    },
    handleReject(row) {
      console.log('点击了驳回')
      MessageBox.confirm('是否确认将调拨单驳回', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.requestParam.name = 'updateBigPartTransfer'
        this.requestParam.parammaps = {}
        this.requestParam.parammaps.id = row.id
        this.requestParam.parammaps.statue = 2
        PostDataByName(this.requestParam).then(() => {
          this.get_table_data()
          this.dialogFormVisible = false
          this.$notify({
            title: '成功',
            message: '驳回成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消驳回'
        })
      })
    }

  }
}
</script>
<style lang="scss" scoped>
  .pagination-container{
    display: block !important;
  }
</style>
<style lang="scss">
.el-step__head.is-success {
    color: #409EFF;
    border-color: #409EFF;
}
.el-step__title.is-success{
  color: #409EFF;
}
.el-step__head.is-process{
  color: #409EFF;
  border-color: #409EFF;
  .el-step__icon.is-text{
    background: #409EFF;
    color: #fff;
  }
}
.step-row{
  color: #000;
}
</style>
