<template>
    <div id="manualModal">
      <a-modal
        title="手工匹配订单"
        v-model="manualModVis"
        :confirmLoading="confirmLoading"
        @cancel="handleCancel"
        width="80%"
        style="top:330px;left:100px;"
      >
        <!-- 主表信息 回显  -->
        <a-card :bordered="false" >
          <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
                <a-form-item label="账套">
                  <j-search-select-tag
                  placeholder="请选择账套"
                  v-model="queryParam.account"
                  dict="view_account,account,account">
                </j-search-select-tag>
                </a-form-item>
              </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="销售订单号">
                <a-input placeholder="请输入销售订单号" v-model="queryParam.orderNumber"></a-input>
              </a-form-item>
            </a-col>            
            <template v-if="toggleSearchStatus">
            </template>

            <a-col :md="6" :sm="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
          </div>
        </a-card>
  
        <!-- 増行 子表 -->
        <a-card :bordered="false" style="margin:10px 0;">
          <!-- 子表 -->
          <a-spin :spinning="confirmLoading">
              <a-table
                bordered
                :row-key="record => record.id"
                :columns="manualColumns"
                :data-source="manualData"
                :scroll="{x: 1200,y:300 }"
                :pagination="pagination"
                 @change="handleTableChange"
              >
              <span slot="quantitySlot" slot-scope="text, record,index">
                <a-input placeholder="请输入数量" v-model="record.quantity"></a-input>
              </span>
              <span slot="operationSlot" slot-scope="text, record,index">
                     <a @click="handleExportXls(record)" style="color:green;">关联委外</a>
                     <a-divider type="vertical" />
                     <a @click="handleCopy(record,index)" style="color:green;">复制</a>
                     <!-- <a @click="handleDelete(record,index)" style="color:red;">删行</a> -->
                 </span>

              </a-table>
          </a-spin>
        </a-card>
  
        <!-- 页面底部保存取消 -->
        <div
          :style="{
            position: 'absolute',
            right: 0,
            bottom: 0,
            width: '100%',
            borderTop: '1px solid #e9e9e9',
            padding: '10px 16px',
            background: '#fff',
            textAlign: 'right',
            zIndex: 1
          }"
        >
          <a-popconfirm title="确定放弃？" @confirm="handleCancel" okText="确定" cancelText="取消">
            <a-button :style="{ marginRight: '8px' }">返回</a-button>
          </a-popconfirm>
          <a-button type="primary" @click="addSave">
            保存
          </a-button>
        </div>
      </a-modal>
      <subcontract-order-modal ref="subcontractOrderModal" @close="closeBack"></subcontract-order-modal>
    </div>
  </template>
  
  <script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import moment from 'moment'
  import { syDeclarationElementsAdd,queryLatelyDeclarationElements } from '@api/document/shipmentList'
  import {Dictionaries} from '@api/document/book.js'
  import {salesOrder,submitSalesOrder} from '@api/document/order'
  import subcontractOrderModal from '@views/order/subcontractOrderModal.vue'
  
  export default {
    name: 'manualModal', // 报关要素
    mixins: [JeecgListMixin],
    components: { JEllipsis, moment,subcontractOrderModal },
  
    data() {
      let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略
  
      return {
        queryParam:{},
        manualModVis:false,
        confirmLoading: false,
        manualData:[ ],
        manualColumns:[
        {
          title: '账套号',
          width: 70,
          dataIndex: 'account',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '销售订单号',
          width: 180,
          dataIndex: 'orderNumber',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '行信息',
          width: 70,
          dataIndex: 'irowNo',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '存货编码',
          width: 100,
          dataIndex: 'inventoryCode',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '存货名称',
          width: 220,
          dataIndex: 'inventoryName',
          className: 'replacecolor',
        },
        {
          title: '规格型号',
          width: 120,
          dataIndex: 'specificationAndModel',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '颜色',
          width: 80,
          dataIndex: 'colour',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '委外订单号',
          width: 120,
          dataIndex: 'ccode',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '委外订单行号',
          width: 90,
          dataIndex: 'ivouchRowNo',
          className: 'replacecolor',
          ellipsis: true
        },
        {
          title: '数量',
          width: 90,
          dataIndex: 'quantity',
          className: 'replacecolor',
          scopedSlots: { customRender: 'quantitySlot' },
          ellipsis: true
        },
        {
          title: '操作',
          width: 120,
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operationSlot' },
          className: 'replacecolor',
          ellipsis: true
        },
        ],
        pagination:{}
      }
    },
    // 接收父组件 方法
    props: {},
  
    created() {  

    },
    methods: {
        //获取数据
        getData(){
      var that = this;
      this.confirmLoading = true
      this.$nextTick(() => {
        salesOrder(this.queryParam).then(res => {
          this.confirmLoading = false
          if (res.success) {
            that.manualData = res.result.records;
            that.manualData.map(item=>{
              item.selectStatus = '0'
            })
            that.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }else{
              that.$message.error(res.message);
          }
          
        })
      })
        },
        //查询 
        searchQuery(){
          if(this.queryParam.account == '' || !this.queryParam.account){
             this.$message.error('请选择账套')
          }else if(this.queryParam.orderNumber == '' || !this.queryParam.orderNumber){
            this.$message.error('请选择销售订单号')
          }else{

            this.getData()
          }
        },
        //重置
        searchReset(){
          this.queryParam ={}
          this.manualData =[]
        },
        //返回
        handleCancel(){
           this.close()
        },
        //保存
        addSave(){
          var newObj =[]
          this.manualData.map(item=>{
            if(!item.selectStatus){newObj.push(item)}
          })
          if(newObj.length !== this.manualData.length){
            this.$message.error('未关联全部数据！');
          }else{
            submitSalesOrder(newObj).then(res => {
            if (res.success) {
              this.$message.success('保存成功')
              this.close()
            }else{
              this.$message.error(res.message);
             }
          
            })
          }
        },
        //关联委外
        handleExportXls(record){
           this.$refs.subcontractOrderModal.subcontractOrderModVis = true
           this.$refs.subcontractOrderModal.fatherList = record
           this.$refs.subcontractOrderModal.manualData.push(record)
        },
        //复制行
        handleCopy(record,index){
          var newObj = {...record}
          var num = 0
          this.manualData.map(item=>{
             if(item.copyRow == newObj.copyRow){
              num+=1
             }
          })
          newObj.copyRow = num
          this.manualData.splice(index,0,newObj)
        },
        //删行
        handleDelete(record,index){
          this.manualData.splice(index, 1);
        },
        //关闭弹窗
        close(){
          this.queryParam ={}
          this.manualData =[]
          this.manualModVis = false
        },
         // 分页变化时触发
         handleTableChange(pagination, filters, sorter) {
             this.queryParam.pageNo = pagination.current
              this.getData()
          },
        //委外订单弹窗关闭后
        closeBack(data,obj){
           this.manualData.map(item=>{
             if(item.irowNo == data.irowNo && item.copyRow == data.copyRow){
               item.ccode = obj.ccode
               item.ivouchRowNo = obj.ivouchRowNo
               delete item.selectStatus
             }
           })
        }
    },
  
    computed: {}
  }
  </script>
  <style lang="less" scoped>
  @import '~@assets/less/common.less';
  @import '~@assets/less/overwriter.less';
  /deep/ .ant-table-thead > tr > th {
    text-align: center;
    // font-weight: 700;
  }
  /deep/ .ant-table-tbody {
    text-align: center;
  }
  // th.replacecolor {
  // background-color:  #ccc;
  // }
  // 对话框里的card样式
  /deep/ .ant-modal-content {
    background-color: #f0f2f5;
  }
  /deep/ .ant-modal-body {
    padding: 10px;
  }
  /deep/.ant-form-item{
    margin-bottom: 0px !important;
  }
   /deep/.ant-table-tbody .ant-table-row td{
    padding-top: 8px;
    padding-bottom: 8px;
  }
  /deep/ .ant-table-thead > tr > th, .ant-table-tbody > tr > td{
  padding: 9px 16px
 }
 /deep/.ant-card-body{
  padding-top: 10px !important;
  padding-bottom: 0px !important;
  }
   /deep/ .table-page-search-wrapper .table-page-search-submitButtons{
    margin-bottom: 8px;
  }
  </style>
  