<template>
  <!-- 预托书列表 -->
  <div id="preBookList">
    <!-- 查询区域 -->
    <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="订单号">
                <a-input placeholder="请输入订单号" v-model="queryParam.orderNumber"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="订单日期" has-feedback>
                <a-date-picker style="width: 100%" v-model="queryParam.orderDate"> </a-date-picker>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="客户简称">
                <a-input placeholder="请输入客户简称" v-model="queryParam.clientAbbreviation"></a-input>
              </a-form-item>
            </a-col>

            <template v-if="toggleSearchStatus">
              <!-- <a-col :md="6" :sm="8">
                <a-form-item label="箱号">
                  <a-input placeholder="请输入箱号" v-model="queryParam.containerNumber"></a-input>
                </a-form-item>
              </a-col> -->

              <a-col :md="6" :sm="8">
                <a-form-item label="小po号">
                  <a-input placeholder="请输入小po号" v-model="queryParam.smailPo"></a-input>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="托书号">
                  <a-input placeholder="请输入托书号" v-model="queryParam.shippingOrderNumber"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-item label="外销发票">
                  <a-input placeholder="请输入外销发票" v-model="queryParam.exportInvoiceNo"></a-input>
                </a-form-item>
              </a-col>
               <a-col :md="6" :sm="8">
                <a-form-model-item label="类型">
                  <a-select v-model="queryParam.type">
                    <a-select-option value="">请选择</a-select-option>
                    <a-select-option value="成衣">成衣</a-select-option>
                    <a-select-option value="面料">面料</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </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="marginTop:10px;">
      <div class="table-operator">
        <a-button type="primary" @click="addBookDrawer" icon="plus">新增</a-button>
        <a-button type="primary" @click="batchSubmit()"  icon="plus">批量提交</a-button>
        <a-button type="primary" @click="batchCanaleSubmit()" icon="plus">批量取消提交</a-button>
      </div>

      <!-- 子表 -->
      <a-table
        bordered
        :columns="bookListColumns"
        :data-source="bookListData"
        :loading="loading"
        :scroll="{x: 1500 , y: 500 }"
        :pagination="pagination"
        :row-key="record => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <!-- 金额 输入框
        <template slot="money" slot-scope="text, record, index">
          <a-form-model-item prop="money" :rules="rules.money" required>
            <a-input style="width:100%" type="text" v-model="record.money" />
          </a-form-model-item>
        </template> -->

        <!-- 单据状态 -->
        <span slot="documentStateSlot" slot-scope="text, record">
          <a-tag color="orange" v-if="record.theDocumentsState == '0'">仅保存</a-tag>
          <a-tag color="green" v-if="record.theDocumentsState == '1'">已提交</a-tag>
        </span>
        <!-- if  已提交 -->
        <!-- <span slot="documentStateSlot">
          <a-tag color="#2db7f5">已提交</a-tag>
        </span> -->

        <!-- 操作 -->
        <span slot="operationSlot" slot-scope="text, record">
          <a @click="handleExportXls(record)" style="color:green;">结汇发票打印</a>
          <a-divider type="vertical" />

          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay" v-if="record.theDocumentsState == '0'">
              <!-- <a-menu-item><a @click="submit(record)">提交</a></a-menu-item> -->
              <a-menu-item><a @click="edit(record,'2')">详情</a></a-menu-item>
              <a-menu-item><a @click="edit(record,'1')">编辑</a></a-menu-item>
              <a-menu-item><a @click="print(record)">打印</a></a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" ok-text="是" cancel-text="否" @confirm="handleDelete(record.id)">
                  <a href="javascript:void(0);" style="color:red;">删除</a>
                </a-popconfirm></a-menu-item
              >
            </a-menu>
             <a-menu slot="overlay" v-if="record.theDocumentsState == '1'">
              <!-- <a-menu-item><a @click="canaleSubmit(record)">取消提交</a></a-menu-item> -->
              <a-menu-item><a @click="edit(record,'2')">详情</a></a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>

    <!-- 抽屉 -->
    <addBook-drawer ref="addBookDrawer"  @ok="modalFormOk" @back="getBookList"></addBook-drawer>
    <detailsBook-drawer ref="detailsBookDrawer"  @ok="modalFormOk"></detailsBook-drawer>
    <!-- <editBook-drawer ref="editBookDrawer" :father="cc" @ok="modalFormOk"></editBook-drawer> -->
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'

import { downFile } from '@/api/manage'

import addBookDrawer from '@views/book/addBookDrawer.vue'
import detailsBookDrawer from '@views/book/detailsBookDrawer.vue'
import editBookDrawer from '@views/book/editBookDrawer.vue'
import {bookList,bookListId,submitPackingList,deletePackingList,settlement} from '@api/document/book.js'

export default {
  name: 'BookList', // 托书列表
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment, addBookDrawer, detailsBookDrawer, editBookDrawer },
  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} />
    return {
      // 表头
      bookListColumns: [
        { title: '托书号', width: 120, dataIndex: 'shippingOrderNumber',  className: 'replacecolor' , ellipsis: true,},
        { title: '订单号', width: 140, dataIndex: 'orderNumber', className: 'replacecolor', ellipsis: true, },
        { title: '外销发票号码', dataIndex: 'exportInvoiceNo', width: 120, className: 'replacecolor', ellipsis: true, },
        { title: '客户简称', dataIndex: 'clientAbbreviation', width: 120, className: 'replacecolor' , ellipsis: true,},
        {
          title: '分销点',
          dataIndex: 'distributionPoint',
          width: 120,
           ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '经营单位',
          dataIndex: 'unitInOperation',
          width: 150,
           ellipsis: true,
          className: 'replacecolor'
        },
        { title: '总箱数', dataIndex: 'boxNumber', width: 120, className: 'replacecolor', ellipsis: true, },
        { title: '总毛重', dataIndex: 'totalGrossWeight', width: 120, className: 'replacecolor', ellipsis: true, },
        { title: '总体积', dataIndex: 'totalVolume', width: 100, className: 'replacecolor', ellipsis: true, },
        {
          title: '金额',
          dataIndex: 'money',
          // scopedSlots: { customRender: 'money' },
          width: 140,
           ellipsis: true,
          className: 'replacecolor'
        },

        {
          title: '实际船期',
          dataIndex: 'theFinalShippingDate',
           ellipsis: true,
          width: 130,
          // customRender: text => { 有问题 显示当日日期
          //   return moment(text).format('YYYY-MM-DD')
          // },
          className: 'replacecolor'
        },
        // { title: '收汇方式', dataIndex: 'exchangeEarningsValue', width: 120, className: 'replacecolor' },
        // {
        //   title: '收货人',
        //   dataIndex: 'consignee',
        //   width: 100,
        //   customRender: t => ellipsis(t),
        //   className: 'replacecolor'
        // },
        // {
        //   title: '提单或承运收据',
        //   dataIndex: 'billOfLadingOrCarriageReceipt',
        //   width: 140,
        //   customRender: t => ellipsis(t),
        //   className: 'replacecolor'
        // },
        // {
        //   title: '抬头人',
        //   dataIndex: 'addressee',
        //   width: 120,
        //   customRender: t => ellipsis(t),
        //   className: 'replacecolor'
        // },
        // { title: '贸易国别', dataIndex: 'tradeCountry', width: 100, className: 'replacecolor' },
        // { title: '运抵国别', dataIndex: 'arriveInCountry', width: 100, className: 'replacecolor' },
        // { title: '出口口岸', dataIndex: 'exportPort', width: 100, className: 'replacecolor' },
        // {
        //   title: '通知人',
        //   dataIndex: 'notifier',
        //   width: 100,
        //   customRender: t => ellipsis(t),
        //   className: 'replacecolor'
        // },
        // { title: 'NOTIFY', dataIndex: 'nottfy', width: 150, className: 'replacecolor' },
        // { title: '目的港', dataIndex: 'destinationPort', width: 100, className: 'replacecolor' },
        // { title: '装运期限', dataIndex: 'latestDateOfShipment', width: 100, className: 'replacecolor' },
        // {
        //   title: '单据状态',
        //   dataIndex: 'theDocumentsState',
        //   width: 90,
        //   scopedSlots: { customRender: 'documentStateSlot' },
        //   fixed: 'right',
        //   className: 'replacecolor'
        // },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operationSlot' },
          width: 180,
          fixed: 'right',
          className: 'replacecolor'
        }
      ],
      bookListData: [],
      id: '',
      selectedRowKeys:[],//选中行id
      loading: false, // 表格加载
      pagination:{},
      // 查询条件
      queryParam: {
        pageNo:''
      }
    }
  },

  created() {
    this.getBookList( )
  },
  methods: {
    moment,
    //获取表格数据
    getBookList(){
      this.$nextTick(() => {
        this.queryParam.pageSize = 20
        bookList(this.queryParam).then(res => {
          if (res.success) {
           this.bookListData = res.result.records
           this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }else {
             this.$message.error(res.message);
          }
        })
      })
    },
    // 查询按钮
    searchQuery() {
      this.toggleSearchStatus = false
      this.queryParam.pageNo = ''
      this.getBookList()
      // this.getpreBookList() // 渲染渲染预托书
    },
    // 重置
    searchReset() {
      this.queryParam = {
        pageNo:''
      }
      this.getBookList()
      // this.getpreBookList()
    },
    // 新增
    addBookDrawer() {
      console.log('新增托书')
      this.$refs.addBookDrawer.visible = true
    },

    // -----------------------------------
    // 结汇发票打印
    handleExportXls(record) {
     
       var exinvoiceArr = ["报关信息-报关单","报关信息-申报要素-成衣","报关信息-发票","报关信息-合同","报关信息-申报要素-面料","报关信息-装箱单"]

       exinvoiceArr.map(item =>{
      downFile('/syShippingOrder/syShippingOrder/syShippingOrderPrint',{id:record.id,testName:item}).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), item + '.xlsx')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', item + '.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
})
    },
    // 上传附件
    accessoryUpload() {},
    // 操作 提交
    submit(record) {
      this.$nextTick(() => {
        submitPackingList({ id: record.id, type: '1' }).then(res => {
          if (res.success) {
          //  record.theDocumentsState = '1'
           this.getBookList()
           this.$message.success('提交成功')
          }else {
             this.$message.error(res.message);
          }
        })
      })
    },
    // //批量提交
    batchSubmit(){
      this.$nextTick(() => {
        submitPackingList({ ids: this.selectedRowKeys, type: '1' }).then(res => {
          if (res.success) {
          //  record.theDocumentsState = '1'
           this.getBookList()
           this.$message.success('提交成功')
           this.selectedRowKeys = []
          }else {
             this.$message.error(res.message);
          }
        })
      })

    },
    //取消提交
    canaleSubmit(record) {
      this.$nextTick(() => {
        submitPackingList({ id: record.id, type: '2' }).then(res => {
          if (res.success) {
           this.$message.success('取消提交成功')
           record.theDocumentsState = '0'
           this.getBookList()
          }else {
             this.$message.error(res.message);
          }
        })
      })
    },
    batchCanaleSubmit(){
      this.$nextTick(() => {
        submitPackingList({ ids: this.selectedRowKeys, type: '2' }).then(res => {
          if (res.success) {
           this.$message.success('取消提交成功')
          //  record.theDocumentsState = '0'
           this.getBookList()
           this.selectedRowKeys = []
          }else {
             this.$message.error(res.message);
          }
        })
      })
    },
    // 操作 详情
    details(record) {
      this.$refs.detailsBookDrawer.visible = true
    },
    // 操作 编辑
    edit(record,pattern) {
      this.$nextTick(() => {
        bookListId({id:record.id}).then(res => {
          if (res.success) {
            var data =res.result
            if(pattern == '1'){
              this.$refs.addBookDrawer.visible = true
              this.$refs.addBookDrawer.defaultMethod = 'edit'
              data.latestDateOfShipment = (moment(data.latestDateOfShipment)).format('YYYY-MM-DD')
              this.$refs.addBookDrawer.addBook = data
              this.$refs.addBookDrawer.addBookData = data.syShippingOrderItemList
              this.$refs.addBookDrawer.addBookData.map(item =>{
                item.smallPo = item.smailPo
                item.itemNumber = item.styleNumber
                item.totalVolume = item.volume
                item.price = item.unitPrice
              })
              this.$refs.addBookDrawer.rmbList = data.rmbList
              this.$refs.addBookDrawer.usdList = data.usdList
            }else {
              this.$refs.detailsBookDrawer.visible = true
              data.latestDateOfShipment = (moment(data.latestDateOfShipment)).format('YYYY-MM-DD')
              this.$refs.detailsBookDrawer.addBook = data
              this.$refs.detailsBookDrawer.addBookData = data.syShippingOrderItemList
              this.$refs.detailsBookDrawer.rmbList = data.rmbList
              this.$refs.detailsBookDrawer.usdList = data.usdList
            }
             
          //  this.bookListData = res.result.records
          }else {
             this.$message.error(res.message);
          }
        })
      })
    },
    // 操作 打印
    print(record) {},
    // 操作 删除
    handleDelete(id) {
        this.$nextTick(() => {
          if(this.queryParam.pageNo > 1 && this.bookListData.length === 1){
          this.queryParam.pageNo = this.queryParam.pageNo -1
        }
        deletePackingList({id:id}).then(res => {
          if (res.success) {
            this.getBookList()
          }else {
             this.$message.error(res.message);
          }
        })
      })
    },

    // // 分页、排序、筛选变化时触发
    handleTableChange(pagination, filters, sorter) {
      // console.log('当前页信息>>>>',pagination)
      this.queryParam.pageNo = pagination.current
      this.getBookList()
    },
    // 选中行
    onSelectChange(keys) {
      this.selectedRowKeys = keys
    }
    
  },
  computed: {},
  mounted() {}
}
</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;
}

// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
</style>
