<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <el-form ref="screenForm" :model="screenForm" label-width="90px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="产品名称" prop="goodsName">
              <el-input v-model="screenForm.goodsName" placeholder="请输入产品名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="产品编码" prop="goodsCode">
              <el-input v-model="screenForm.goodsCode" placeholder="请输入产品编码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="厂产品代码" prop="goodsOldCode">
              <el-input v-model="screenForm.goodsOldCode" placeholder="请输入产品名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="规格型号" prop="model">
              <el-input v-model="screenForm.model" placeholder="请输入规格型号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="制单日期" prop="createDate">
              <el-date-picker
                v-model="screenForm.createDate"
                type="datetimerange"
                range-separator="至"
                style="width: 100%;"
                value-format="yyyy-MM-dd HH:mm:ss"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="发货日期" prop="deliverDate">
              <el-date-picker
                v-model="screenForm.deliverDate"
                type="datetimerange"
                range-separator="至"
                style="width: 100%;"
                value-format="yyyy-MM-dd HH:mm:ss"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="经销商编号" prop="jxsNum">
              <el-input v-model="screenForm.jxsNum" placeholder="请输入经销商编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="经销商名称" prop="jxsName">
              <el-input v-model="screenForm.jxsName" placeholder="请输入经销商名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="发货单号" prop="orderNum">
              <el-input v-model="screenForm.orderNum" placeholder="请输入发货单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="存货类别" prop="type">
              <el-select v-model="screenForm.type" placeholder="选择存货类别" style="width: 100%" clearable>
                <el-option v-for="item in categoryList" :key="item.name" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="业务员" prop="salesMan">
              <el-select v-model="screenForm.salesMan" placeholder="选择业务员" size="small" clearable filterable style="width: 100%">
                <el-option
                  v-for="item in salesmanList"
                  :key="item.adminUserId"
                  :label="item.nickName"
                  :value="item.adminUserId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          
          <el-col :xs="24" :sm="12" :lg="6" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fr">
          <ExportButton :exUrl="'invoice/exportInvoice'" :exParams="exParams" />
        </div>
      </div>
      <div class="table">
        <el-table 
          v-loading="listLoading" 
          :data="dataList" 
          element-loading-text="Loading" 
          border 
          fit 
          highlight-current-row 
          stripe
          show-summary
          :summary-method="$getSummaries">
          <el-table-column align="center" label="厂产品代码" prop="materialOldNumber" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="制单日期" prop="createTime" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="发货日期" prop="orderTime" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="发货单号" prop="id" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="经销商编码" prop="customerNumber" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="经销商名称" prop="customerName" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="销售类型" prop="saleTypeName" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="产品名称" prop="materialName" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="产品编码" prop="materialCode" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="规格型号" prop="specification" min-width="200" show-overflow-tooltip></el-table-column>

          <el-table-column align="right" label="发货金额" prop="payAmount" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.payAmount | numToFixed}}
            </template>
          </el-table-column>
          <el-table-column align="right" label="发货返利金额" prop="payRebateAmount" min-width="110" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.payRebateAmount | numToFixed}}
            </template>
          </el-table-column>
          <el-table-column align="right" label="发货折扣金额" prop="discAmount" min-width="110" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.discAmount | numToFixed}}
            </template>
          </el-table-column>
          <el-table-column align="right" label="折扣额合计" prop="totalDiscAmount" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.totalDiscAmount | numToFixed}}
            </template>
          </el-table-column>
          <el-table-column align="right" label="发货数量" prop="refundableQty" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="业务员" prop="serviceName" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="政策单号" prop="policyCode" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="政策名称" prop="policyTitle" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="区域" prop="adminArea" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="业务区域" prop="serviceArea" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="月份" prop="month" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="项目编码" prop="refEnginRecordNo" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="项目名称" prop="refProjectName" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="备注" prop="remark" min-width="200" show-overflow-tooltip></el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal">
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
import { getSumList } from "@/api/supply/deliver";
import { getCategoryList, getSalesmanList } from '@/api/common'

export default {
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      screenForm: { // 筛选表单数据
        goodsName: '',
        goodsCode: '',
        goodsOldCode: '',
        model: '',
        createDate: '',
        deliverDate: '',
        jxsNum: '',
        jxsName: '',
        orderNum: '',
        type: '',
        salesMan: '',
      },
      categoryList: [],
      salesmanList: [],
    }
  },

  computed: {
    exParams() {
      return {
        materialName: this.screenForm.goodsName,
        materialNumber: this.screenForm.goodsCode,
        materialOldNumber: this.screenForm.goodsOldCode,
        specification: this.screenForm.model,
        createStartTime: this.screenForm.createDate ? this.screenForm.createDate[0] : '',
        createEndTime: this.screenForm.createDate ? this.screenForm.createDate[1] : '',
        startTime: this.screenForm.deliverDate ? this.screenForm.deliverDate[0] : '',
        endTime: this.screenForm.deliverDate ? this.screenForm.deliverDate[1] : '',
        customerNumber: this.screenForm.jxsNum,
        customerName: this.screenForm.jxsName,
        id: this.screenForm.orderNum,
        categoryName: this.screenForm.type,
        serviceId: this.screenForm.salesMan,
      }
    },
  },

  created() {
    this.getSalesmanList();
    this.getCategoryList();
    this.getList();
  },

  methods: {
    // 获取业务员列表
    getSalesmanList() {
      getSalesmanList({
        pageNum: 1,
        pageSize: -1,
        isCustomer: 0,
        status: true,
      }).then(res => {
        this.salesmanList = res.data.records;
      })
    },

    // 获取存货类别列表
    getCategoryList() {
      getCategoryList({
        pageNum: 1,
        pageSize: -1,
      }).then(res => {
        this.categoryList = res.data.records;
      })
    },

    // 查询列表
    getList() {
      this.listLoading = true;

      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        materialName: this.screenForm.goodsName,
        materialNumber: this.screenForm.goodsCode,
        materialOldNumber: this.screenForm.goodsOldCode,
        specification: this.screenForm.model,
        createStartTime: this.screenForm.createDate ? this.screenForm.createDate[0] : '',
        createEndTime: this.screenForm.createDate ? this.screenForm.createDate[1] : '',
        startTime: this.screenForm.deliverDate ? this.screenForm.deliverDate[0] : '',
        endTime: this.screenForm.deliverDate ? this.screenForm.deliverDate[1] : '',
        customerNumber: this.screenForm.jxsNum,
        customerName: this.screenForm.jxsName,
        id: this.screenForm.orderNum,
        categoryName: this.screenForm.type,
        serviceId: this.screenForm.salesMan,
      };
      getSumList(params).then((res) => {
        res.data.records.forEach(item => {
          item.notOutNumber = item.salesStatus ? 0 : item.refundableQty;
          item.sums1 = ['refundableQty'];
          item.sums2 = ['payAmount', 'payRebateAmount', 'discAmount', 'totalDiscAmount'];
        })
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    
  }
}
</script>

<style lang="scss" scoped>

</style>
