<template>
  <div class="oYeTransPlan">
    <div class="search">
      <el-form :inline="true">
        <el-form-item>
          <el-date-picker
            v-model="startTime"
            type="datetime"
            placeholder="制单时间"
            style="width: 180px;"
            value-format="timestamp"
          >
          </el-date-picker>
          <span>至</span>
          <el-date-picker
            v-model="endTime"
            type="datetime"
            placeholder="制单时间"
            style="width: 180px;"
            value-format="timestamp"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="screen"
            placeholder="选择条件"
            style="width:180px"
            clearable
          >
            <el-option
              v-for="(item, index) in selectOptions"
              :label="item.label"
              :key="index"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-input v-model="con" style="width:200px" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="screen1"
            placeholder="选择条件"
            style="width:180px"
            clearable
          >
            <el-option
              v-for="(item, index) in selectOptions"
              :label="item.label"
              :key="index"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-input v-model="con1" style="width:200px" clearable></el-input>
        </el-form-item>

        <el-form-item
          ><el-button type="primary" @click="search">查询</el-button>
          <el-button type="primary" @click="insert">新增</el-button>
          <el-button
            type="primary"
            @click="approve"
            v-privilege="activeMenu + 'approve'"
            >审核</el-button
          >
          <el-button type="danger" @click="deleteOrder">删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <!-- 未上报 -->
        <el-tab-pane label="待审核" name="first">
          <dilTable
            v-bind.sync="approvingOptions"
            ref="excelDom"
            @selection-change="approvingOptionsSelectionChange"
            :pageSize="200"
            :max-height="500"
            :showSummaryList="showSummaryList"
            :isshowSummary="true"
            :showIndex="false"
          >
          </dilTable>
        </el-tab-pane>
        <!-- 已审批 -->
        <el-tab-pane label="已审核" name="two">
          <dilTable
            v-bind.sync="approvedOptions"
            ref="table"
            :pageSize="200"
            :max-height="500"
            @selection-change="approvingOptionsSelectionChange"
            :showSummaryList="showSummaryList"
            :isshowSummary="true"
            :showIndex="false"
          >
          </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div class="dialog">
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="80%"
        fullscreen
      >
        <el-divider>计划主体信息</el-divider>
        <el-form :model="planForm" ref="form" :inline="true">
          <el-form-item label="发货单位">
            <el-input v-model="planForm.shipperName" disabled></el-input>
          </el-form-item>
          <el-form-item label="收货单位" prop="receiveId">
            <el-select v-model="planForm.receiveId" style="width:auto">
              <el-option
                v-for="item in consigneeInfo"
                :label="item.label"
                :value="item.value"
                :key="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="片区" prop="saleRemark">
            <el-select
              v-model="planForm.saleRemark"
              style="width:auto"
              :disabled="planForm.receiveId == null ? true : false"
            >
              <el-option
                v-for="item in saleAreaInfo"
                :label="item.label"
                :value="item.value"
                :key="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务员">
            <el-select v-model="planForm.saleMan" style="width:auto">
              <el-option
                v-for="item in saleMans"
                :label="item.label"
                :value="item.value"
                :key="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="checkRealTimeInventory"
              :disabled="planForm.receiveId == null ? true : false"
              >浏览实时库存</el-button
            >
          </el-form-item>
        </el-form>
        <el-divider>客户详细信息</el-divider>
        <el-form :inline="true">
          <el-form-item label="省">
            <el-select
              v-model="planForm.province"
              filterable
              placeholder="请选择"
              @change="onchangeProvince"
            >
              <el-option
                v-for="item in provinceList"
                :key="item.id"
                :label="item.addressProvince"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="市">
            <el-select
              v-model="planForm.city"
              filterable
              placeholder="请选择"
              @change="onchangeCity"
              :disabled="planForm.province == null ? true : false"
            >
              <el-option
                v-for="item in cityList"
                :key="item.id"
                :label="item.addressDistrict"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="县">
            <el-select
              v-model="planForm.county"
              filterable
              placeholder="请选择"
              @change="onchangeCounty"
              :disabled="planForm.city == null ? true : false"
            >
              <el-option
                v-for="item in countyList"
                :key="item.id"
                :label="item.addressTown"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="详细地址">
            <el-autocomplete
              class="inline-input"
              v-model="planForm.place"
              :fetch-suggestions="querySearch1"
              placeholder="请输入具体收货地址"
              :trigger-on-focus="false"
              :disabled="planForm.county == null ? true : false"
            >
              >
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="收货客户电话">
            <el-input
              style="width: 150px"
              v-model="planForm.saleOrderConsigneeTel"
              placeholder="(非必填)"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              style="width: 150px"
              v-model="planForm.truckRemark"
              placeholder="(非必填)"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-divider>物资信息</el-divider>
        <el-table
          :data="materialList"
          border
          style="width: 100%"
          highlight-current-row
          ref="materialTableTop"
          id="table"
        >
          <el-table-column
            v-for="(item, i) in materialTableTop"
            :key="i"
            :prop="item.prop"
            :label="item.label"
            align="center"
            show-overflow-tooltip
          >
            <!-- 插入输入框 -->
            <template slot-scope="scope">
              <!-- 物资件数 -->
              <template v-if="item.prop == 'orderPlanWeight'">
                <el-input
                  class="textinput111"
                  type="number"
                  v-model.number="scope.row.orderPlanWeight"
                  placeholder="(必填)"
                ></el-input>
              </template>
              <!-- 米数 -->
              <template v-else-if="item.prop == 'meterNumber'">
                <el-input
                  v-if="scope.row.haveMeter == 1"
                  class="textinput111"
                  v-model="scope.row.meterNumber"
                  placeholder="(必填)"
                  :disabled="scope.row.isDisable == 1"
                ></el-input>
              </template>
              <!-- 分装车数 -->
              <template v-else-if="item.prop == 'carNumber'">
                <el-input
                  v-if="materialList.length == 1"
                  class="textinput111"
                  v-model.number="scope.row.carNumber"
                  placeholder="(非必填)"
                ></el-input>
              </template>
              <!-- 是否磅重销售 -->
              <template v-else-if="item.prop == 'isPound'">
                <el-select
                  v-model="scope.row.isPound"
                  placeholder="请选择"
                  style="width:65px"
                >
                  <el-option
                    v-for="item in options2"
                    :key="item.values2"
                    :label="item.label2"
                    :value="item.values2"
                  >
                  </el-option>
                </el-select>
              </template>
              <template v-else>
                <span>{{ scope.row[item.prop] }}</span>
              </template>
            </template>
          </el-table-column>
          <!-- 操作列 -->
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="
                  deleteMaterialRow(scope.$index, materialList)
                "
                type="text"
                icon="el-icon-close"
                size="big"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div
          style="display: flex;justify-content: space-around;align-items: center;margin-top: 10px;"
        >
          <el-button type="primary" class="btn" @click="singleDistribute"
            >单拼分配到每车</el-button
          >
          <el-button type="primary" class="btn" @click="createACar"
            >添加到订单</el-button
          >
        </div>
        <el-divider>排车信息</el-divider>
        <el-table
          :data="selectionList"
          border
          style="width: 100%"
          :span-method="cellMerge"
          highlight-current-row
        >
          <el-table-column
            v-for="(item, i) in tableTop"
            :key="i"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            align="center"
            show-overflow-tooltip
          >
            <template slot="scope" v-if="item.label !== '物资名称'">
              <span>{{ item.label }}</span>
            </template>
            <template slot="scope" v-if="item.label !== '件数'">
              <span>{{ item.label }}</span>
            </template>
            <template slot="scope" v-if="item.label !== '收货地址'">
              <span>{{ item.label }}</span>
            </template>
            <!-- <template slot="scope" v-if="item.label !== '收货日期'">
            <span>{{ item.label }}</span>
          </template> -->
            <template slot="scope" v-if="item.label !== '收货姓名'">
              <span>{{ item.label }}</span>
            </template>
            <template slot="scope" v-if="item.label !== '收货方电话'">
              <span>{{ item.label }}</span>
            </template>
            <!-- 插入输入框 -->
            <template slot-scope="scope">
              <template v-if="item.slot">
                <!-- 物资名称 -->
                <template v-if="item.prop == 'materialName'">
                  <el-input
                    style="width: 200px"
                    v-model="scope.row.materialName"
                    disabled
                  >
                  </el-input>
                  <!--更改-->
                </template>
                <!--规格型号-->
                <template v-if="item.prop == 'Specification'">
                  <el-input
                    style="width: 120px"
                    class="textinput"
                    v-model.number="scope.row.Specification"
                  ></el-input>
                </template>
                <!-- 物资件数 -->
                <template v-if="item.prop == 'orderPlanWeight'">
                  <el-input
                    class="textinput"
                    v-model.number="scope.row.orderPlanWeight"
                  ></el-input>
                </template>
                <!-- 米数 -->
                <template v-if="item.prop == 'meterNumber'">
                  <el-input
                    v-if="scope.row.haveMeter == 1"
                    class="textinput"
                    v-model="scope.row.meterNumber"
                    :disabled="scope.row.isDisable == 1"
                  ></el-input>
                </template>
                <!-- 是否磅重销售 -->
                <template v-if="item.prop == 'isPound'">
                  <el-select
                    v-model="scope.row.isPound"
                    placeholder="请选择"
                    style="width:65px"
                  >
                    <el-option
                      v-for="item in options2"
                      :key="item.values2"
                      :label="item.label2"
                      :value="item.values2"
                    >
                    </el-option>
                  </el-select>
                </template>
                <!-- 收货地址id -->
                <template v-if="item.prop == 'saleShipperAddressName'">
                  <el-input
                    style="width: 250px"
                    v-model="scope.row.saleShipperAddressName"
                    disabled
                  >
                  </el-input>
                  <!--增加-->
                </template>
                <!-- 收货客户电话号码 -->
                <template v-if="item.prop == 'saleOrderConsigneeTel'">
                  <el-input
                    class="textinput5"
                    placeholder="(必填)"
                    v-model.number="scope.row.saleOrderConsigneeTel"
                    @change="batchUpdateValue(scope.row, scope.$index)"
                  ></el-input>
                </template>
                <template v-if="item.prop == 'truckRemark'">
                  <el-input
                    class="textinput5"
                    placeholder="(非必填)"
                    v-model.number="scope.row.truckRemark"
                    @change="batchUpdateValue(scope.row, scope.$index)"
                  ></el-input>
                </template>
              </template>
              <template v-else>
                <span>{{ scope.row[item.prop] }}</span>
              </template>
            </template>
          </el-table-column>
          <!-- 操作列 -->
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="deleteRow(scope.$index, selectionList)"
                type="text"
                icon="el-icon-close"
                size="big"
              ></el-button>
              <el-button
                @click.native.prevent="copyRow(scope.$index, scope.row)"
                type="text"
                size="big"
                >复制为一车</el-button
              >
              <el-button
                @click.native.prevent="copyRowflu(scope.$index, scope.row)"
                type="text"
                size="big"
                >复制为分录</el-button
              >
            </template>
          </el-table-column>
          <el-table-column fixed="right" width="80">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="copyRowfluAll(scope.$index, scope.row)"
                type="text"
                size="big"
                >复制整车</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div
          style="display: flex;justify-content: center;align-items: center;margin-top: 10px;"
        >
          <el-button type="primary" @click="makeSureAddOrder"
            >确定新增</el-button
          >
        </div>
      </el-dialog>
      <el-dialog title="实时库存信息" :visible.sync="drawer" width="80%">
        <el-form :inline="true" style="margin-top: 0.5rem;">
          <el-form-item>
            <label class="el-form-item__label" style="width: auto;"
              >物资名称/型号</label
            >
          </el-form-item>
          <el-form-item>
            <el-input
              placeholder="请输入物资名称或型号"
              v-model="materialNameText"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <label class="el-form-item__label" style="width: auto;">规格</label>
          </el-form-item>
          <el-form-item>
            <el-input
              placeholder="请输入内容"
              v-model="materialSpecificationText"
              clearable
              ><template slot="prepend">Φ</template></el-input
            >
          </el-form-item>
          <el-button
            type="primary"
            class="btn"
            @click="onclickRealTimeInventory"
            style="margin-left: 4px;"
            ><i class="el-icon-search"></i>查询</el-button
          >
          <el-button type="primary" @click="makeSureMaterial"
            ><i class="el-icon-check"></i>确定</el-button
          >
        </el-form>
        <dilTable
          v-bind.sync="realTimeInventoryOptions"
          @selection-change="selectionChange"
          :showIndex="false"
        ></dilTable>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getCookie, formatDate } from '@/utils/util.js'
export default {
  data() {
    const generateStartDate = _ => {
      let startDate = new Date()
      let startDateStr = formatDate(startDate, 'yyyy-MM-dd')
      let startTimeStr = startDateStr + ' 00:00:00'
      let startTime = new Date(startTimeStr)
      return startTime.getTime()
    }
    const generateEndDate = _ => {
      let endDate = new Date()
      let endDateStr = formatDate(endDate, 'yyyy-MM-dd')
      let endTimeStr = endDateStr + ' 23:59:59'
      let endTime = new Date(endTimeStr)
      return endTime.getTime()
    }
    return {
      activeMenu: window.top.localStorage.getItem('activeMenu'),
      drawer: false,
      dialogVisible: false,
      activeName: 'first',
      selectOptions: [
        {
          value: 'consigneeCompanyName',
          label: '客户'
        },
        {
          value: 'saleRemark',
          label: '片区'
        },
        {
          value: 'inboundWarehouse',
          label: '装货仓库'
        },
        {
          value: 'materialName',
          label: '物资名称'
        },
        {
          value: 'specificationModel',
          label: '规格'
        }
      ],
      con: '',
      con1: '',
      screen: '',
      screen1: '',
      startTime: generateStartDate(),
      endTime: generateEndDate(),
      approvingOptions: {
        requestUrl: '/api/v1/ams/getTransPlan?apiId=533',
        requestQuery: {
          orderStatus: 0,
          deleted: 0
        },
        selectionType: 'select'
      },
      approvedOptions: {
        requestUrl: '/api/v1/ams/getTransPlan?apiId=533',
        requestQuery: {
          orderStatus: 4,
          deleted: 0
        },
        selectionType: 'select'
      },
      realTimeInventoryOptions: {
        requestUrl: '',
        requestQuery: {},
        selectionType: 'select'
      },
      consigneeInfo: [],
      saleAreaInfo: [],
      saleMans: [],
      planForm: {
        shipperName: '四川省达州钢铁集团有限责任公司',
        shipperId: 1,
        filterFlag: 1
      },
      title: '新增转运计划',
      materialList1: [],
      materialNameText: '',
      materialSpecificationText: '',
      materialList: [],
      materialTableTop: [
        {
          prop: 'inboundWarehouse',
          label: '装货仓库'
        },

        {
          prop: 'materialName',
          label: '物资名称'
        },
        {
          prop: 'meterNumber',
          label: '米数'
        },
        {
          prop: 'Specification',
          label: '规格型号'
        },
        {
          prop: 'orderPlanWeight',
          label: '件数',
          slot: true
        },
        {
          prop: 'singleWeight',
          label: '单重'
        },
        {
          prop: 'carNumber',
          label: '分装车数',
          slot: true
        },
        {
          prop: 'isPound',
          label: '是否磅重销售',
          slot: true
        }
      ],
      tableTop: [
        {
          prop: 'cxh',
          label: '车序号',
          width: '50px'
        },
        {
          prop: 'singleWeight',
          label: '单重'
        },
        {
          prop: 'inboundWarehouse',
          label: '装货仓库'
        },
        {
          prop: 'materialName',
          label: '物资名称',
          width: '300px',
          slot: true
        },
        {
          prop: 'meterNumber',
          label: '米数',
          width: '60px',
          slot: true
        },
        {
          prop: 'Specification',
          label: '规格型号',
          width: '140px',
          slot: true
        },
        {
          prop: 'orderPlanWeight',
          label: '件数',
          width: '60px',
          slot: true
        },
        {
          prop: 'isPound',
          label: '是否磅重销售',
          width: '80px',
          slot: true
        },
        {
          prop: 'saleShipperAddressName',
          label: '收货地址',
          width: '400px',
          slot: true
        },
        {
          prop: 'saleOrderConsigneeTel',
          label: '收货客户电话',
          width: '120px',
          slot: true
        },
        {
          prop: 'truckRemark',
          label: '备注',
          slot: true
        }
      ],
      //是否磅重销售
      options2: [
        {
          values2: 0,
          label2: '是'
        },
        {
          values2: 1,
          label2: '否'
        }
      ],
      selectionList: [],
      spanArr: [],
      //最大车序号数
      maxCxh: 1,
      //所有省
      provinceList: [],
      //所有市
      cityList: [],
      //所有的县(区)
      countyList: [],
      approvingList: [],
      loginName: '',
      orgCode: '',
      showSummaryList: ['materialNumber', 'materialWeight']
    }
  },
  created() {
    this.getInfo()
    this.getOyeConsigneeId()
    this.getOyeSaleArea()
    this.getAllsaleMan()
    this.getAllProvince()
  },
  watch: {
    saleRemark(val) {
      console.log(val, 'val')
      this.getOyeConsigneeId({ saleArea: val })
      this.updateAddress(val)
    },
    receiveCompanyId(val) {
      console.log(val, 'vval')
      this.planForm.consigneeId = val
      this.getOyeSaleArea({ consigneeId: val })
    },
    province(val) {
      this.onchangeProvince(val)
    },
    city(val) {
      this.onchangeCity(val)
    },
    county(val) {
      this.onchangeCounty(val)
    },
    dialogVisible(val) {
      if (val) {
        this.getOyeConsigneeId()
        this.getOyeSaleArea()
      } else {
        this.planForm.receiveId = ''
        this.planForm.consigneeId = ''
        this.planForm.saleArea = ''
        this.materialList = []
        this.materialList1 = []
        this.selectionList = []
      }
    }
  },
  computed: {
    saleRemark() {
      return this.planForm.saleRemark
    },
    receiveCompanyId() {
      return this.planForm.receiveId
    },
    province() {
      return this.planForm.province
    },
    city() {
      return this.planForm.city
    },
    county() {
      return this.planForm.county
    }
  },
  methods: {
    updateAddress(val) {},
    getInfo() {
      this.loginName = getCookie('loginName')
      this.orgCode = getCookie('orgCode')
    },
    getRequestUrl() {
      this.approvingOptions.requestUrl =
        '/api/v1/ams/getTransPlan?apiId=533&startTime=' +
        this.startTime +
        '&endTime=' +
        this.endTime
      let queryOp = {}
      queryOp[this.screen] = [this.con]
      queryOp[this.screen1] = [this.con1]
      this.approvingOptions.requestQuery = {
        orderStatus: 0,
        deleted: 0,
        ...queryOp
      }
      this.approvedOptions.requestUrl =
        '/api/v1/ams/getTransPlan?apiId=533&startTime=' +
        this.startTime +
        '&endTime=' +
        this.endTime
      this.approvedOptions.requestQuery = {
        orderStatus: 4,
        deleted: 0,
        ...queryOp
      }
    },
    search() {
      this.getRequestUrl()
    },
    handleClick() {},
    //以下是收货地址边输边查搜索
    querySearch1(queryString, cb) {
      if (this.planForm.shipperAddressId != null) {
        this.axios
          .post(
            '/api/v1/ams/getRealAddressByLike?addressId=' +
              this.planForm.shipperAddressId +
              '&index=' +
              queryString
          )
          .then(res => {
            this.isNiTui = 0
            if (res.data.code == '200') {
              var restaurants = res.data.data
              var results = queryString
                ? restaurants.filter(this.createFilter1(queryString))
                : restaurants
              // 调用 callback 返回建议列表的数据
              cb(results)
            }
          })
      }
    },
    selectionChange(selection) {
      console.log(selection, 'selection')
      this.materialList1 = []
      this.materialList1 = [].concat(JSON.parse(JSON.stringify(selection)))
    },
    insert() {
      this.dialogVisible = true
      this.title = '新增转运计划'
    },
    getOyeConsigneeId(data = {}) {
      this.axios.post('/api/v1/uc/getOyeConsigneeId', data).then(res => {
        this.consigneeInfo = res.data.data || []
      })
    },
    getOyeSaleArea(data = {}) {
      this.axios.post('/api/v1/uc/getOyeSaleArea', data).then(res => {
        this.saleAreaInfo = res.data.data || []
      })
    },
    //获取所有的业务员数据
    getAllsaleMan() {
      this.axios.get('/api/v1/uc/getSalerList').then(res => {
        this.saleMans = res.data.data
      })
    },
    //获取所有省数据
    getAllProvince() {
      this.axios.post('/api/v1/uc/getAllProvince').then(res => {
        this.provinceList = res.data.data
      })
    },
    //省改变
    onchangeProvince(val) {
      this.axios
        .post(
          '/api/v1/uc/getDistrictByProvince?addressProvince=' +
            val +
            '&i=' +
            new Date()
        )
        .then(res => {
          this.cityList = res.data.data
        })
    },
    //市改变
    onchangeCity(val) {
      this.axios
        .post(
          '/api/v1/uc/getTownByDistrict?addressDistrict=' +
            val +
            '&i=' +
            new Date()
        )
        .then(res => {
          this.countyList = res.data.data
          this.remarks = this.remarksPro + `,直发${this.city}`
        })
    },
    //县(区)改变
    onchangeCounty() {
      let mapValue = {
        addressProvince: this.planForm.province,
        addressDistrict: this.planForm.city,
        addressTown: this.planForm.county
      }
      this.axios.post('/api/v1/uc/getPlaceByAllAddress', mapValue).then(res => {
        if (res.data.data.length > 0) {
          this.planForm.shipperAddressId = res.data.data[0].addressId
        } else {
          this.$message({
            type: 'error',
            message: '省市县区域不存在,请联系管理员维护'
          })
        }
      })
    },
    checkRealTimeInventory() {
      this.realTimeInventoryOptions.requestUrl =
        '/api/v1/wms/getOyeRealTimeResult?apiId=538&i=' + new Date()
      this.realTimeInventoryOptions.requestQuery = JSON.parse(
        JSON.stringify(this.planForm)
      )

      this.drawer = true
    },
    onclickRealTimeInventory() {
      this.realTimeInventoryOptions.requestQuery = JSON.parse(
        JSON.stringify({
          ...this.planForm,
          ...{
            materialNameText: this.materialNameText,
            materialSpe: this.materialSpecificationText
          }
        })
      )
    },
    //删除物资行数据
    deleteMaterialRow(index, rows) {
      rows.splice(index, 1)
    },
    //确认新增物资
    makeSureMaterial() {
      //记录是理重还是磅重
      let getisPound = 1
      this.materialList1.forEach(e => {
        if (!e.materialSpecification) {
          e.materialSpecification = ''
        }
        if (!e.materialModel) {
          e.materialModel = ''
        }
        //判断物资是否为盘螺或非定尺
        if (
          e.materialName.includes('盘螺') ||
          e.materialName.includes('乱尺') ||
          e.materialName.includes('盘元') ||
          e.materialName.includes('盘圆')
        ) {
          getisPound = 0
        } else {
          getisPound = 1
        }
        //若物资为带有盘螺、盘圆、盘元，默认无米数
        let map = {}
        map = JSON.parse(JSON.stringify(e))
        map.isPound = getisPound
        map.meterNumber = e.meter
        map.Specification = `${e.materialSpe}(${e.materialModel})`
        map.orderPlanWeight = null
        map.carNumber = null
        this.materialList.push(map)
      })
      console.log(this.materialList, 'materialList')
      this.drawer = false
      this.materialList1 = []
      this.table1 = false
      //初始化查询数据
      this.materialNameText = null
      this.materialSpecificationText = null
      this.materialModelText = null
    },
    singleDistribute() {
      console.log(this.materialList)
      let ml = this.materialList.length
      if (ml == 0) {
        this.$message.warning('请至少填入一车物资')
        return
      }
      let singleArr = this.materialList
      this.$confirm(
        `以上${ml}车物资将分配到不同的${ml}车,确定继续分配?`,
        '提醒',
        {
          cancelButtonText: '取消',
          confirmButtonText: '确定',
          center: true
        }
      ).then(() => {
        this.creatArtSingle(singleArr)
      })
      this.isNiTui = 0
    },
    async creatArtSingle(singleArr) {
      let i = 0
      singleArr.forEach(e => {
        if (!/(^[1-9]\d*$)/.test(e.orderPlanWeight)) {
          e.orderPlanWeight = null
          i++
        }
      })
      if (i != 0) {
        this.$message.error('件数必填且请输入正整数')
        return
      }
      this.addMaterialToSaleOrderSingle()
    },
    createACar() {
      let i = 0
      this.materialList.forEach(e => {
        if (!/(^[1-9]\d*$)/.test(e.orderPlanWeight)) {
          e.orderPlanWeight = null
          i++
        }
      })
      if (i != 0) {
        this.$message.error('件数必填且请输入正整数')
        return
      }
      this.materialList.forEach(e => {
        if (e.materialName.includes('定尺')) {
          if (!e.meterNumber) {
            i++
          }
        }
      })
      if (i != 0) {
        this.$message.error('定尺必须填写米数')
        return
      }
      if (
        !this.saleOrderConsigneeTel &&
        this.ignoreSaleOrderConsigneeTel == 0
      ) {
        this.$confirm(
          '收货客户电话未填写会导致无法接收车辆发货短信, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(() => {
            this.ignoreSaleOrderConsigneeTel = 1
            this.addMaterialToSaleOrder()
          })
          .catch(() => {
            this.$message.info('已取消！')
          })
      } else {
        this.addMaterialToSaleOrder()
      }
      this.isNiTui = 0
    },
    addMaterialToSaleOrderSingle() {
      this.materialList.forEach(e => {
        var addmap = {
          cxh: this.maxCxh,
          materialName: e.materialName,
          Specification: e.Specification,
          materialCode: e.materialCode,
          orderPlanWeight: e.orderPlanWeight,
          meterNumber: e.meterNumber,
          isPound: e.isPound,
          saleShipperAddressId: this.planForm.shipperAddressId,
          inboundWarehouse: e.inboundWarehouse,
          inventoryId: e.inventoryId,
          singleWeight: e.singleWeight,
          shipperAddressId: this.planForm.shipperAddressId,
          place: this.planForm.place,
          saleShipperAddressName:
            this.planForm.province +
            this.planForm.city +
            this.planForm.county +
            this.planForm.place,
          saleOrderConsigneeTel: this.planForm.saleOrderConsigneeTel,
          truckRemark: this.planForm.truckRemark,
          materialId: e.materialId
        }
        console.log(addmap)
        this.selectionList.push(addmap)
        //最大车序号加1
        this.maxCxh += 1
      })
      this.materialList = []
      this.getSpanArr(this.selectionList)
    },
    //记录每一行的合并数
    getSpanArr(data) {
      //每次调用方法初始化
      this.spanArr = []
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1)
          this.pos = 0
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].cxh === data[i - 1].cxh) {
            this.spanArr[this.pos] += 1
            this.spanArr.push(0)
          } else {
            this.spanArr.push(1)
            this.pos = i
          }
        }
      }
    },
    //根据条件合并行
    cellMerge({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
      // if (columnIndex === 6) {
      //   const _row = this.spanArr[rowIndex]
      //   const _col = _row > 0 ? 1 : 0
      //   return {
      //     rowspan: _row,
      //     colspan: _col
      //   }
      // }
      // if (columnIndex === 7) {
      //   const _row = this.spanArr[rowIndex]
      //   const _col = _row > 0 ? 1 : 0
      //   return {
      //     rowspan: _row,
      //     colspan: _col
      //   }
      // }
      // if (columnIndex === 8) {
      //   const _row = this.spanArr[rowIndex]
      //   const _col = _row > 0 ? 1 : 0
      //   return {
      //     rowspan: _row,
      //     colspan: _col
      //   }
      // }
      // if (columnIndex === 10) {
      //   const _row = this.spanArr[rowIndex]
      //   const _col = _row > 0 ? 1 : 0
      //   return {
      //     rowspan: _row,
      //     colspan: _col
      //   }
      // }
    },
    addMaterialToSaleOrder() {
      console.log(this.materialList)
      if (this.materialList.length == 0) {
        this.$message.warning('请至少为这一车添加一个物资！')
      } else if (!this.saleShipperAddressName && this.values == 'no') {
        this.$message.warning('收货地址未填写！')
      } else if (
        this.materialList.length == 1 &&
        this.materialList[0].carNumber != null
      ) {
        var eachOrderPlanWeight =
          this.materialList[0].orderPlanWeight / this.materialList[0].carNumber
        if (isIntegerNumber(eachOrderPlanWeight)) {
          var e = this.materialList[0]
          for (var i = 0; i < this.materialList[0].carNumber; i++) {
            var addmap = {
              cxh: this.maxCxh,
              materialName: e.materialName,
              Specification: e.Specification,
              materialCode: e.materialCode,
              orderPlanWeight: eachOrderPlanWeight,
              meterNumber: e.meterNumber,
              isPound: e.isPound,
              saleShipperAddressId: this.planForm.saleShipperAddressId,
              shipperAddressId: this.planForm.shipperAddressId,
              inboundWarehouse: e.inboundWarehouse,
              singleWeight: e.singleWeight,
              inventoryId: e.inventoryId,

              place: this.planForm.place,
              saleShipperAddressName:
                this.planForm.province +
                this.planForm.city +
                this.planForm.county +
                this.planForm.place,
              saleOrderConsigneeTel: this.planForm.saleOrderConsigneeTel,
              truckRemark: this.planForm.truckRemark,
              materialId: e.materialId
            }
            this.selectionList.push(addmap)
            //最大车序号加1
            this.maxCxh += 1
          }
          //清空materialList
          this.materialList = []
          //调用记录每一行的合并数的方法
          this.getSpanArr(this.selectionList)
        } else {
          this.$message.warning('分配件数不是整数！')
        }
      } else {
        this.materialList.forEach(e => {
          var addmap = {
            cxh: this.maxCxh,
            materialName: e.materialName,
            Specification: e.Specification,
            materialCode: e.materialCode,
            orderPlanWeight: e.orderPlanWeight,
            meterNumber: e.meterNumber,
            isPound: e.isPound,
            saleShipperAddressId: this.planForm.saleShipperAddressId,
            shipperAddressId: this.planForm.shipperAddressId,
            inboundWarehouse: e.inboundWarehouse,
            singleWeight: e.singleWeight,
            inventoryId: e.inventoryId,

            place: this.planForm.place,
            saleShipperAddressName:
              this.planForm.province +
              this.planForm.city +
              this.planForm.county +
              this.planForm.place,
            saleOrderConsigneeTel: this.planForm.saleOrderConsigneeTel,
            truckRemark: this.planForm.truckRemark,
            materialId: e.materialId
          }
          console.log(addmap)
          this.selectionList.push(addmap)
        })
        //清空materialList
        this.materialList = []
        //最大车序号加1
        this.maxCxh += 1
        //调用记录每一行的合并数的方法
        this.getSpanArr(this.selectionList)
      }
    },
    //删除一车数据
    deleteRow(index, rows) {
      //删除车序号相同的行
      if (this.spanArr[index] == 1) {
        //动态调整车序号
        rows.forEach(e => {
          if (rows[index].cxh < e.cxh) {
            e.cxh -= 1
          }
        })
        //最大车序号减1
        this.maxCxh -= 1
      }
      rows.splice(index, 1)
      //调用记录每一行的合并数的方法
      this.getSpanArr(this.selectionList)
    },
    copyRow(index, row) {
      let copyRow = new Object()
      Object.keys(row).forEach(key => {
        copyRow[key] = row[key]
      })
      copyRow.cxh = this.maxCxh
      this.selectionList.push(copyRow)
      this.getSpanArr(this.selectionList)
      this.maxCxh++
    },
    copyRowflu(index, row) {
      console.log(index)
      let copyRowflu = new Object()
      Object.keys(row).forEach(key => {
        copyRowflu[key] = row[key]
      })
      this.selectionList.splice(index + 1, 0, copyRowflu)
      // this.selectionList.push(copyRowflu);
      this.getSpanArr(this.selectionList)
    },
    copyRowfluAll(index, row) {
      let arr = []
      //复制整车,根据车序号去寻找相同的列
      arr = this.selectionList.concat([]).filter((item, index) => {
        return item.cxh == row.cxh
      })
      arr.forEach(e => {
        let copyRow = new Object()
        Object.keys(e).forEach(key => {
          copyRow[key] = e[key]
        })
        copyRow.cxh = this.maxCxh
        this.selectionList.push(copyRow)
      })
      this.getSpanArr(this.selectionList)
      this.maxCxh++
    },
    makeSureAddOrder() {
      console.log(this.planForm, 'pl')
      console.log(this.selectionList, 'se')
      let data = { ...this.planForm }
      data.salerId = data.saleMan
      data.mapList = this.selectionList
      data.isSelfMention = '否'
      this.axios.post('/api/v1/ams/addTransPlan', data).then(res => {
        if (res.data.status == 'succeed') {
          this.$message.success('添加成功！')
          this.getRequestUrl()
          this.dialogVisible = false
        } else {
          this.$message.error(res.data.data)
        }
      })
    },
    approvingOptionsSelectionChange(section) {
      this.approvingList = [].concat(section)
    },
    approve() {
      let arr = this.approvingList.map(item => {
        let map = {}
        map.orderStatus = 4
        map.saleOrderId = item.saleOrderId
        map.deleted = 0
        map.orderPlanWeight = item.materialNumber
        map.inventoryId = item.inventoryId
        return map
      })
      this.$confirm('确认审核选中的转运计划', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定'
      }).then(res => {
        if (res == 'confirm') {
          this.axios
            .post('/api/v1/ams/approveTransPlan', {
              mapList: arr
            })
            .then(res => {
              if (res.data.status == 'succeed') {
                this.$message.success('审核成功！')
                this.getRequestUrl()
              } else {
                this.$message.error(res.data.msg)
              }
            })
        }
      })
    },
    deleteOrder() {
      let arr = this.approvingList.map(item => {
        let map = {}
        map.orderStatus = this.activeName == 'first' ? 0 : 4
        map.saleOrderId = item.saleOrderId
        map.deleted = 1
        return map
      })
      this.$confirm('确认删除选中的转运计划', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定'
      }).then(res => {
        if (res == 'confirm') {
          this.axios
            .post('/api/v1/ams/approveTransPlan', {
              mapList: arr
            })
            .then(res => {
              if (res.data.status == 'succeed') {
                this.$message.success('删除成功！')
                this.getRequestUrl()
              } else {
                this.$message.error(res.data.msg)
              }
            })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.oYeTransPlan {
  .search {
    margin-left: 10px;
    margin-top: 10px;
  }
  .table {
    margin-left: 10px;
    margin-top: 10px;
  }
  /deep/ .el-divider--horizontal {
    margin: 10px 0;
  }
}
</style>
