<template>
  <!-- 齐套率报表 -->
  <div id="fullratetable">
    <!-- 查询 -->
    <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="开始年月">
                <!--  :disabled-date="disabledDate" 不可选择的日期 -->
                <a-date-picker
                  placeholder="请选择开始年月日"
                  format="YYYY-MM-DD"
                  style="width:100%;"
                  v-model="queryParam.beginDate"
                   @change="orderDataChange"
                   :allowClear="false"
                />
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="结束年月">
                <a-date-picker
                  placeholder="请选择结束年月"
                  format="YYYY-MM-DD"
                  style="width:100%;"
                  v-model="queryParam.endDate"
                   @change="orderDataChange"
                   :allowClear="false"
                />
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="区间(天)">
                <a-input placeholder="请输入区间" v-model="queryParam.section"></a-input>
              </a-form-item>
            </a-col>

            <template v-if="toggleSearchStatus">
              <a-col :md="6" :sm="8">
                <a-form-item label="部门">
                  <j-search-select-tag
                  placeholder="请选择业务员"
                  v-model="queryParam.department"
                  dict="view_department,department,department">
                 </j-search-select-tag> 
                  <!-- <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.fullSet"></a-input>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="来源订单号">
                  <a-input placeholder="请输入来源订单号" v-model="queryParam.sourceOrderNo"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <a-form-item label="订单编号">
                  <a-input placeholder="请输入订单编号" v-model="queryParam.subcontractOrderNo"></a-input>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="颜色">
                  <a-input placeholder="请输入颜色" v-model="queryParam.color"></a-input>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="业务员">
                   <j-search-select-tag
                  placeholder="请选择业务员"
                  v-model="queryParam.salesman"
                  dict="view_salesman,salesman,salesman">
                 </j-search-select-tag> 
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="小po">
                   <a-input placeholder="请输入小po" v-model="queryParam.smllPo"></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" icon="bell" @click="emailMsgMod">维护邮件发送通知</a-button>
        <a-button type="primary" @click="checkMonth" icon="line-chart">查看分析图-月份</a-button>
        <a-button type="primary" @click="checkdepartment" icon="bar-chart">查看分析图-部门</a-button>
      </div>

      <!-- table rowKey="id" -->
      <a-table
        bordered
        :columns="fullrateColumns"
        :pagination="pagination"
        :data-source="fullrateData"
        :loading="loading"
        :row-key="record => record.id"
        :scroll="{ x: 1500 }"
        @change="handleTableChange"
      >
      <!-- 操作 -->
        <span slot="operationSlot" slot-scope="text, record">
          <a-button type="primary" @click="lineDetail(record)">明细</a-button>
        </span>
      </a-table>
    </a-card>
    <!-- 维护邮件发送通知 :father="cc"-->
    <emailMsg-modal ref="emailMsgModal"></emailMsg-modal>
    <monthChart-modal ref="monthChartModal"></monthChart-modal>
    <departmentChart-modal ref="departmentChartModal"></departmentChart-modal>
    <Detail ref="detail"></Detail>
  </div>
</template>

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

import emailMsgModal from '@views/reportForms/full-rate-table/emailMsgModal.vue'
import monthChartModal from '@views/reportForms/full-rate-table/monthChartModal.vue'
import departmentChartModal from '@views/reportForms/full-rate-table/departmentChartModal.vue'
import Detail from '@views/reportForms/full-rate-table/detail.vue'
import { fullRateList,fullRateDestail } from '@api/reportForms/full-rate-table'
export default {
  name: 'Fullratetable', // 齐套率报表
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment, emailMsgModal, monthChartModal, departmentChartModal,Detail },

  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略

    return {
      // 齐套率统计表 表头
      fullrateColumns: [
        {
          title: '出库齐套',
          dataIndex: 'fullSetOfDelivery',
          width: 120,
          fixed: 'left',
          className: 'replacecolor'
        },
        {
          title: '来源订单号',
          dataIndex: 'sourceOrderNo',
          width: 160,
          fixed: 'left',
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        }, 
        {
          title: '款号',
          dataIndex: 'styleNumber',
          width: 120,
          className: 'replacecolor'
        },

        {
          title: '销售订单数量',
          dataIndex: 'salesOrderQuantity',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '订单编号',
          dataIndex: 'subcontractOrderNo',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '订单数量',
          dataIndex: 'subcontractOrderQuantity',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '交期',
          dataIndex: 'deliveryDate',
          width: 120,
          className: 'replacecolor',
        },
        {
          title: '部门',
          dataIndex: 'department',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '业务员',
          dataIndex: 'salesman',
          width: 120,

          className: 'replacecolor'
        },
        {
          title: '存货上游订单数量',
          dataIndex: 'salesOrderQuantity1',
          width: 170,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '累计出库数量',
          dataIndex: 'cumulativeIssueQuantity',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '累计准时出库数量',
          dataIndex: 'cumulativeOnTimeDeliveryQuantity',
          width: 170,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '累计延迟出库数量',
          dataIndex: 'cumulativeDelayedDeliveryQty',
          width: 170,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '未出库数量',
          dataIndex: 'quantityNotIssued',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '预计发货日期',
          dataIndex: 'estimatedDeliveryDate',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor',
        },

        {
          title: '最早出库日期',
          dataIndex: 'earliestIssueDate',
          width: 120,
          className: 'replacecolor',
        },
        {
          title: '最晚出库日期',
          dataIndex: 'latestIssueDate',
          width: 120,
          className: 'replacecolor',
          
        },
        {
          title: '出库率',
          dataIndex: 'deliveryRate',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '区间出库率',
          dataIndex: 'intervalDeliveryRate',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '齐套率',
          dataIndex: 'fullSetRate',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 90,
          fixed: 'right',
          className: 'replacecolor',
          scopedSlots: { customRender: 'operationSlot' },
        }
      ],
      fullrateData: [
      ],
      loading: false, // 加载状态

      // 查询条件
      queryParam: {
        beginDate:'',
        endDate:'',
        pageNo:''
      },
      pagination:{},

      url: {
        // syncUser: '/act/process/extActProcess/doSyncUser',
        list: '/sys/user/list',
        // delete: '/sys/user/delete',
        // deleteBatch: '/sys/user/deleteBatch',
        exportXlsUrl: '/sys/user/exportXls', // 导出
        importExcelUrl: 'sys/user/importExcel' // 导入
      }
    }
  },
  created() {
    // this.getfullrateData()
    this.getDefaultTime()
  },
  // html渲染出来后，操作dom
  mounted() {
    // this.loadingChart()
  },
  methods: {
    moment,
    getDefaultTime(){
      this.queryParam.beginDate=moment(new Date()).startOf("month").format("YYYY-MM-DD")
       var bb =new Date(this.queryParam.beginDate)
        bb.setDate(bb.getDate() + 7)
        this.queryParam.endDate = moment(bb).format("YYYY-MM-DD")
    },
    getfullrateData(){
      this.$nextTick(() => {
        this.loading = true
        fullRateList(this.queryParam).then(res => {
          this.loading = false
          if (res.success) {
            this.fullrateData = res.result.records;
            this.fullrateData.map(item=>{
              item.deliveryDate = (item.deliveryDate=='')?'':(moment(item.deliveryDate).format('YYYY-MM-DD'))
              item.earliestIssueDate = (item.earliestIssueDate=='')?'':(moment(item.earliestIssueDate).format('YYYY-MM-DD'))
              item.estimatedDeliveryDate = (item.estimatedDeliveryDate=='')?'':(moment(item.estimatedDeliveryDate).format('YYYY-MM-DD'))
              item.latestIssueDate = (item.latestIssueDate=='')?'':(moment(item.latestIssueDate).format('YYYY-MM-DD'))
            })
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              // pageSize: res.result.size
            }
          }else{
              this.$message.error(res.message);
          }
          
        })
      })
    },
    loadingChart() {
      setTimeout(() => {
        this.loading = false
      }, 500)
    },
    // 邮件通知
    emailMsgMod() {
      this.$refs.emailMsgModal.emailMsgModVis = true
    },
    // 分析图- 月份
    checkMonth() {
      console.log('打开齐套率月份统计图')
      this.$refs.monthChartModal.monthChartModVis = true
    },
    // 分析图- 部门
    checkdepartment() {
      console.log('打开部门齐套率统计图')
      this.$refs.departmentChartModal.departmentChartModVis = true
    },

    searchQuery() {
      if(this.queryParam.beginDate == '' || !this.queryParam.beginDate){
        this.$message.error('请选择开始日期');
      }else if(this.queryParam.endDate == '' || !this.queryParam.endDate){
         this.$message.error('请选择结束日期');
      }else{
        var separator = "-"; //日期分隔符  
        var beginDates = this.queryParam.beginDate.split(separator);  
        var endDates = this.queryParam.endDate.split(separator);  
        var beginDate = new Date(beginDates[0], beginDates[1]-1, beginDates[2]);  
        var endDate = new Date(endDates[0], endDates[1]-1, endDates[2]);  
        var timeInterval= parseInt(Math.abs(endDate - beginDate ) / 1000 / 60 / 60 /24) + 1;
        if(timeInterval > 31){
          this.$message.error('时间间隔超过31天，请重新选择！');
        }else { 
          this.getfullrateData() 
          this.toggleSearchStatus = false
        }       
      }
    },
    // 重置
    searchReset() {
      this.queryParam={}
      this.getDefaultTime()
      this.toggleSearchStatus = false
      this.fullrateData = []
      // this.getfullrateData()
    },
    lineDetail(record){  
      var iSOsID = record.soIDsID.toString()
       fullRateDestail({iSOsID}).then(res => {
          if (res.success) {
            this.$refs.detail.detailModVis = true
            res.result.map(item =>{item.deliveryDate = moment(item.deliveryDate).format("YYYY-MM-DD")})
            this.$refs.detail.detailData = res.result
             
          }else{
              this.$message.error(res.message);
          }
          
        })
    },
    orderDataChange(){
      this.queryParam.beginDate = moment(this.queryParam.beginDate).format("YYYY-MM-DD")
      this.queryParam.endDate = moment(this.queryParam.endDate).format("YYYY-MM-DD")
    },

    // 选中行
    // onSelectChange(keys, rows) {
    //   this.selectedRowKeys = keys
    //   this.selectedRows = rows
    // },
    // 分页、排序、筛选变化时触发
    handleTableChange(pagination, filters, sorter) {
      // console.log('当前页信息>>>>',pagination)
      this.queryParam.pageNo = pagination.current
      this.getfullrateData()
    },

    aa() {}
  },
  computed: {
    // 选中项
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
        },
        getCheckboxProps: record => ({
          props: {
            disabled: record.title === 'Disabled User',
            // Column configuration not to be checked
            title: record.title
          }
        })
      }
    }
  }
}
</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>
