<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.aa"> </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.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="id"
        :columns="preBookListColumns"
        :data-source="preBookListData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        :scroll="{ x: 1500 }"
      >
        <!-- 金额 输入框-->
        <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>

        <!-- 操作 -->
        <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" :father="aa" @ok="modalFormOk"></addPreBook-drawer>
    <detailsPreBook-drawer ref="detailsPreBookDrawer" :father="bb" @ok="modalFormOk"></detailsPreBook-drawer>
    <editPreBook-drawer ref="editPreBookDrawer" :father="cc" @ok="modalFormOk"></editPreBook-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 } 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: 100, className: 'replacecolor' },
        {
          title: '金额',
          dataIndex: 'money',
          scopedSlots: { customRender: '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: {
        orderNumber: '',
        aa: '',//订单日期
        clientAbbreviation: '',
        containerNumber: '',
        smallPo: '',
        depositaryReceiptNo: '',
        pageNo: '' // 点击的页数
      },
      pagination: {
        // total: '',
        // current: 0,
        // pageSize: 0
      },
      dateFormat: 'YYYY-MM-DD',
      
    }
  },

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

    //  详情
    details(record) {
      
      // console.log('点击项的ID', record.id)
      preBookById({ id: record.id }).then(res => {
        if (res.success) {
          // 把通过id查询到的对象，赋值给子组件
          this.$refs.detailsPreBookDrawer.preBookDetails = res.result; //主表
          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) {
          this.$refs.addPreBookDrawer.addPreBook = res.result;
          this.$refs.addPreBookDrawer.data = res.result.syLetterDepositItemList;
          this.$refs.editPreBookDrawer.visible = true
        }else{
          this.$message.error(res.message)
        }
      })
    },

    // 操作 提交
    submit(record) {
      console.log('点击id:', record.id)
      this.$nextTick(() => {
        submit({ id: record.id, type: '1' }).then(res => {
          if (res.success) {
            console.log('提交成功，单据状态改为【已提交】')
            this.preBookListData.theDocumentsState == '1'
            this.getPreBookData()
            this.$message.success('提交成功')
          }
        })
      })
    },
    // 取消提交
    cancel(record) {
      console.log('取消订单id:', record.id)
      this.$nextTick(() => {
        cancelSubmit({ id: record.id, type: '2' }).then(res => {
          if (res.success) {
            console.log('取消提交成功，单据状态改为【仅保存】')
            this.preBookListData.theDocumentsState == '0'
            this.getPreBookData()
            this.$message.success('取消提交成功')
          }
        })
      })
    },
    //  删除
    handleDelete(record) {
      console.log('点击删除项id:', record.id)
      this.$nextTick(() => {
        deletePreBook({ id: record.id }).then(res => {
          console.log('res:', res)
          this.getPreBookData()
          this.$message.success('删除成功')
        })
      })
    },

    // 查询按钮
    searchQuery() {
      this.getPreBookData() /// 渲染渲染预托书
    },
    // 重置
    searchReset() {
      this.queryParam = {}
      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>
