<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <!-- 搜索 -->
      <el-input v-model="query.itemCode" clearable placeholder="品番" style="width: 185px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />
      <el-input v-model="query.itemName" clearable placeholder="品名" style="width: 185px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />
      <el-select
        v-model="query.relaSupplier"
        clearable filterable
        size="small"
        placeholder="供应商"
        class="filter-item"
        style="width: 150px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in dict.supplier_compare"
          :key="item.id"
          :label="item.label"
          :value="item.label"
        />
      </el-select>
      <el-select
        v-model="query.status"
        clearable
        size="small"
        placeholder="状态"
        class="filter-item"
        style="width: 150px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in dict.xxp_status"
          :key="item.id"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-select
        v-model="query.beFz"
        clearable
        size="small"
        placeholder="是否复制"
        class="filter-item"
        style="width: 150px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in dict.boolean_status"
          :key="item.id"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

        <el-input v-model="query.ddbh" clearable placeholder="订单编号" style="width: 185px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />
      <el-input v-model="query.receiptCode" clearable placeholder="收货清单号" style="width: 185px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />
      <el-input v-model="query.prop_c1" clearable placeholder="批次号" style="width: 185px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />
      <el-input v-model="query.inv_point_code" clearable placeholder="库存库位" style="width: 185px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />
      <el-input v-model="query.ewm" clearable placeholder="现品票" style="width: 185px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />
      <label class="el-form-item-label" >采集时间
        <date-range-picker v-model="query.createTime" class="date-item" style="width: 200px"/>
      </label>
      <label class="el-form-item-label" >备货时间
       <date-range-picker v-model="query.bhTime" class="date-item" style="width: 200px"/>
      </label>
      <rrOperation :crud="crud"/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button
            slot="right"
            v-permission="permission.collectEwm"
            class="filter-item"
            type="primary"
            size="mini"
            @click="collectFromFlag = true"
            icon="el-icon-document-add"
        >
          二维码采集
        </el-button>
        <el-button
            slot="right"
            v-permission="permission.xppLyOut"
            class="filter-item"
            type="primary"
            size="mini"
            @click="xppLyOutM(crud.selections)"
        >
          现品票留样
        </el-button>
        <el-button
            slot="right"
            v-permission="permission.xppHyIn"
            class="filter-item"
            type="primary"
            size="mini"
            :disabled="!(crud.selections.length >0)"
            @click="xppHyInM(crud.selections)"
        >
          现品票还样
        </el-button>
        <el-button
            slot="right"
            v-permission="permission.xppHyInBf"
            class="filter-item"
            type="primary"
            size="mini"
            :disabled="crud.selections.length != 1"
            @click="xppRecordOnOff=true"
        >
          部分还样
        </el-button>

        <el-button
          slot="right"
          v-permission="permission.copyToApro"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length <= 0"
          @click="copyToApro(crud.selections)"
        >
          复制到Apro
        </el-button>
        <el-button
          slot="right"
          v-permission="permission.cancelReceiv"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length <= 0"
          @click="cancelReceivM(crud.selections)"
        >
          取消收货
        </el-button>
        <el-button
              slot="right"
              v-permission="permission.cancelPut"
              class="filter-item"
              type="primary"
              size="mini"
              :disabled="crud.selections.length <= 0"
              @click="cancelPutM(crud.selections)"
        >
            取消上架
        </el-button>
        <el-button
          slot="right"
          v-permission="permission.cancelPut"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length <= 0"
          @click="adjustOutBtn(crud.selections)"
        >
          调整出库
        </el-button>
        <el-button
          slot="right"
          v-permission="permission.showXppBd"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length != 1"
          @click="showXppBd(crud.selections[0])"
        >
          现品票补打
        </el-button>
        <el-button
          slot="right"
          v-permission="permission.createReceiptBtn"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="!(crud.selections.length >0)"
          @click="createReceiptBtn(crud.selections)"
        >
          生成收货清单
        </el-button>
        <el-button
          slot="right"
          v-permission="permission.cancelReceiptBtn"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="!(crud.selections.length >0)"
          @click="cancelReceiptBtn(crud.selections)"
        >
          取消收货清单
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="!(crud.selections.length >0)"
          @click="showReportShd(crud.selections[0])"
          v-permission="permission.showReportShd"
          icon="el-icon-document-add"
        >
          打印收货清单
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
          title="部分还样"
          :visible.sync="xppRecordOnOff"
          width="30%"
      >
        <el-form :model="xppRecordButton" label-width="120px">
          <el-form-item label="现品票">
            <el-input v-model="xppRecordButton.ewm"  :rows="3" type="textarea" style="width: 270px;"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="xppRecordOnOff = false">取 消</el-button>
      <el-button type="primary" @click="xpplyBFM(crud.selections)">确 定</el-button>
  </span>
      </el-dialog>
      <!--      二维码采集-->
      <el-dialog title="现品票采集" :visible.sync="collectFromFlag" width="400px">
        <el-form ref="collectFrom" :model="collectFrom" :rules="rules" size="small" label-width="80px">
          <el-form-item label="二维码" prop="ewm">
            <el-input v-model="collectFrom.ewm" :rows="3" type="textarea" style="width: 270px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="collectFromFlag= false">取 消</el-button>
          <el-button type="primary" @click="collectFromPass">确 定</el-button>
        </div>
      </el-dialog>

      <!--      现品票补打-->
      <el-dialog title="现品票补打" :visible.sync="xppBdOnOff" width="700px" >
        <el-form ref="form" :model="xppBdForm" :rules="rules" size="small" label-width="80px" :inline="true">
          <el-form-item label="品番">
            <el-input v-model="xppBdForm.xppRecord.item_code"  :disabled="true" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="品名">
            <el-input v-model="xppBdForm.xppRecord.item_name"  :disabled="true" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="订单号">
            <el-input v-model="xppBdForm.xppRecord.ddbh"  :disabled="true" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="纳入数">
            <el-input v-model="xppBdForm.xppRecord.nrs"  style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="分支号">
            <el-input v-model="xppBdForm.xppRecord.fzh"  style="width: 200px;"/>
          </el-form-item>
          <el-form-item />
          <el-form-item label="二维码" prop="ewm">
            <el-input v-model="xppBdForm.xppRecord.ewm" :rows="3" :disabled="true" type="textarea" style="width: 500px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="xppBdOnOff= false">取 消</el-button>
          <el-button type="primary" @click="xppBdBtn(crud.selections)">确 定</el-button>
        </div>
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table" :height="crud.tableHeight" v-loading="crud.loading" :data="crud.data" size="small"
                style="width: 100%;" @selection-change="crud.selectionChangeHandler" border
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="序号" width="80px"/>
        <el-table-column prop="receipt_code" label="收货清单号" width="120px"/>
        <el-table-column prop="rela_supplier" label="供应商" width="180"/>
        <el-table-column prop="item_code" label="品番" width="100px">
        </el-table-column>
        <el-table-column prop="item_name" label="品名" width="150px">
        </el-table-column>
        <el-table-column prop="xxp_status#status" label="状态" width="80px">
          <template slot-scope="scope">
            {{ dict.label.xxp_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="nrs" label="纳入数" width="60px"/>
        <el-table-column prop="ddbh" label="订单编号"/>
        <el-table-column prop="fzh" label="分支号" width="60px"/>
          <el-table-column prop="prop_c1" label="批次号" width="100px"/>
        <el-table-column prop="boolean_status#be_fz" label="是否复制">
          <template slot-scope="scope">
            {{ dict.label.boolean_status[scope.row.be_fz] }}
          </template>
        </el-table-column>
        <el-table-column prop="ac" label="A/C" width="60px"/>
          <el-table-column prop="srs" label="收容数" width="80px"/>
        <el-table-column prop="nr_date" label="纳入指示日" width="100px">
          <template slot-scope="scope">
            {{ formatDate1(scope.row.nr_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="inv_point_code" label="库存库位" width="120px"/>
        <el-table-column prop="cjr" label="采集人" width="60px"/>
        <el-table-column prop="cj_date" label="采集时间" width="150"/>
        <el-table-column prop="bhr" label="备货人" width="60px"/>
        <el-table-column prop="bh_code" label="备货单号" width="120px"/>
        <el-table-column prop="fhr" label="发货人" width="60px"/>
        <el-table-column prop="jsr" label="线边接收人"  width="90px"/>
        <el-table-column prop="jskw" label="接收库位" width="80" align="center"/>
        <el-table-column prop="zztlr" label="制造投料人" width="90px"/>
        <el-table-column prop="hctlr" label="缓存投料人"  width="90px"/>

        <el-table-column prop="bh_date" label="备货时间" width="150px"/>
        <el-table-column prop="fh_date" label="发货时间" width="150px"/>
        <el-table-column prop="js_date" label="接收时间" width="150px"/>
        <el-table-column prop="zztl_date" label="投料时间" width="150px"/>
        <el-table-column prop="zzkw" label="投料库位" width="80" align="center"/>
        <el-table-column prop="ewm" :show-overflow-tooltip="true" label="现品票"/>
        <el-table-column prop="description" label="描述"/>
        <el-table-column prop="boolean_status#tf_ly" label="是否留样">
          <template slot-scope="scope">
            {{ dict.label.boolean_status[scope.row.tf_ly] }}
          </template>
        </el-table-column>
        <el-table-column prop="ly_code" label="留样单号" width="120px"/>
        <el-table-column prop="ly_data"  label="留样日期" width="150px"/>
        <el-table-column prop="count_code" label="盘点单号" width="120px"/>
        <el-table-column prop="hy_data"  label="还样日期" width="150px"/>
      </el-table>
    </div>
    <div style="float: right;">
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudXppRecord, {
  xpphybf,
  xppLyOut,
  xppHyIn,
  changeFz,
  cancelReceiv,
  xppBd,
  createReceipt,
  cancelReceipt, adjustOut
} from '@/api/xppRecord'
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 crudAsnDetail,{cancelPut} from '@/api/asnDetail'
import DonMessage from '@/utils/message'
import { formatDate } from '@/utils/commonUtils'
import DateRangePicker from "@/components/DateRangePicker/index.vue";
import {mapGetters} from "vuex";

const defaultForm = {
  tfLy:null,
  lyData:null,
  hyData:null,
  id: null,
  ewm: null,
  item: null,
  ac: null,
  nrs: null,
  ddbh: null,
  zf: null,
  fzh: null,
  nrDate: null,
  shQuantity: null,
  asnDetailId: null,
  cjr: null,
  bhr: null,
  fhr: null,
  jsr: null,
  jskw: null,
  zztlr: null,
  hctlr: null,
  zzkw: null,
  cjDate: null,
  bhDate: null,
  fhDate: null,
  jsDate: null,
  zztlDate: null,
  cPointCode: null,
  zPointCode: null,
  contents: null,
  relaSupplier: null,
  relaDoor: null,
  relaCode: null,
  relaType: null,
  relaQuantity: null,
  areaCode: null,
  itemCode: null,
  deptId: null,
  description: null,
  enabled: false,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  status: 'OPEN',
  receipt_code:null
}
export default {
  name: 'XppRecord',
  components: {DateRangePicker, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: 'xppRecord',
      url: 'api/xppRecord',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudXppRecord },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: true
      }
    })
  },
  dicts: ['xxp_status', 'supplier_compare','boolean_status','base_staus'],
  data() {
    return {
      collectFromFlag: false,
      collectFrom: { ewm: null },
      permission: {
        add: ['admin', 'xppRecord:add'],
        edit: ['admin', 'xppRecord:edit'],
        del: ['admin', 'xppRecord:del'],
        collectEwm: ['admin', 'xppRecord:collectEwm'],
        xppLyOut: ['admin', 'xppRecord:xppLyOut'],
        xppHyIn: ['admin', 'xppRecord:xppHyIn'],
        xppHyInBf: ['admin', 'xppRecord:xppHyInBf'],
        copyToApro: ['admin', 'xppRecord:copyToApro'],
        cancelReceiv: ['admin', 'xppRecord:cancelReceiv'],
        cancelPut: ['admin', 'xppRecord:cancelPut'],
        showXppBd: ['admin', 'xppRecord:showXppBd'],
        createReceiptBtn: ['admin', 'xppRecord:createReceiptBtn'],
        cancelReceiptBtn: ['admin', 'xppRecord:cancelReceiptBtn'],
        showReportShd: ['admin', 'xppRecord:showReportShd']
      },
      tableKey: 'api/xppRecord',
      rules: {},
      selectIds: {
        ids: []
      },
      queryTypeOptions: [
        { key: 'itemCodePf', display_name: '品番代码' }
      ],
      xppRecordButton: {
        xppRecordId: null,
        ewm: null
      },
      xppBdForm: {
        xppRecord:{ddbh:null,nrs:null,ewm:null,item_code:null,item_name:null,fzh:null},
        fzh:null,
        nrs:null
      },
      xppRecordOnOff: false,
      xppBdOnOff: false
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    formatDate1(value) {
      return formatDate(value)
      // const date = new Date(value)
      // const year = date.getUTCFullYear()
      // const month = (date.getMonth() + 1).toString().padStart(2, '0')
      // const day = date.getDate().toString().padStart(2, '0')
      // return  `${year}-${month}-${day}`;
    },
    collectFromPass() {
      crudXppRecord.collectFromPass(this.collectFrom).then(res => {
        if (res.status == 200) {
          this.crud.notify(res.message, CRUD.NOTIFICATION_TYPE.SUCCESS)
        }
        this.collectFromFlag = false
        this.crud.toQuery()
        this.resetForm()

      }).catch(() => {
      })
    },
    //重置表单
    resetForm() {
      this.$refs['collectFrom'].resetFields()
      this.$data.collectFrom = JSON.parse(JSON.stringify(this.$options.data().collectFrom))
    },
    //日期格式化
    getFormatDate(data) {
      return formatDate(data)
    },
    //现品票留样出库
    xppLyOutM(data) {
      let list = []
      for (const datum of data) {
        list.push(datum.id)
      }
      xppLyOut(list).then(res => {
        this.crud.toQuery()
        this.$message.success(res)
      })
    },
    //现品票还样入库
    xppHyInM(data) {
      let list = []
      for (const datum of data) {
        list.push(datum.id)
      }
      xppHyIn(list).then(res=>{
          this.crud.toQuery()
          this.$message.success(res)
      })
    },
      //现品票复制到Apros
      copyToApro(data) {
          let list = ''
          for (const datum of data) {
              list=list+(datum.ddbh+',')
              list=list+(datum.fzh+',')
              list=list+(datum.nrs+',')
          }
          let ids = []
          for (const datum of data) {
              ids.push(datum.id)
          }
          changeFz(ids).then(res=>{
              this.$copyText(list)
                  .then(e=>{
                      this.$message.success('复制成功')
                      this.crud.refresh()
                  },e=>{
                      this.$message.success('复制失败')
                  })
          })

      },
    xpplyBFM(data) {
      let data_ = this.xppRecordButton
      data_.xppRecordId = data[0].id
      xpphybf(data_).then(res => {
        this.xppRecordButton.xppRecordId = null;
        this.crud.toQuery()
        this.$message.success(res)
      })
    },
    xppBdBtn(data) {
      let data_ = this.xppBdForm
      data_.id = data[0].id
      data_.fzh=this.xppBdForm.xppRecord.fzh;
      data_.nrs=this.xppBdForm.xppRecord.nrs;
      xppBd(data_).then(res=>{
        this.xppBdOnOff=false;
        this.$router.push({path:'/business-asn/xpp_bd_report',query:{tableID:'现品票补打',parm: "id="+data_.id}})
      })
    },
    showReportShd(data){
      this.$router.push({path:'/business-asn/xpp_shd_report',query:{tableID:'收货单打印',parm: "code="+data.receipt_code+"&username="+this.user.username}})
    },
    //取消收货
    cancelReceivM(data) {
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      cancelReceiv(data_.ids).then(res=>{
        this.crud.toQuery()
        this.$message.success(res)
      })
    },
    //取消上架
    cancelPutM(data) {
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      cancelPut(data_.ids).then(res=>{
            this.crud.toQuery()
            this.$message.success(res)
        })
    },
    //调整出库
    adjustOutBtn(data) {
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      adjustOut(data_.ids).then(res=>{
        this.crud.toQuery()
        this.$message.success(res)
      })
    },
    //生成收货清单
    createReceiptBtn(data) {
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      createReceipt(data_.ids).then(res=>{
        console.log(res.data)
        this.crud.toQuery()
        //this.curd.notify(res.message, CRUD.NOTIFICATION_TYPE.SUCCESS)
        //this.$message.success(res.message)
        this.$router.push({path:'/business-asn/xpp_shd_report',query:{tableID:'收货单打印',parm: "code="+res.data+"&username="+this.user.username}})

      })
    },
    //取消收货清单
    cancelReceiptBtn(data) {
      let data_ = this.selectIds;
      data_.ids = data.map(v => v.id);
      cancelReceipt(data_.ids).then(res=>{
        this.crud.toQuery()
        this.$message.success(res)
      })
    },
    //弹出现品票补打
    showXppBd(data){
      this.xppBdForm.xppRecord=data;
      this.xppBdOnOff=true;
    },
  }
}
</script>

<style scoped>

</style>
