<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.index"></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.planNum"></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">
              <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-card :bordered="false" style=" marginTop:10px;">
      <a-table
        bordered
        rowKey="id"
        :columns="costAllocationColumns"
        :data-source="costAllocationData"
        :loading="loading"
        :pagination="pagination"
        :scroll="{ x: 1500 }"
        @change="handleTableChange"
      >
        <!-- 单据状态 -->
        <span slot="billstatus" slot-scope="text, record">
          <a-tag color="orange" v-if="record.billstatus == '0'">未保存</a-tag>
          <a-tag color="green" v-if="record.billstatus == '1'">仅保存</a-tag>
          <a-tag color="blue" v-if="record.billstatus == '2'">已审批</a-tag>
        </span>

        <!-- 操作 -->

        <span slot="operationSlot" slot-scope="text, record" v-if="record.billstatus == '1'">
          <a href="javascript:void(0);" @click="detail(record)">详情</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定审核吗?" ok-text="是" cancel-text="否" @confirm="check(record)">
            <a href="javascript:void(0);" style="color:green">审核</a>
          </a-popconfirm>
        </span>

        <span slot="operationSlot" slot-scope="text, record" v-else-if="record.billstatus == '2'">
          <a href="javascript:void(0);" @click="detail(record)">详情</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定取消审核吗?" ok-text="是" cancel-text="否" @confirm="cancelCheck(record)">
            <a href="javascript:void(0);" style="color:red">取消审核</a>
          </a-popconfirm>
        </span>

        <span slot="operationSlot" slot-scope="text, record" v-else-if="record.billstatus == '0'">
          <a href="javascript:void(0);" @click="detail(record)">详情</a>
        </span>
      </a-table>
    </a-card>

    <!-- 详情 大抽屉 -->
    <costDetail-drawer ref="costDetailDrawer" :fatherList="getCostList" @ok="modalFormOk"></costDetail-drawer>
  </div>
</template>

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

import costDetailDrawer from '@views/cost-allocation-total/costDetailDrawer.vue'

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

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

  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} />
    return {
      // 成本分配汇总 表头
      costAllocationColumns: [
        {
          title: '序号',
          width: 90,
          dataIndex: 'index',
          customRender: (text, record, index) => `${index + 1}`,
          fixed: 'left',
          className: 'replacecolor'
        },
        { title: '计划单号', width: 120, dataIndex: 'planNum', fixed: 'left', className: 'replacecolor' },
        { title: '款号', width: 120, dataIndex: 'poStyleNum', className: 'replacecolor' },
        { title: '业务员', width: 120, dataIndex: 'exportSales', className: 'replacecolor' },
        {
          title: '部门',
          width: 120,
          dataIndex: 'department',
          className: 'replacecolor'
        },
        { title: '客户简称', width: 150, dataIndex: 'customerShortame', className: 'replacecolor' },
        { title: '加工单位', width: 220, dataIndex: 'processUnit', align: 'left', className: 'replacecolor' },
        //  {
        //   title: '创建时间',
        //   dataIndex: 'createTime',
        //   align: 'center',
        //   sorter: true,
        //   customRender: text => {
        //     return moment(text).format('YYYY-MM-DD')
        //   }
        // },
        {
          title: '出运日期',
          width: 120,
          dataIndex: 'outdata',
          customRender: text => {
            return moment(text).format('YYYY-MM-DD')
          },
          className: 'replacecolor'
        },
        { title: '成衣件数', width: 90, dataIndex: 'actualShipquantity', className: 'replacecolor' },
        { title: '销售美元', width: 120, dataIndex: 'usdTotalexportprice', className: 'replacecolor' },
        { title: '销售人民币', width: 120, dataIndex: 'rmbExportamount', className: 'replacecolor' },
        { title: '面料(不含税)', width: 120, dataIndex: 'fabriccostNotincludestax', className: 'replacecolor' },
        { title: '辅料(不含税)', width: 120, dataIndex: 'excipiencostNotincludestax', className: 'replacecolor' },
        { title: '加工费(CNY)', width: 120, dataIndex: 'rmbProcesscost', className: 'replacecolor' },
        { title: '加工费(USD)', width: 120, dataIndex: 'usdProcesscost', className: 'replacecolor' },
        { title: '费用支出(汇总CNY)', width: 160, dataIndex: 'rmbExpense', className: 'replacecolor' },
        { title: '费用支出(汇总USD)', width: 160, dataIndex: 'usdExpense', className: 'replacecolor' },
        { title: '事故单人民币金额', width: 160, dataIndex: 'accidentcnyAmount', className: 'replacecolor' },
        {
          title: '备注',
          width: 180,
          dataIndex: 'remarks',
          customRender: t => ellipsis(t),
          className: 'replacecolor',
          scopedSlots: { customRender: 'remarks' }
        },
        {
          title: '单据状态',
          width: 120,
          dataIndex: 'billstatus',
          className: 'replacecolor',
          fixed: 'right',
          scopedSlots: { customRender: 'billstatus' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operationSlot' },
          fixed: 'right',
          width: 160,
          className: 'replacecolor'
        }
      ],
      costAllocationData: [],
      // 查询条件
      queryParam: {
        index: '',
        planNum: '',
        customerShortame: '',
        pageNo: '' // 点击的页数
      },
      // 分页
      pagination: {
        // total: '',
        // current: 0,
        // pageSize: 0
      },
      dateFormat: 'YYYY-MM-DD',
      loading: false // 表格加载
    }
  },
  created() {
    this.getCostList()
  },
  methods: {
    // 分页查询
    getCostList() {
      this.$nextTick(() => {
        costList(this.queryParam).then(res => {
          if (res.success) {
            this.costAllocationData = res.result.records
            console.log('成本分配汇总数据', this.costAllocationData)
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }
        })
      })
    },
    //  详情
    detail(record) {
      this.$refs.costDetailDrawer.visible = true
      this.$refs.costDetailDrawer.record = record //接口参数
      // console.log('参数record', this.$refs.costDetailDrawer.record.planNum)
      this.$refs.costDetailDrawer.getALLDetail()
    },
    //  审核
    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('取消审核成功')
          }
        })
      })
    },

    searchQuery() {
      this.getCostList()
    },
    searchReset() {
      this.queryParam.pageNo = ''
      this.queryParam = {}
      this.getCostList()
    },

    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>
