<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.depositaryReceiptNo"></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.shippingOrderDate" @change = "changeTime"> </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.createBy"></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.smallPo"></a-input>
                </a-form-item>
              </a-col>

              <!-- <a-col :md="6" :sm="8">
                <a-form-item label="预托书号">
                  <a-input placeholder="请输入预托书号" v-model="queryParam.depositaryReceiptNo"></a-input>
                </a-form-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="addPreBook" icon="plus">新增</a-button>
      </div>

      <!-- 子表 -->
      <a-table
        bordered
        v-if="preBookListData"
        :rowKey=" (row, index) => {return index;}"
        :columns="preBookListColumns"
        :data-source="preBookListData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        :scroll="{ x: 1500,y: 500 }"
      >

        <!-- 单据状态 -->
        <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>

        <!-- 操作 -->
        <span slot="operationSlot" slot-scope="text, record">
          <a @click="print(record)">打印</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="details(record)">详情</a></a-menu-item>
              <a-menu-item><a @click="edit(record)">编辑</a></a-menu-item>
              <!-- <a-menu-item>
                <a-popconfirm title="确定提交吗?" ok-text="是" cancel-text="否" @confirm="submit(record)">
                  <a href="javascript:void(0);" style="color:green;">提交</a>
                </a-popconfirm>
              </a-menu-item> -->
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" ok-text="是" cancel-text="否" @confirm="handleDelete(record)">
                  <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="details(record)">详情</a></a-menu-item>
              <!-- <a-menu-item>
                <a-popconfirm title="确定取消提交吗?" ok-text="是" cancel-text="否" @confirm="cancel(record)">
                  <a href="javascript:void(0);" style="color:red;">取消提交</a>
                </a-popconfirm>
              </a-menu-item> -->
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>

    <!-- 抽屉 -->
    <addPreBook-drawer ref="addPreBookDrawer" :fatherList="getPreBookData" @ok="modalFormOk"></addPreBook-drawer>
    <detailsPreBook-drawer ref="detailsPreBookDrawer" @ok="modalFormOk"></detailsPreBook-drawer>
  </div>
</template>

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

import addPreBookDrawer from '@views/pre-book/addPreBookDrawer.vue'
import detailsPreBookDrawer from '@views/pre-book/detailsPreBookDrawer.vue'
import editPreBookDrawer from '@views/pre-book/editPreBookDrawer.vue'

import { preBookList, addPreBook, preBookById, editById,deletePreBook,submit,cancelSubmit } from '@api/document/pre-book.js'

export default {
  name: 'PreBookList', // 预托书列表
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment, addPreBookDrawer, detailsPreBookDrawer, editPreBookDrawer },
  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略

    return {
      // 表头
      preBookListColumns: [
        {
          title: '预托书号',
          width: 220,
          dataIndex: 'depositaryReceiptNo',
          // fixed: 'left',
          align: 'left',
          className: 'replacecolor'
        },
        // { title: '订单号', width: 120, dataIndex: 'orderNumber', fixed: 'left', className: 'replacecolor' },
        // { title: '外销发票号码', dataIndex: 'exportInvoiceNo', width: 220, align: 'left', className: 'replacecolor' },
        { title: '客户简称', dataIndex: 'clientAbbreviation', width: 120, className: 'replacecolor' },
        {
          title: '分销点',
          dataIndex: 'distributionPoint',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '经营单位',
          dataIndex: 'unitInOperation',
          width: 150,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        { title: '总箱数', dataIndex: 'boxNumber', width: 120, className: 'replacecolor' },
        { title: '总毛重', dataIndex: 'totalGrossWeight', width: 120, className: 'replacecolor' },
        { title: '总体积', dataIndex: 'totalVolume', width: 150, className: 'replacecolor' },
        {
          title: '金额',
          dataIndex: 'money',
          width: 140,
          className: 'replacecolor'
        },
        //  {
        //   title: '创建时间',
        //   dataIndex: 'createTime',
        //   align: 'center',
        //   sorter: true,
        //   customRender: text => {
        //     return moment(text).format('YYYY-MM-DD')
        //   }
        // },
        // { title: '实际船期', dataIndex: 'theActualShippingDate', width: 120, 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: 'nottfy', dataIndex: 'nottfy', width: 150, className: 'replacecolor' },
        { title: '目的港', dataIndex: 'destinationPort', width: 100, className: 'replacecolor' },
        // { title: '装运期限', dataIndex: 'latestDateOfShipment', width: 120, className: 'replacecolor' },
        {
          title: '单据状态',
          dataIndex: 'theDocumentsState',
          width: 90,
          scopedSlots: { customRender: 'documentStateSlot' },
          // fixed: 'right',
          className: 'replacecolor'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operationSlot' },
          width: 160,
          fixed: 'right',
          className: 'replacecolor'
        }
      ],
      preBookListData: [],
      id: '', //
      loading: false, // 表格加载

      // 查询条件
      queryParam: {
        depositaryReceiptNo: '',
        shippingOrderDate: '',//托书日期
        clientAbbreviation: '',
        // createBy: '',
        smallPo: '',
        pageNo: '' // 点击的页数
      },
      pagination: {
        // total: '',
        // current: 0,
        // pageSize: 0
      },
      dateFormat: 'YYYY-MM-DD',
      
    }
  },

  created() {
    this.getPreBookData() // 渲染预托书
  },
  methods: {
    // 分页查询 预托书
    getPreBookData() {
      this.$nextTick(() => {
        this.queryParam.pageSize = 20
        preBookList(this.queryParam).then(res => {
          if (res.success) {
            this.preBookListData = res.result.records;
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }else{
            this.$message.error(res.message)
          }
        })
      })
    },
    // 新增
    addPreBook() {
      this.$refs.addPreBookDrawer.addPreBook = {syLetterDepositItemList:[],syRmbList:[],syUsdList:[]};
      this.$refs.addPreBookDrawer.visible = true;
      
    },

    //  详情
    details(record) {
      preBookById({ id: record.id }).then(res => {
        if (res.success) {
          // 把通过id查询到的对象，赋值给子组件
          this.$refs.detailsPreBookDrawer.preBookDetails = res.result; //主表
          res.result.money = res.result.money.toFixed(2)
          res.result.syLetterDepositItemList.map(item =>{
             var str =item.hod
              var n=str.split(" ");
              item.hod = n[0]
            item.preDeliveryDate = item.hod
          })
          this.$refs.detailsPreBookDrawer.data = res.result.syLetterDepositItemList; //子表
          this.$refs.detailsPreBookDrawer.visible = true
        }else{
          this.$message.error(res.message)
        }
      })
    },

    //  编辑
    edit(record) {
      preBookById({ id: record.id }).then(res => {
        if (res.success) {
          res.result.consigneeAll = res.result.consignee+"+"+res.result.consigneeAddress+"+"+res.result.notifyParty+"+"+res.result.notifyPartyAddress;
          res.result.money = res.result.money.toFixed(2)
          this.$refs.addPreBookDrawer.addPreBook = res.result;
          this.$refs.addPreBookDrawer.defultMethod = 'edit';
          res.result.syLetterDepositItemList.map(item =>{
             var str =item.hod
              var n=str.split(" ");
              item.hod = n[0]
            item.preDeliveryDate = item.hod
          })
          this.$refs.addPreBookDrawer.data = res.result.syLetterDepositItemList;
          this.$refs.addPreBookDrawer.visible = true
        }else{
          this.$message.error(res.message)
        }
      })
    },

    // 操作 提交
    submit(record) {
      this.$nextTick(() => {
        submit({ id: record.id, type: '1' }).then(res => {
          if (res.success) {
            this.preBookListData.theDocumentsState == '1'
            this.getPreBookData()
            this.$message.success('提交成功')
          }else{
            this.$message.error(res.message)
          }
        })
      })
    },
    // 取消提交
    cancel(record) {
      this.$nextTick(() => {
        cancelSubmit({ id: record.id, type: '2' }).then(res => {
          if (res.success) {
            this.preBookListData.theDocumentsState == '0'
            this.getPreBookData()
            this.$message.success('取消提交成功')
          }else{
            this.$message.error(res.message)
          }
        })
      })
    },
    //  删除
    handleDelete(record) {
      this.$nextTick(() => {
        if(this.queryParam.pageNo > 1 && this.preBookListData.length === 1){
          this.queryParam.pageNo = this.queryParam.pageNo -1
        }
        deletePreBook({ id: record.id }).then(res => {
          if (res.success) {
          this.getPreBookData()
          this.$message.success('删除成功')
          }else{
            this.$message.error(res.message)
          }
        })
      })
    },
    
    //托书日期改变触发
    changeTime(val){
      this.queryParam.shippingOrderDate= val.format('YYYY-MM-DD')
    },

    // 查询按钮
    searchQuery() {
      this.toggleSearchStatus = false
      this.getPreBookData() /// 渲染渲染预托书
    },
    // 重置
    searchReset() {
      this.queryParam = {
        depositaryReceiptNo: '',
        shippingOrderDate: '',//托书日期
        clientAbbreviation: '',
        // createBy: '',
        smallPo: '',
        pageNo: '' // 点击的页数
      }
      this.getPreBookData()
    },

    // 操作 打印
    print(record) {},

    handleTableChange(pagination, filters, sorter) {
      this.queryParam.pageNo = pagination.current
      this.getPreBookData()
    },
    aa() {},
    bb() {},
    cc() {}
  },
  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>
