<template>
  <!-- 【单证】 成本分配汇总 -->
  <div id="costAllocationTotal">
    <!-- 查询区域 -->
    <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.preparedBy"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="计划单号">
                <a-input placeholder="请输入序号" v-model="queryParam.planNum"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="产品款号" has-feedback>
                <a-input placeholder="请输入" v-model="queryParam.poStyleNum"></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.exportSales"></a-input>
              </a-form-item>
            </a-col>
              <a-col :md="6" :sm="8">
              <a-form-item label="成衣合同号">
                <a-input placeholder="请输入" v-model="queryParam.garmentContractno"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="部门">
                <a-input placeholder="请输入" v-model="queryParam.department"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="客户简称">
                <a-input placeholder="请输入" v-model="queryParam.customerShortame"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="加工单位">
                <a-input placeholder="请输入" v-model="queryParam.processUnit"></a-input>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="8">
              <a-form-item label="出运日期">
                <a-date-picker style="width: 100%" v-model="outdata"  @change="changeData" :format="dateFormat"> </a-date-picker>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="计划数量">
                <a-input placeholder="请输入" v-model="queryParam.planQuantity"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="实际出货数量">
                <a-input placeholder="请输入" v-model="queryParam.actualShipquantity"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="短出数">
                <a-input placeholder="请输入" v-model="queryParam.shortseveral"></a-input>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="8">
            <a-form-item label="是否有驳回">
                <a-select v-model="queryParam.rejectDate">
                    <a-select-option value="">请选择</a-select-option>
                    <a-select-option value="1">是</a-select-option>
                    <a-select-option value="0">所有</a-select-option>
                  </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="审核人">
                <a-input placeholder="请输入" v-model="queryParam.approvalMan"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
            <a-form-item label="单据状态">
                <a-select v-model="queryParam.status">
                    <a-select-option value="">请选择</a-select-option>
                    <a-select-option value="保存">保存</a-select-option>
                    <a-select-option value="提交">提交</a-select-option>
                    <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-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>

    <!-- table区域  -->
    <a-spin :spinning="loading">
    <a-card :bordered="false" style=" marginTop:10px;">
      <div class="table-operator">
        <a-button type="primary" @click="exportList('成本分配列表')" icon="plus">导出</a-button>
      </div>
      <a-table
        bordered
        rowKey="id"
        :columns="costAllocationColumns"
        :data-source="costAllocationData"
        :loading="loading"
        :pagination="pagination"
        :scroll="{ x: 1000 }"
        @change="handleTableChange"
      >
        <!-- 操作 -->
        <!-- <span slot="option" slot-scope="text, record" >
          <a href="javascript:void(0);" @click="detail(record)">详情</a>
        </span> -->
        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleApply(record)" v-if="apply == 1 && (record.status=='保存' || record.status=='' || !record.status)">发起申请</a> -->
          <a @click="detail(record)">详情</a>
          <a-divider type="vertical"  />
          <a-dropdown>
          <a class="ant-dropdown-link">更多<a-icon type="down"/></a>
            <a-menu slot="overlay" >
              <a-menu-item><a @click="cancelSumit(record)">取消提交</a></a-menu-item>
              <a-menu-item><a @click="approvalHistory(record)">审批历史</a></a-menu-item>
            </a-menu>
            </a-dropdown>  
        </span>
      </a-table>
    </a-card>
  </a-spin>
    <ChistoryModal ref="ChistoryModal"></ChistoryModal>
    <!-- 详情 大抽屉 -->
    <costDetail-drawer ref="costDetailDrawer"   @close="getCostList"></costDetail-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 costDetailDrawer from '@views/cost-allocation-total/costDetailDrawer.vue'
import ChistoryModal from '@views/cost-allocation-total/modal/ChistoryModal'

import { costList, itemCheck, cancelItemCheck,cancelSubmit } from '@api/document/cost-allocation-total.js'

export default {
  name: 'CostAllocationTotal', // 成本分配汇总 单证
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment, costDetailDrawer,ChistoryModal },

  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} />
    return {
      // 成本分配汇总 表头
      costAllocationColumns: [
        {
          title: '计划单号',
          width: 90,
          dataIndex: 'planNum',
          // customRender: (text, record, index) => `${index + 1}`,
          // fixed: 'left',
          className: 'replacecolor',
           ellipsis: true
        },
        { title: '制单人', width: 120, dataIndex: 'preparedBy', ellipsis: true, className: 'replacecolor' },
        { title: '保存日期', width: 120, dataIndex: 'createTime', ellipsis: true, className: 'replacecolor' },
        { title: '审核人', width: 120, dataIndex: 'approvalMan', ellipsis: true, className: 'replacecolor' },
        { title: '审核日期', width: 120, dataIndex: 'approvalDate', ellipsis: true, className: 'replacecolor' },
        { title: '返单日期', width: 120, dataIndex: 'rejectDate', ellipsis: true, className: 'replacecolor' },
        { title: '结束日期', width: 120, dataIndex: 'finalApproveDate', ellipsis: true, className: 'replacecolor' },
        { title: '产品款号', width: 120, dataIndex: 'poStyleNum',  ellipsis: true, className: 'replacecolor' },
        { title: '外销员', width: 120, dataIndex: 'exportSales',  ellipsis: true,className: 'replacecolor' },
        {
          title: '部门',
          width: 120,
          dataIndex: 'department',
          ellipsis: true,
          className: 'replacecolor'
        },
        { title: '成衣合同号', width: 140, dataIndex: 'garmentContractno', ellipsis: true, className: 'replacecolor' },
        
        { title: '客户简称', width: 150, dataIndex: 'customerShortame', ellipsis: true, className: 'replacecolor' },
        { title: '加工单位', width: 220, dataIndex: 'processUnit', ellipsis: true, align: 'left', className: 'replacecolor' },
        {
          title: '出运日期',
          width: 120,
          dataIndex: 'outdata',
          customRender: text => {
            if(text!==''&&text){
              
              return moment(parseInt(text)).format('YYYY-MM-DD')
            }else{
              return ''
            }
            
          },
          className: 'replacecolor'
        },
        { title: '计划数量', width: 90, dataIndex: 'planQuantity', ellipsis: true, className: 'replacecolor' },
        { title: '实际出货数量', width: 120, dataIndex: 'actualShipquantity', ellipsis: true, className: 'replacecolor' },
        { title: '短出数', width: 100, dataIndex: 'shortseveral', ellipsis: true, className: 'replacecolor' },
        { title: '外销总价($)', width: 120, dataIndex: 'usdTotalexportprice', ellipsis: true, className: 'replacecolor' },
        { title: '外销金额(￥)', width: 120, dataIndex: 'rmbExportamount', ellipsis: true, className: 'replacecolor' },
        { title: '面料不含税金额(￥)', width: 160, dataIndex: 'fabriccostNotincludestax', ellipsis: true, className: 'replacecolor' },
        { title: '辅料不含税金额(￥)', width: 160, dataIndex: 'excipiencostNotincludestax', ellipsis: true, className: 'replacecolor' },
        { title: '加工费(￥)', width: 100, dataIndex: 'rmbProcesscost', ellipsis: true, className: 'replacecolor' },
        { title: '加工费($)', width: 100, dataIndex: 'usdProcesscost', ellipsis: true, className: 'replacecolor' },
        { title: '不含税运杂费(￥)', width: 145, dataIndex: 'rmbExpense', ellipsis: true, className: 'replacecolor' },
        { title: '运杂费($)', width: 100, dataIndex: 'usdExpense', ellipsis: true, className: 'replacecolor' },
        { title: '事故金额(￥)', width: 120, dataIndex: 'accidentcnyAmount', ellipsis: true, className: 'replacecolor' },
        { title: '事故金额($)', width: 120, dataIndex: 'accidentUsdamount', ellipsis: true, className: 'replacecolor' },
        { title: '备注', width: 120, dataIndex: 'remarks', ellipsis: true, className: 'replacecolor' },
        { title: '状态', width: 120,fixed: 'right', dataIndex: 'status', ellipsis: true, className: 'replacecolor' },

        { title: '操作', width: 120 ,fixed: 'right', dataIndex: 'option', ellipsis: true, className: 'replacecolor', scopedSlots: { customRender: 'action' } },
      ],
      costAllocationData: [],
      outdata:'',
      dateFormat: 'YYYY-MM-DD',
      // 查询条件
      queryParam: {
        
      },
      // 分页
      pagination: {
        // total: '',
        // current: 0,
        // pageSize: 0
      },
      apply:"",
      dateFormat: 'YYYY-MM-DD',
      loading: false // 表格加载
    }
  },
  created() {
    this.apply = this.$route.query.apply;
    console.log(this.$route.query)
    this.getCostList()
    document.title = '成本分配列表'+ ' · ' + '森语'
  },
  methods: {
    // 分页查询
    getCostList() {
      this.$nextTick(() => {
        costList(this.queryParam).then(res => {
          if (res.success) {
            this.costAllocationData = res.result.records
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }else{
            this.$message.error(res.message);
          }
        })
      })
    },
    cancelSumit(record){
      var syApprovalHistory={
        stuta:'取消提交',
        planNo:record.planNum,
        processingOpinion:'',
        processedBy:this.$store.getters.userInfo.realname,
        processingTime:moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        assignedBy:''
      }
      cancelSubmit({planNum:record.planNum,status:record.status,currentState:'取消提交',syApprovalHistory:syApprovalHistory}).then(res => {
          if (res.success) {
            this.getCostList()
            this.$message.success('取消提交成功')
          }else{
            this.$message.error(res.message);
          }
        })
    },
    //  详情
    detail(record) {
      this.$refs.costDetailDrawer.detailModVis = true
      this.$refs.costDetailDrawer.planNo = record.planNum
      this.$refs.costDetailDrawer.status = record.status
      // console.log('参数record', this.$refs.costDetailDrawer.record.planNum)
      this.$refs.costDetailDrawer.onSearch(record.planNum)
    },
    //  审核
    check(record) {
      this.$nextTick(() => {
        itemCheck({ plannum: record.planNum }).then(res => {
          if (res.success) {
            console.log('提交成功，单据状态改为【已审核】')
            this.costAllocationData.billstatus == '2'
            this.getCostList()
            this.$message.success('审核成功')
          }
        })
      })
    },
    //  取消审核
    cancelCheck(record) {
      this.$nextTick(() => {
        cancelItemCheck({ plannum: record.planNum }).then(res => {
          if (res.success) {
            console.log('提交成功，单据状态改为【仅保存】')
            this.costAllocationData.billstatus == '1'
            this.getCostList()
            this.$message.success('取消审核成功')
          }
        })
      })
    },
    approvalHistory(record){
      this.$refs.ChistoryModal.histiryModVis = true
      this.$refs.ChistoryModal.getHistoryList(record.planNum)
    },

    searchQuery() {
      this.queryParam.pageNo=''
      this.getCostList()
    },
    changeData(){
      var dd =moment(this.outdata).format('YYYY-MM-DD')
      dd = dd+' '+'00:00:00'
      this.queryParam.outdata = moment(dd).valueOf()
    },
    searchReset() {
      this.queryParam.pageNo = ''
      this.queryParam = {}
      this.getCostList()
    },
    handleApply(record){
      var data = {
        type:"成本表",
        ccode:record.planNum,
      };
      window.parent.postMessage({msg:"startApply", data:data}, "*");
    },
    //导出
    exportList(fileName){
      var params = this.queryParam
      this.loading = true
      downFile('/orderData/syCostLossReview/exportXls', params).then(data => {
      this.loading = false
        console.log('888')
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.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', fileName + '.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    },

    handleTableChange(pagination, filters, sorter) {
      this.queryParam.pageNo = pagination.current
      this.getCostList()
    },
    // father 抽屉方法
    aa() {}
  },
  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>
