<template>
  <div class="app-container">
    <el-form
      :model="query"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-row>
        <el-form-item label="提单号" prop="fMblno">
          <el-input
            v-model="query.fMblno"
            placeholder="请输入提单号"
            clearable
            size="small"
            @change="fMblnoChange"
          />
        </el-form-item>
        <el-form-item label="货主" prop="fCorpid">
          <el-select
            v-model="query.fCorpid"
            filterable
            clearable
            remote
            style="width: 80%"
            :remote-method="corpsRemoteMethod"
            placeholder="请输入模糊查找"
          >
            <el-option
              v-for="(dict, index) in fMblnoOptions"
              :key="index.fId"
              :label="dict.fName"
              :value="dict.fId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期" prop="bsdateList">
          <el-date-picker
            v-model="query.bsdateList"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            unlink-panels>
          </el-date-picker>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="getList"
          v-hasPermi="['warehouseBusiness:containerPort:add']"
        >查询
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportOne"
        >导出
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['agreement:agreementTask:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--    <el-col :span="1.5">-->
      <!--      <el-button-->
      <!--        type="info"-->
      <!--        icon="el-icon-download"-->
      <!--        size="mini"-->
      <!--        :disabled="single"-->
      <!--        @click="handleUpdate(null, 2)"-->
      <!--        v-hasPermi="['agreement:agreementStorage:export']"-->
      <!--      >复制新增-->
      <!--      </el-button>-->
      <!--    </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="提单号">
        <el-input v-model="query.fMblno" placeholder="提单号" disabled></el-input>
      </el-form-item>
      <el-form-item label="货主">
        <el-select
          v-model="query.fCorpid"
          filterable
          remote
          style="width: 80%"
          :remote-method="corpsRemoteMethod"
          placeholder="请输入模糊查找"
          disabled
        >
          <el-option
            v-for="(dict, index) in fMblnoOptions"
            :key="index.fId"
            :label="dict.fName"
            :value="dict.fId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="品名">
        <el-input v-model="formInline.goodsName" placeholder="请输入品名" disabled></el-input>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
      ref="table1"
      :height="tableHeight1"
    >
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column type="index" label="行号" align="center"/>
      <el-table-column prop="goodsName" label="品名" width="180" align="center"/>
      <el-table-column prop="fBusinessTypes" label="货物属性" width="180" align="center"/>
      <el-table-column prop="fMarks" label="属性详情" width="180" align="center"/>
      <el-table-column prop="fWName" label="库区" width="180" align="center"/>
      <el-table-column prop="fBusinessType" label="作业类型" width="180" align="center"/>
      <el-table-column prop="fCntval" label="箱型箱量" width="180" align="center"/>
      <el-table-column prop="inBsdate" label="入库日期" width="180" align="center"/>
      <el-table-column prop="inQty" label="入库件数" width="180" align="center"/>
      <el-table-column prop="inGrossweight" label="入库毛重" width="180" align="center"/>
      <el-table-column prop="outBsdate" label="出库日期" width="180" align="center"/>
      <el-table-column prop="outQty" label="出库件数" width="180" align="center"/>
      <el-table-column prop="outGrossweight" label="出库毛重" width="180" align="center"/>
      <el-table-column prop="fQtyblc" label="结余件数" width="180" align="center"/>
      <el-table-column prop="fGrossweightblc" label="结余毛重" width="180" align="center"/>
      <el-table-column prop="fBilltype" label="业务类型" width="180" align="center"/>
      <el-table-column prop="fTruckno" label="车号" width="180" align="center"/>
      <el-table-column prop="fBillingDays" label="堆存天数" width="180" align="center"/>
      <el-table-column prop="fAmt" label="出库堆存费" width="180" align="center"/>
      <el-table-column prop="remark" label="备注" width="180" align="center"/>
    </el-table>
    <h3>
      <i class="el-icon-coin"></i>费用明细
      <el-button
        type="warning"
        icon="el-icon-download"
        size="mini"
        @click="exportTwo"
      >导出
      </el-button>
    </h3>
    <el-table
      :data="costData"
      style="width: 100%"
      ref="table2"
      :height="tableHeight2"
    >
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column type="index" label="行号" align="center"/>
      <el-table-column prop="createTime" label="日期" width="180" align="center"/>
      <el-table-column prop="fBusinessType" label="作业类型" width="180" align="center"/>
      <el-table-column prop="fFeeid" label="费用" width="180" align="center"/>
      <el-table-column prop="fDc" label="收/付" width="180" align="center"/>
      <el-table-column prop="fCName" label="结算单位" width="200" align="center"/>
      <el-table-column prop="fQty" label="数量" align="center"/>
      <el-table-column prop="fUnitprice" label="单价" align="center"/>
      <el-table-column prop="fAmount" label="金额" align="center"/>
      <el-table-column prop="remark" label="备注" align="center"/>
    </el-table>
  </div>
</template>

<script>
import { listpayable , exportOne , exportTwo, getCorps} from '@/api/singleTicket'
import {listCorps} from "@/api/basicdata/corps";

export default {
  name: "index",
  data(){
    return{
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      tableHeight1: '0',
      tableHeight2: '0',
      query:{},
      costData:[],
      formInline:{},
      tableData:[],
      showSearch: true,
      fMblnoOptions:[]
    }
  },
  created() {
    // let date = new Date();
    // let year = parseInt(date.getFullYear())
    // let month = parseInt(date.getMonth() + 1)
    // let currentMonth = date.getMonth()
    // let nextMonth = ++currentMonth
    // let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)  // 下个月的第一天
    // let oneDay = 1000*60*60*24
    // let lastTime = new Date(nextMonthFirstDay - oneDay) // 下个月的第一天减去一天，就是上个月的最后一天
    // let day = lastTime.getDate()
    // if (day < 10) {
    //   day = '0' + day
    // }
    // this.$set(this.query,'bsdateList', [year + '-' + month + '-' + '01 00:00:00', year + '-' + month + '-' + day + ' 23:59:59'])

  },
  mounted() {
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight1 = (window.innerHeight - this.$refs.table1.$el.offsetTop - 160) / 2
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () => {
        this.tableHeight1 = (window.innerHeight - this.$refs.table1.$el.offsetTop - 70) /2
      }
    });
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight2 = (window.innerHeight - this.$refs.table1.$el.offsetTop - 160) / 2
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () => {
        this.tableHeight2 = (window.innerHeight - this.$refs.table1.$el.offsetTop - 70) /2
      }
    })
  },
  methods:{
    getList(){
      listpayable(this.query).then(res=>{
        this.costData = res.data.feeList
        this.tableData = res.data.singleAnalysis
        for (let item in this.tableData){
          if (this.tableData[item].inQty == 0){
            this.tableData[item].inQty = ''
          }
          if (this.tableData[item].inGrossweight == 0){
            this.tableData[item].inGrossweight = ''
          }
          if (this.tableData[item].outGrossweight == 0){
            this.tableData[item].outGrossweight = ''
          }
          if (this.tableData[item].fQtyblc == 0){
            this.tableData[item].fQtyblc = ''
          }
          if (this.tableData[item].fGrossweightblc == 0){
            this.tableData[item].fGrossweightblc = ''
          }
          if (this.tableData[item].outQty == 0){
            this.tableData[item].outQty = ''
          }
        }
        this.formInline.goodsName = res.data.goodsName
      })
    },
    exportOne(){
      let query = this.query
      this.$confirm("是否确认导出", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
          return exportOne(query);
        }).then((response) => {
          this.download(response.msg);
        });
    },
    exportTwo(){
      let query = this.query
      this.$confirm("是否确认导出", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        return exportTwo(query);
      }).then((response) => {
        this.download(response.msg);
      });
    },
    corpsRemoteMethod(name) {
      if (this.query.fMblno) return
      if (name == null || name === "") {
        return false;
      }
      let queryParams = { pageNum: 1, fName: name, type: 1 };
      listCorps(queryParams).then((response) => {
        this.fMblnoOptions = response.rows;
      });
    },
    resetQuery(){},
    // 提单号变化触发
    fMblnoChange() {
      this.$set(this.query, 'fCorpid', '')
      if (this.query.fMblno) {
        getCorps({fMblno: this.query.fMblno}).then(res => {
          this.fMblnoOptions = res.data;
          if (this.fMblnoOptions.length > 0) this.query.fCorpid = this.fMblnoOptions[0].fId
        })
      } else {
        listCorps({type: 1}).then((response) => {
          this.fMblnoOptions = response.rows;
        });
      }
    },
  }
}
</script>

<style scoped>

</style>
