<template>
  <div>
    <div class="customer-head">
      <div class="customer-back">
        <el-button
            type="danger"
            style="border: none;background: none;color: red"
            icon="el-icon-arrow-left"
            @click="backToList"
        >返回列表
        </el-button>
      </div>
      <div>
        <el-button
            class="el-button--small-yh add-customer-btn"
            type="primary"
            :disabled="disabled"
            size="small"
            style="margin-right: 70px;"
            @click="editCustomer"
        >提交
        </el-button>
        <el-button
            class="el-button--small-yh add-customer-btn"
            type="primary"
            :disabled="disabled"
            size="small"
            @click="editCustomer"
        >保存
        </el-button>
      </div>
    </div>
    <containerTitle
        title="起止地信息"
        style="margin-top: 50px"></containerTitle>
    <basic-container>
      <el-table
          ref="singleTable"
          :data="tableData"
          size="mini"
          :header-cell-style="{color:'#000',background:'#fafafa'}"
          style="width: 100%">
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="60">
        </el-table-column>
        <el-table-column
            property="province"
            label="省市区"
            align="center"
            width="280">
          <template slot-scope="scope">
            <el-cascader
                size="small"
                style="width: 100%;"
                v-model="scope.row.region"
                disabled
                :props="treeOption"></el-cascader>
          </template>
        </el-table-column>
        <el-table-column
            property="address"
            align="center"
            show-overflow-tooltip
            label="详细地址">
          <!--          <template slot-scope="scope">-->
          <!--            <el-input v-model="scope.row.address" size="small" placeholder="请输入内容"></el-input>-->
          <!--          </template>-->
        </el-table-column>
      </el-table>
    </basic-container>
    <containerTitle title="箱信息"></containerTitle>
    <basic-container>
      <avue-crud
          :data="entrustList"
          ref="crudContact"
          :option="entrustOptionTwoT"
          @row-save="(row,done,loading)=>{rowSaveT(row,0,done,loading)}"
          @row-update="rowSaveT">
<!--        <template slot-scope="{type,size,row}" slot="menu">-->
<!--          <el-button icon="el-icon-edit" :size="size" :type="type" @click="designate(row)">指派</el-button>-->
<!--        </template>-->
        <template slot-scope="{row,index}" slot="menu">
          <el-button
              type="text"
              size="small"
              @click="rowSaveT(row,index)"
          >{{ row.$cellEdit ? '保 存' : '修 改' }}
          </el-button>
          <el-button
              size="small"
              icon="el-icon-delete"
              type="text"
              @click="designate(row, index)"
              v-if="!row.$cellEdit"
          >指派</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <containerTitle title="货物信息"></containerTitle>
    <basic-container>
      <avue-form :option="goodsOptionForm" v-model="goodsForm">
        <template slot-scope="scope" slot="corpId">
          <crop-select
              v-model="goodsForm.corpId"
              disabled
          ></crop-select>
        </template>
      </avue-form>
    </basic-container>
    <span>
  <!--    <containerTitle title="费用明细"></containerTitle>-->
      <!--    <basic-container>-->
      <!--      <el-tabs v-model="activeIndex" @tab-click="handleSelect">-->
      <!--        <el-tab-pane label="应收" name="1"></el-tab-pane>-->
      <!--        <el-tab-pane label="应付" name="2"></el-tab-pane>-->
      <!--      </el-tabs>-->
      <!--      <avue-crud-->
      <!--          v-if="activeIndex === '1'"-->
      <!--          :data="collectionList"-->
      <!--          :option="collectionOption"-->
      <!--          ref="collection"-->
      <!--          @row-save="(row,done,loading)=>{rowSave(row,0,done,loading)}"-->
      <!--          @row-update="rowSave"-->
      <!--          @row-del="makeDel">-->
      <!--        <template slot-scope="{row}" slot="corpId">-->
      <!--          <crop-select-->
      <!--              v-if="row.$cellEdit"-->
      <!--              v-model="row.corpId"-->
      <!--              @getCorpData="(data)=>{getfleetId(data,row)}"-->
      <!--          ></crop-select>-->
      <!--          <span v-else>{{row.corpName}}</span>-->
      <!--        </template>-->
      <!--        <template slot-scope="{row}" slot="feeId">-->
      <!--          <breakdown-select-->
      <!--              v-if="row.$cellEdit"-->
      <!--              v-model="row.feeId"-->
      <!--              @selectValue="(value) => selectValue(value,row)"-->
      <!--              :configuration="breakConfiguration"-->
      <!--          >-->
      <!--          </breakdown-select>-->
      <!--          <span v-else>{{ row.feeName }}</span>-->
      <!--        </template>-->
      <!--        <template slot-scope="{row,index}" slot="menu">-->
      <!--          <el-button-->
      <!--              type="text"-->
      <!--              size="small"-->
      <!--              :icon="row.$cellEdit?'el-icon-circle-plus-outline':'el-icon-edit'"-->
      <!--              @click="rowCell(row,index)"-->
      <!--          >{{row.$cellEdit?'保存':'编辑'}}</el-button>-->
      <!--          <el-button type="text"-->
      <!--                     icon="el-icon-delete"-->
      <!--                     size="small"-->
      <!--                     @click="$refs.collection.rowDel(row,index)"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--        <template slot="menuLeft">-->
      <!--          <el-button type="primary" icon="el-icon-plus" @click="addRowCollection" size="small">录入明细</el-button>-->
      <!--        </template>-->
      <!--      </avue-crud>-->
      <!--      <avue-crud-->
      <!--          v-if="activeIndex === '2'"-->
      <!--          :data="paymentList"-->
      <!--          :option="paymentOption"-->
      <!--          ref="payment"-->
      <!--          @row-save="(row,done,loading)=>{rowSave(row,0,done,loading)}"-->
      <!--          @row-update="rowSave"-->
      <!--          @row-del="feeDel"-->
      <!--      >-->
      <!--        <template slot-scope="{row}" slot="corpId">-->
      <!--          <crop-select-->
      <!--              v-if="row.$cellEdit"-->
      <!--              v-model="row.corpId"-->
      <!--              @getCorpData="(data)=>{getfleetId(data,row)}"-->
      <!--          ></crop-select>-->
      <!--          <span v-else>{{row.corpName}}</span>-->
      <!--        </template>-->
      <!--        <template slot-scope="{row}" slot="feeId">-->
      <!--          <breakdown-select-->
      <!--              v-if="row.$cellEdit"-->
      <!--              v-model="row.feeId"-->
      <!--              @selectValue="(value) => selectValue(value,row)"-->
      <!--              :configuration="breakConfiguration"-->
      <!--          >-->
      <!--          </breakdown-select>-->
      <!--          <span v-else>{{ row.feeName }}</span>-->
      <!--        </template>-->
      <!--        <template slot-scope="{row,index}" slot="menu">-->
      <!--          <el-button-->
      <!--              type="text"-->
      <!--              size="small"-->
      <!--              :icon="row.$cellEdit?'el-icon-circle-plus-outline':'el-icon-edit'"-->
      <!--              @click="paymentRowCell(row,index)"-->
      <!--          >{{row.$cellEdit?'保存':'编辑'}}</el-button>-->
      <!--          <el-button type="text"-->
      <!--                     icon="el-icon-delete"-->
      <!--                     size="small"-->
      <!--                     @click="$refs.payment.rowDel(row,index)"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--        <template slot="menuLeft">-->
      <!--          <el-button type="primary" icon="el-icon-plus" @click="addRowPayment" size="small">录入明细</el-button>-->
      <!--        </template>-->
      <!--      </avue-crud>-->
      <!--    </basic-container>-->
</span>
    <containerTitle title="上传附件"></containerTitle>
    <c-upload
        typeUpload="CD"
        :data="orderFilesList"
        :enumerationValue="76"
    ></c-upload>
  </div>
</template>

<script>
import {getLazyTree} from "@/api/base/region";
import {saveDelegationList, detailDelegationList, removeCollection} from "@/api/landTransportation";

export default {
  props: {
    id: {
      type: String
    }
  },
  data() {
    return {
      activeIndex: '1',
      goodsForm: {},
      orderFilesList: [],
      roleName: localStorage.getItem("roleName").split(',')[0],
      breakConfiguration: {
        multipleChoices: false,
        multiple: false,
        disabled: false,
        searchShow: true,
        collapseTags: false,
        clearable: true,
        placeholder: '请点击右边按钮选择',
        dicData: []
      },
      advantageProjectData: [],
      treeOption: {
        label: 'title',
        value: 'id',
        lazy: true,
        lazyLoad(node, resolve) {
          const parentId = node.level === 0 ? '00' : node.data.id;
          getLazyTree(parentId).then(res => {
            resolve(
                res.data.data.map(item => {
                  return {
                    ...item,
                    leaf: !item.hasChildren
                  };
                })
            );
          });
        }
      },
      tableData: [],
      entrustListT: [],
      entrustOptionTwoT: {
        align: 'center',
        menuAlign: 'center',
        index: true,
        cellBtn: false,
        addBtn: false,
        cancelBtn:false,
        editBtn:false,
        addRowBtn: false,
        delBtn: false,
        showSummary: true,
        // editBtn:false,
        summaryText: "合计",
        sumColumnList: [
          {
            name: 'landWeight',
            type: 'sum',
            decimals: 6
          }, {
            name: 'landAmountC',
            type: 'sum',
            decimals: 2
          }, {
            name: 'landAmountD',
            type: 'sum',
            decimals: 2
          }, {
            name: 'ctnQuantity',
            type: 'count'
          }],
        column: [
          {
            label: '状态',
            width: 200,
            type: 'select',
            dicUrl: "/api/blade-client/corpsdesc/fleet-list",
            props: {
              label: "cname",
              value: "id"
            },
            cell: true,
            prop: 'status'
          },{
            label: '车队',
            width: 200,
            type: 'select',
            dicUrl: "/api/blade-client/corpsdesc/fleet-list",
            props: {
              label: "cname",
              value: "id"
            },
            cell: true,
            prop: 'fleetId'
          }, {
            label: '尺寸箱型',
            width: 100,
            slot: true,
            prop: 'ctnType'
          }, {
            label: '箱量',
            width: 50,
            prop: 'ctnQuantity'
          }, {
            label: '重量',
            width: 160,
            prop: 'landWeight',
            controls: false,
            precision: 6,
            type: 'number'
          }, {
            label: '车号',
            width: 150,
            prop: 'plateNo'
          }, {
            label: '司机',
            width: 115,
            prop: 'driverId'
          }, {
            label: '电话',
            width: 160,
            prop: 'tel'
          }, {
            label: '陆运费(D)',
            prop: 'landAmountD'
          }, {
            label: '陆运费(C)',
            cell: true,
            prop: 'landAmountC'
          }, {
            label: '备注',
            cell: true,
            prop: 'remarks'
          }
        ]
      },
      entrustList: [],
      goodsOptionForm: {
        menuBtn: false,
        disabled: true,
        column: [{
          label: '客户名称',
          formslot: true,
          prop: 'corpId'
        }, {
          label: '订单号',
          prop: 'id',
          disabled: true
        }, {
          label: '提单号',
          prop: 'billNo'
        }, {
          label: '场站',
          prop: 'station'
        }, {
          label: '到厂时间',
          type: "datetime",
          format: 'yyyy-MM-dd HH:mm:ss',
          valueFormat: 'yyyy-MM-dd HH:mm:ss',
          prop: 'arrivalTime'
        }, {
          label: '工厂名称',
          prop: 'factory'
        }, {
          label: '货物名称',
          prop: 'goods'
        }, {
          label: '备注',
          prop: 'remarks'
        }
        ]
      },
      entrustOptionTwo: {
        align: 'center',
        menuAlign: 'center',
        addBtnText: '录入箱信息',
        index: true,
        cellBtn: true,
        addBtn: false,
        addRowBtn: true,
        showSummary: true,
        summaryText: "合计",
        sumColumnList: [
          {
            name: 'landWeight',
            type: 'sum',
            decimals: 6
          }, {
            name: 'landAmountD',
            type: 'sum',
            decimals: 2
          }, {
            name: 'ctnQuantity',
            type: 'sum',
            decimals: 1
          }],
        column: [{
          label: '尺寸箱型',
          cell: true,
          prop: 'ctnType',
          type: 'select',
          width: 100,
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=boxType",
          props: {
            label: "dictValue",
            value: "dictKey"
          }
        }, {
          label: '箱量',
          cell: true,
          prop: 'ctnQuantity',
          controls: false,
          precision: 0,
          width: 100,
          type: 'number'
        }, {
          label: '总重量(吨)',
          cell: true,
          prop: 'landWeight',
          controls: false,
          width: 200,
          precision: 6,
          type: 'number'
        }, {
          label: '陆运费(D)',
          cell: true,
          prop: 'landAmountD',
          controls: false,
          width: 200,
          precision: 2,
          type: 'number'
        }, {
          label: '备注',
          cell: true,
          prop: 'remarks'
        }
        ]
      },
      collectionList: [],
      collectionOption: {
        align: 'center',
        menuAlign: 'center',
        index: true,
        cancelBtn: false,
        editBtn: false,
        delBtn: false,
        cellBtn: false,
        addBtn: false,
        addRowBtn: false,
        showSummary: true,
        addBtnText: '录入明细',
        summaryText: "合计",
        sumColumnList: [{
          name: 'price',
          type: 'sum',
          decimals: 2
        }, {
          name: 'amount',
          type: 'sum',
          decimals: 2
        }, {
          name: 'quantity',
          type: 'sum',
          decimals: 1
        }],
        column: [{
          label: '客户名称',
          slot: true,
          prop: 'corpId'
        }, {
          label: '费用名称',
          slot: true,
          prop: 'feeId'
        }, {
          label: '计价单位',
          cell: true,
          prop: 'unit',
          type: "select",
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=unit",
          props: {
            label: "dictValue",
            value: "dictKey"
          }
        }, {
          label: '数量',
          cell: true,
          prop: 'quantity'
        }, {
          label: '单价',
          cell: true,
          prop: 'price'
        }, {
          label: '金额',
          cell: true,
          prop: 'amount'
        }, {
          label: '税率(默认6)',
          cell: true,
          prop: 'taxRate'
        }, {
          label: '币别',
          cell: true,
          prop: 'currency',
          type: "select",
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=currency",
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        }
        ]
      },
      paymentList: [],
      paymentOption: {
        align: 'center',
        menuAlign: 'center',
        index: true,
        cancelBtn: false,
        editBtn: false,
        delBtn: false,
        cellBtn: false,
        addBtn: false,
        addRowBtn: false,
        showSummary: true,
        addBtnText: '录入明细',
        summaryText: "合计",
        sumColumnList: [{
          name: 'price',
          type: 'sum',
          decimals: 2
        }, {
          name: 'amount',
          type: 'sum',
          decimals: 2
        }, {
          name: 'quantity',
          type: 'sum',
          decimals: 1
        }],
        column: [{
          label: '客户名称',
          slot: true,
          prop: 'corpId'
        }, {
          label: '费用名称',
          slot: true,
          prop: 'feeId'
        }, {
          label: '计价单位',
          cell: true,
          prop: 'unit',
          type: "select",
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=unit",
          props: {
            label: "dictValue",
            value: "dictKey"
          }
        }, {
          label: '数量',
          cell: true,
          prop: 'quantity'
        }, {
          label: '单价',
          cell: true,
          prop: 'price'
        }, {
          label: '金额',
          cell: true,
          prop: 'amount'
        }, {
          label: '税率(默认6)',
          cell: true,
          prop: 'taxRate'
        }, {
          label: '币别(默认RMB)',
          cell: true,
          prop: 'currency',
          type: "select",
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=currency",
          props: {
            label: "dictValue",
            value: "dictKey"
          },
        }
        ]
      }
    };
  },
  created() {
    if (this.id) {
      detailDelegationList({id: this.id, kind: '2'}).then(res => {
        this.goodsForm = res.data.data
        this.tableData = res.data.data.orderAddressList
        this.entrustList = res.data.data.orderItemList
        this.goodsList = res.data.data.orderItemList
        if (res.data.data.orderFeeList.length > 0) {
          res.data.data.orderFeeList.forEach(item => {
            if (item.type == 1) {
              this.collectionList.push(item)
            } else {
              this.paymentList.push(item)
            }
          })
        }
        delete this.goodsForm.orderAddressList
        delete this.goodsForm.orderItemList
        delete this.goodsForm.orderFeeList
        this.tableData.forEach(item => {
          if (item.region) item.region = item.region.split(',')
        })
      })
    }
  },
  methods: {
    //切换收付费
    handleSelect(tab, event) {
      this.activeIndex = tab.name
    },
    //箱信息保存
    rowSaveT(row, index, done, loading) {
      this.$refs.crudContact.rowCell(row, index)
      done()
    },
    //箱信息指派
    designate(row){
      this.$confirm('是否确定指派车队', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('11111')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    //箱信息选择车队
    getfleetIdT(data, row) {
      this.$set(row, 'fleetName', data.cname)
    },
    //地址新增
    addAddress() {
      this.tableData.splice(this.tableData.length - 1, 0, {leas: 1111})
    },
    //返回主列表
    backToList() {
      this.$emit('backToList')
    },
    //删除途径地址
    deleteAddress(scope) {
      this.tableData.splice(scope.$index, 1);
    },
    //箱信息保存
    rowSave(row, index, done, loading) {
      row.$cellEdit = false
      done()
    },
    rowCell(row, index) {
      this.$refs.collection.rowCell(row, index)
    },
    paymentRowCell(row, index) {
      this.$refs.payment.rowCell(row, index)
    },
    //箱信息移除
    rowDel(row, index) {
      this.$confirm('此操作将永久删除该单据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.entrustList.splice(index, 1);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //保存
    editCustomer() {
      let data = JSON.parse(JSON.stringify(this.tableData))
      data.forEach(item => {
        if (item.region.length !== 0) item.region = item.region.join(',')
      })
      saveDelegationList({
        ...this.goodsForm,
        kind: '2',
        orderAddressList: data,
        orderItemList: this.entrustList,
        orderFeeList: this.collectionList.concat(this.paymentList)
      }).then(res => {
        this.$message.success('保存成功');
      })
    },
    //收费新增
    addRowCollection() {
      this.$refs.collection.rowCellAdd({
        currency: '1',
        type: '1',
        $cellEdit: true
      });
    },
    //付费新增
    addRowPayment() {
      this.$refs.payment.rowCellAdd({
        currency: '1',
        type: '2',
        $cellEdit: true
      });
    },
    //选择费用
    selectValue(value, row) {
      this.$set(row, 'feeName', value.cname)
    },
    //选择客户信息触发
    getfleetId(data, row) {
      this.$set(row, 'corpName', data.cname)
    },
    //收款信息移除
    makeDel(row, index) {
      this.$confirm('此操作将永久删除该单据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (row.id) removeCollection(row.id)
        this.collectionList.splice(index, 1);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //付款信息移除
    feeDel(row, index) {
      this.$confirm('此操作将永久删除该单据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (row.id) removeCollection(row.id)
        this.paymentList.splice(index, 1);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //自定义表头
    renderHeader(h) {
      return (
          <div>
            <el-tooltip className="item" effect="dark" content="增加途径地址" placement="top">
              <el-button type="primary" icon="el-icon-plus" size="mini" circle onClick={() => this.addAddress()}
                         style="margin-right: 1.25rem;"></el-button>
            </el-tooltip>
          </div>
      )
    }
  }
}
</script>
<style scoped>

</style>
