<template>
  <div class="app-container">
    <el-row class="title-style">
      <el-col :span="12">
        <h2 class="bg-left title">客户账单</h2>
      </el-col>
      <el-col :span="12" class="bg-right">
        <el-button class="bg-bottom" type="primary" size="small" @click="returnsales()">返回</el-button>
        <el-button type="primary" size="small" @click="editData">提交</el-button>
      </el-col>
    </el-row>
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="dataForm"
      status-icon
      label-position="left"
      label-width="200px"
      class="position"
    >
      <div class="small-title" style="font-size: 16px">基本信息</div>
      <div class="ws-info-table">
        <el-form-item label="id" prop="id" hidden>
          <el-input v-model="dataForm.id"/>
        </el-form-item>
        <el-form-item label="编号:" prop="payNo" style="margin-right:50px">
          <el-input v-model="dataForm.payNo" :disabled="true" class="input-width"/>
        </el-form-item>
        <el-form-item label="客户:" prop="customer" style="margin-right:50px">
          <el-input v-model="dataForm.customer" :disabled="true" class="input-width"/>
        </el-form-item>
        <el-form-item label="单据类型:" prop="businessType" style="margin-right:50px">
          <el-select v-model="dataForm.businessType" :disabled="true" placeholder="请选择单据类型" clearable class="input-width">
            <el-option
              v-for="item in djList"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="装车日期" prop="closingDate" style="margin-right:50px">
          <el-date-picker
            v-model="dataForm.closingDate"
            type="date"
            value-format="yyyy-MM-dd"
            style="width:200px"
          />
        </el-form-item>
        <el-form-item label="应付金额:" prop="amountIng" style="margin-right:50px">
          <el-input v-model="dataForm.amountIng" :disabled="true" class="input-width" @input="priceChange"/>
        </el-form-item>
        <!-- <el-form-item label="已付金额:" prop="amountPaid" style="margin-right:50px">
          <el-input v-model="dataForm.amountPaid" class="input-width" @input="priceChange"/>
        </el-form-item> -->
        <!-- <el-form-item label="未付金额:" prop="unpaidAmount" style="margin-right:50px">
          <el-input v-model="dataForm.unpaidAmount" :disabled="true" class="input-width"/>
        </el-form-item> -->
        <el-form-item label="备注1:" prop="remarks" style="margin-right:50px">
          <el-input v-model="dataForm.remarks" class="input-width"/>
        </el-form-item>
        <el-form-item label="备注2:" prop="remark" style="margin-right:50px">
          <el-input v-model="dataForm.remark" class="input-width"/>
        </el-form-item>
        <!-- <el-form-item label="状态：" prop="status" style="margin-right:50px">
          <el-select v-model="dataForm.status" clearable placeholder="请选择收款状态" class="input-width">
            <el-option
              v-for="item in skList"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            />
          </el-select>
        </el-form-item> -->
      </div>
      <div class="small-title" style="font-size: 16px">详情</div>
      <!-- <el-button style="margin-bottom:20px" @click="addDetail">添加</el-button> -->

      <el-table
        v-loading="listLoading"
        :data="list"
        :summary-method="getSummaries"
        size="small"
        element-loading-text="正在查询中。。。"
        border
        fit
        highlight-current-row
        show-summary
        @selection-change="handleSelectionChange"
      >
        <el-table-column :selectable="selectInit" type="selection" style="width:30px" />
        <el-table-column type="index" label="序号">
          <template scope="scope">
            <span v-if="scope.$index < 9">0{{ scope.$index + 1 }}</span>
            <span v-else>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="柜号" prop="cabinetNo">
          <template slot-scope="scope">
            <el-input v-model="scope.row.cabinetNo" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="到港日期" prop="arrivalDate" width="170px">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.arrivalDate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width:140px"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="发货公司" prop="sendComp">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.sendComp"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="车队" prop="carTeam">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.carTeam"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="货品" prop="product">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.product"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="目的地" prop="deliveryPlace">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.deliveryPlace"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="清关费" prop="clearanceFee">
          <template slot-scope="scope">
            <el-input v-model="scope.row.clearanceFee" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="清关成本" prop="clearanceCost">
          <template slot-scope="scope">
            <el-input v-model="scope.row.clearanceCost" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="卡车费" prop="truck">
          <template slot-scope="scope">
            <el-input v-model="scope.row.truck" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="卡车成本" prop="truckCost">
          <template slot-scope="scope">
            <el-input v-model="scope.row.truckCost" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="其他费用" prop="otherExpenses">
          <template slot-scope="scope">
            <el-input v-model="scope.row.otherExpenses" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="其他成本" prop="otherCost">
          <template slot-scope="scope">
            <el-input v-model="scope.row.otherCost" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="合计应收" prop="pricePayable">
          <template slot-scope="scope">
            <el-input v-model="scope.row.pricePayable" :disabled="true"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="合计成本" prop="cost">
          <template slot-scope="scope">
            <el-input v-model="scope.row.cost" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注1" prop="remarks">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remarks" />
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="备注2" prop="remark">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" />
          </template>
        </el-table-column>-->
        <!-- <el-table-column align="center" label="状态" prop="status">
          <template slot-scope="scope">
            <el-input v-model="scope.row.status" />
          </template>
        </el-table-column>-->
        <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click.native.prevent="deleteRow(scope.$index, list)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script>
import {
  listCustomerBillingInfo,
  createCustomerBillingInfo,
  editCustomerBillingInfo,
  updateCustomerBillingInfo,
  deleteCustomerBillingInfo
} from '@/api/customerBillingInfo'
import { listCustomerBillingDetail } from '@/api/customerBillingDetail'
import Pagination from '@/components/Pagination'
export default {
  name: 'CustomerBillingInfo',
  components: { Pagination },
  data() {
    return {
      djList: [
        {
          value: '1',
          label: '正常单据'
        },
        {
          value: '2',
          label: '结转单据'
        }
      ],
      skList: [
        {
          value: 1,
          label: '未收款'
        },
        {
          value: 2,
          label: '部分收款'
        },
        {
          value: 3,
          label: '已收款'
        }
      ],
      billDetailList: [
        {
          payNo: '11',
          businessType: '1',
          closingDate: '1',
          amountPaid: '1',
          unpaidAmount: '1',
          remarks: '1',
          remark: '1',
          status: '1'
        }
      ],
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 100
      },
      dataForm: {
        id: undefined,
        amountPaid: '0',
        amountIng: '0'
      },
      dialogFormVisible: false,
      submiting: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {}
    }
  },
  created() {
    this.listQuery.id = this.$route.query.id
    this.getList()
  },
  methods: {
    priceChange() {
      if (this.dataForm.amountIng&&this.dataForm.amountPaid) {
        this.dataForm.unpaidAmount = Number(this.dataForm.amountIng) - Number(this.dataForm.amountPaid)
      }
    },
    getList() {
      this.listLoading = true
      listCustomerBillingDetail(this.listQuery)
        .then(response => {
          this.dataForm = response.data.data
          this.list = response.data.data.customerBillingDetailDOList
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    returnsales() {
      this.$router.push({
        path: 'customerBilling'
      })
    },
    handleSelectionChange(val) {
      this.modification = val
    },
    selectInit(row) {
      return true
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (
          index == 9 ||
          index == 8 ||
          index == 10 ||
          index == 11 ||
          index == 12 ||
          index == 13 ||
          index == 14||
          index == 15
        ) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] += ''
          } else {
            sums[index] = '--'
          }
        } else {
          // sums[index] = "--";
        }
      })

      return sums
    },
    addDetail() {
      console.log('add')
      this.list.push({
        cabinetNo: '',
        arrivalDate: '',
        deliveryPlace: '',
        product: '',
        weight: '',
        clearanceFee: '',
        clearanceCost: '',
        truck: '',
        truckCost: '',
        otherExpenses: '',
        otherCost: '',
        pricePayable: '',
        cost: '',
        remarks: '',
        remark: '',
        status: ''
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    editData() {
      this.listLoading = true
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submiting = true
          this.dataForm.customerBillingDetailDTOS = this.list
          editCustomerBillingInfo(this.dataForm)
            .then(response => {
            //   this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加成功'
              })
              this.submiting = false
              this.returnsales()
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.submiting = false
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submiting = true
          updateCustomerBillingInfo(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.submiting = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.submiting = false
            })
        }
      })
    },
    deleteRow(index, rows) {
      rows.splice(index, 1)
    }
  }
}
</script>
<style lang="scss">
.bg-left {
  padding-left: 30px;
}
.bg-right {
  padding-right: 10px;
  text-align: right;
}
.bg-bottom {
  margin: 15px 0px;
}
.title-style {
  background: #f6f7fc;
  border-radius: 5px;
}
.app-container {
  // background: ;
}
.title::before {
  content: "";
  display: inline-block;
  width: 5px;
  height: 30px;
  background: #5473e8;
  position: absolute;
  left: 0;
}
.position {
  position: relative;
}
.small-title {
  position: relative;
  padding: 10px;
  font-weight: 600;
  margin: 20px 0;
}
.small-title::before {
  position: absolute;
  content: "";
  display: block;
  background: #5473e8;
  width: 4px;
  height: 14px;
  left: 0px;
  top: 13px;
}
.el-form {
  padding: 0 40px;
}
.ws-info-table {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  border-left: 1px solid #ccc;
  border-top: 1px solid #ccc;
  position: relative;
  border-left: 0;
  border-top: 0;
}
.input-width{
    width: 200px;
}
</style>
