<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">统计报表</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#/report/monthFHJSB">发货结算表</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!--
      要resetFields起作用，必须配置:model和prop
    -->
    <el-form ref="queryForm" :model="queryModel" inline class="demo-form-inline">
      <el-form-item label="客户" prop="customerId">
        <el-select size="mini" v-model="queryModel.customerId" filterable>
          <el-option v-for="customer in customerList" :key="customer.id" :label="customer.company" :value="customer.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="月度" prop="selectDate">
        <el-date-picker type="month" size="mini" :editable="false" v-model="queryModel.selectDate" style="width: 193px;"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" icon="ios-search" @click="searchBtn" :loading="loading">查询</el-button>&nbsp;
        <el-button type="info" size="mini" style="margin-left: 8px" @click="handleReset('queryForm')">重置</el-button>&nbsp;
        <el-button v-if="dataList.length > 0" type="success" size="mini" icon="ios-search" @click="exportXls" :loading="loading">下载</el-button>&nbsp;
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-table :data="dataList" style="min-height:400px;" v-loading="loading" stripe>
      <el-table-column prop="name" label="品种及规格"></el-table-column>
      <el-table-column prop="width" label="幅宽"></el-table-column>
      <el-table-column prop="number" label="编号"></el-table-column>
      <el-table-column prop="processDate" label="投坯日"></el-table-column>
      <el-table-column prop="processVolume" label="投坯数（米）"></el-table-column>
      <el-table-column prop="processNumber" label="投坯卷数"></el-table-column>
      <el-table-column prop="processRemark" label="投坯详情"></el-table-column>
      <el-table-column prop="ratio" label="伸长率"></el-table-column>
      <el-table-column prop="colour" label="色泽"></el-table-column>
      <el-table-column prop="estimateQuantity" label="应交数（米）"></el-table-column>
      <el-table-column prop="shipment" label="发货日"></el-table-column>
      <el-table-column prop="actualRemark" label="实交数详情"></el-table-column>
      <el-table-column prop="actualQuantityY" label="实交数（码）"></el-table-column>
      <el-table-column prop="actualQuantityM" label="实交数（米）"></el-table-column>
      <el-table-column prop="unitPrice" label="加工单价（元/米）"></el-table-column>
      <el-table-column prop="actualAmount" label="加工金额（元）"></el-table-column>
      <el-table-column prop="clothQuantity" label="坯布数（米）"></el-table-column>
      <el-table-column prop="clothPrice" label="坯布价（元/米）"></el-table-column>
      <el-table-column prop="clothAmount" label="坯布金额（元）"></el-table-column>
      <el-table-column prop="rollAmount" label="力资(元)"></el-table-column>
      <el-table-column prop="totalAmount" label="实际应收款(元)"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
    </el-table>
  </div>
</template>
<script>
import Constant from "@/constant";
import reportApi from "@/api/report";
import customerApi from "@/api/base/customer";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style

export default {
  data() {
    return {
      queryModel: {
        "customerId": "",
        "selectDate": "",
      },
      loading: false,
      customerList: [],
      dataName: "",
      dataDate: "",
      dataList: [],
    };
  },
  methods: {
    searchBtn() {
      if(this.queryModel.customerId != '' && this.queryModel.selectDate != '') {
        this.changePage();
      }
      else {
        this.$message({type: "warning", message: "请选择客户与月度"});
      }
    },
    changePage() {
      var self = this;
      self.loading = true;

      var formData = new FormData();
      formData.append("customerId", self.queryModel.customerId);
      formData.append("selectDate", self.queryModel.selectDate);

      reportApi.monthFHJSB(formData).then(function (response) {
        self.loading = false;

        var jsonData = response.data;
        if(jsonData.result){
          self.dataDate = jsonData.data.date;
          self.dataName = jsonData.data.name;
          self.dataList = jsonData.data.list;
        }
      }).catch((error) => {
        this.$message({type: "error", message: error});
      });
    },
    exportXls() {
      var self = this;
      self.loading = true;

      var formData = new FormData();
      formData.append("customerId", self.queryModel.customerId);
      formData.append("selectDate", self.queryModel.selectDate);

      reportApi.monthFHJSBXls(formData).then(function (response) {
        self.loading = false;

        var jsonData = response.data;
        self.$message({
          showClose: true,
          dangerouslyUseHTMLString: true,
          message: jsonData.message + `,<a href="${jsonData.data}" target="_blank">点击下载</a>&nbsp;`,
          duration: 30000,
        });
      }).catch((error) => {
        self.loading = false;
      });
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
  },
  mounted: function () {
    customerApi.findList().then(response => {
      this.customerList = response.data.data;
    });
  },
};
</script>
<style lang="scss" scoped>
.el-breadcrumb {
  margin: 10px;
  line-height: 20px;
}

.el-divider {
  margin: 5px 0;
}

.demo-form-inline {
  margin-left: 10px;
  text-align: left;
}

.button-group {
  margin-left: 10px;
  text-align: left;
}
</style>