<template>
  <div class="updateSteelDetails">
    <!-- 车牌号查询框 -->
    <div class="top">
      <el-input
        v-model="inputCapacity"
        placeholder="请输入车牌号"
        clearable
      ></el-input>
      <el-button type="primary" @click="onclick">查询</el-button>
      <el-button type="primary" @click="back" icon="el-icon-d-arrow-left"
        >返回</el-button
      >
    </div>
    <!-- //物资模态框多选 -->
    <div class="edrwer">
      <!-- 物资选择模态框 -->
      <el-drawer
        title="选择物资信息"
        :visible.sync="table1"
        direction="rtl"
        size="90%"
        :show-close="true"
      >
        <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="onclick"
            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>
        <div class="tablecls">
          <!-- 查询所有的物资 -->
          <dilTable
            v-bind.sync="option"
            @selection-change="selectionChange"
            @rowDbClick="rowDbClick"
          >
          </dilTable>
        </div>
      </el-drawer>
    </div>
    <!-- //物资模态框单选 -->
    <div class="edrwer">
      <!-- 物资选择模态框 -->
      <el-drawer
        title="选择物资信息"
        :visible.sync="currentTable"
        direction="rtl"
        size="90%"
        :show-close="true"
      >
        <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="onclick1"
            style="margin-left: 4px;"
            ><i class="el-icon-search"></i>查询</el-button
          >
          <el-button type="primary" @click="makeSureChange"
            ><i class="el-icon-check"></i>确定</el-button
          >
        </el-form>
        <div class="tablecls">
          <!-- 查询所有的物资 -->
          <dilTable
            v-bind.sync="option1"
            @radio-change="currentRadioChange"
            @rowDbClick="rowDbClick1"
          >
          </dilTable>
        </div>
      </el-drawer>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        style="width: 100%"
        :span-method="objectSpanMethod"
        border
        key="orderTable"
      >
        <el-table-column
          width="50"
          label="序号"
          align="center"
          fixed="left"
          :resizable="false"
        >
          <template slot-scope="scope">{{ scope.row.group + 1 }}</template>
        </el-table-column>
        <el-table-column property="capacityNumber" label="车牌号" width="120">
        </el-table-column>
        <el-table-column property="consigneeCompanyName" label="收货单位">
        </el-table-column>
        <el-table-column property="completeAddress" label="收货地址">
        </el-table-column>
        <el-table-column property="carrierName" label="承运商">
        </el-table-column>
        <el-table-column property="materialName" label="物资名称">
        </el-table-column>
        <el-table-column property="materialSpecification" label="规格">
        </el-table-column>
        <el-table-column property="orderPlanWeight" label="物资件数">
        </el-table-column>
        <el-table-column property="materialModel" label="型号">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="updateMaterial(scope)"
              v-if="deleted == 1"
            >
              物资变更
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="updateCapacity(scope)"
              v-if="deleted == 0"
            >
              车辆变更
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="closeFlu(scope)"
              v-if="deleted == 0 && scope.row.materialStatus == null"
            >
              关闭分录
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="updateAddress(scope)"
              v-if="deleted == 0"
            >
              收货地址变更
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="dialog">
      <el-dialog
        :title="title"
        :visible.sync="dialogTableVisible"
        :center="true"
        width="1100px"
      >
        <div class="errorMaterial">
          <el-button @click="openDrawer" type="primary">新增拼数</el-button>
        </div>
        <div class="">
          <el-table
            :data="materialList"
            border
            :span-method="objectSpanMethod1"
            highlight-current-row
            key="materialTable"
          >
            <el-table-column
              v-for="(item, i) in materialTableTop"
              :key="i"
              :prop="item.prop"
              :label="item.label"
              :width="item.width"
              align="center"
              show-overflow-tooltip
            >
              <!-- 插入输入框 -->
              <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>
                    <el-button
                      type="primary"
                      @click="changeMaterial(scope.$index)"
                      v-if="scope.row.netWeight == null"
                      >更改</el-button
                    >
                  </template>
                  <!-- 物资件数 -->
                  <template v-if="item.prop == 'orderPlanWeight'">
                    <el-input
                      class="textinput111"
                      v-model.number="scope.row.orderPlanWeight"
                      placeholder="(必填)"
                      :disabled="scope.row.netWeight != null"
                    ></el-input>
                  </template>
                  <!-- 米数 -->
                  <template v-if="item.prop == 'meterNumber'">
                    <el-input
                      v-if="scope.row.haveMeter == 1"
                      :disabled="
                        scope.row.isDisable == 1 || scope.row.netWeight != null
                      "
                      class="textinput111"
                      v-model.number="scope.row.meterNumber"
                      placeholder="(必填)"
                    ></el-input>
                  </template>
                  <!-- 是否磅重销售 -->
                  <template v-if="item.prop == 'isPound'">
                    <el-select
                      v-model="scope.row.isPound"
                      placeholder="请选择"
                      style="width:65px"
                      :disabled="scope.row.netWeight != null"
                    >
                      <el-option
                        v-for="item in options2"
                        :key="item.values2"
                        :label="item.label2"
                        :value="item.values2"
                      >
                      </el-option>
                    </el-select>
                  </template>
                </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"
                  v-if="scope.row.netWeight == null"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="buttonx">
          <el-button type="primary" @click="makeSureUpdateMaterial"
            >确定</el-button
          >
          <el-button type="primary" @click="cancelUpdateMaterial"
            >取消</el-button
          >
        </div>
      </el-dialog>
    </div>
    <div class="addressDialog">
      <!-- 收货地址弹出框 -->
      <el-dialog title="收货地址" :visible.sync="addressDialog" width="90%">
        <div style="magin-bottom:20px,color: red;">
          原收货地址:{{ oldAddress }}
        </div>
        <div class="addressItem">
          <el-form :inline="true">
            <el-form-item>
              <label class="el-form-item__label" style="width: auto;">省</label>
              <el-select
                v-model="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 class="el-form-item__label" style="width: auto;">市</label>
              <el-select
                v-model="city"
                filterable
                placeholder="请选择"
                @change="onchangeCity"
              >
                <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 class="el-form-item__label" style="width: auto;"
                >县(区)</label
              >
              <el-select
                v-model="county"
                filterable
                placeholder="请选择"
                @change="onchangeCounty"
              >
                <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 class="el-form-item__label" style="width: auto;"
                >具体收货地址</label
              >
              <el-autocomplete
                class="inline-input"
                v-model="place"
                :fetch-suggestions="querySearch1"
                placeholder="请输入具体收货地址"
                :trigger-on-focus="false"
                @select="handleSelect1"
              >
              </el-autocomplete>
            </el-form-item>
          </el-form>
        </div>

        <div slot="footer" class="button-box">
          <el-button @click="addressDialog = false">取 消</el-button>
          <el-button type="primary" @click="addAddressClick">确定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 修改运力弹窗 -->
    <div class="updateCapcityDialog">
      <el-dialog title="修改车牌号" :visible.sync="capacityDialog" width="90%">
        <div style="magin-bottom:20px,color: red;">
          原车牌号:{{ oldCapacityNum }}
        </div>
        <div class="inputItem">
          <el-input v-model="inputCapacity1" style="width:200px"></el-input>
          <el-button type="primary" @click="handleCapacity"
            ><i class="el-icon-search"></i
          ></el-button>
        </div>
        <div class="capacityItem">
          <dilTable
            v-bind.sync="capacityOptions"
            @radio-change="radioChangeCapcity"
          ></dilTable>
        </div>
        <div slot="footer" class="button-box">
          <el-button @click="capacityDialog = false">取 消</el-button>
          <el-button type="primary" @click="updateCapacitySure">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputCapacity1: null,
      newCapacityId: null,
      newCapacityNum: null,
      oldCapacityNum: null,
      capacityDialog: false,
      addressDialog: false,
      currentTable: false,
      title: "",
      capacityNumber: null,
      oldAddress: null,
      //是否磅重销售
      options2: [
        {
          values2: 0,
          label2: "是"
        },
        {
          values2: 1,
          label2: "否"
        }
      ],
      capacityOptions: {
        requestUrl: "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248",
        // 控制显示单选列
        selectionType: "radio"
      },
      table1: false,
      isDelete: false,
      dialogTableVisible: false,
      inputCapacity: null,
      tableData: [],
      tableData1: [],
      materialList1: [],
      materialList: [],
      //存放每一行记录的合并数
      spanArr: [],
      spanArr1: [],
      status: null,
      //物资表格多选
      option: {
        // 表格请求数据的地址
        requestUrl: "",
        // 控制显示多选列
        selectionType: "select"
      },
      //物资表格单选
      option1: {
        // 表格请求数据的地址
        requestUrl: "",
        // 控制显示单选列
        selectionType: "radio"
      },
      materialSpecificationText: null,
      materialNameText: null,
      spanArr: [],
      materialTableTop: [
        {
          prop: "capacityNumber",
          label: "车牌号",
          width: "100px"
        },
        {
          prop: "materialName",
          label: "物资名称",
          width: "400px",
          slot: true
        },
        {
          prop: "meterNumber",
          label: "米数",
          width: "100px",
          slot: true
        },
        {
          prop: "Specification",
          label: "规格型号",
          width: "140px"
        },
        {
          prop: "orderPlanWeight",
          label: "件数",
          width: "100px",
          slot: true
        },
        {
          prop: "isPound",
          label: "是否磅重销售",
          width: "100px",
          slot: true
        }
      ],
      orderId: null,
      selectedMaterialCode: null,
      selectedMaterialModel: null,
      selectedMaterialSpecification: null,
      selectedMaterialName: null,
      selectedMaterialId: null,
      //所有省
      provinceList: [],
      //选中的省
      province: "",
      //所有市
      cityList: [],
      //选中的市
      city: "",
      //所有的县(区)
      countyList: [],
      //选中的县
      county: "",
      //判断是内层还是外层点击的收货地址浏览
      inOrOut: 0,
      //地址输入框的值
      addresText: "",
      //已选中省市县
      address: null,
      //具体地址
      place: null,
      //暂存具体地址
      place1: null,
      //省市县Id
      addressId: null,
      orderSaleMap: {},
      isNewCar: null,
      deleted: 0,
      lineSequence: null
    };
  },
  watch: {
    dialogTableVisible() {
      this.getOrderList();
    },
    capacityDialog() {
      this.getOrderList();
      this.inputCapacity1 = null;
      this.capacityOptions.requestUrl =
        "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248&i=" + new Date();
    }
  },
  created() {
    this.deleted = this.$route.params.deleted;
  },
  mounted() {
    console.log(this.$route);
    console.log(this.$route.params.deleted);
    this.getOrderList();
    this.getAllProvince();
  },
  methods: {
    closeFlu(scope) {
      this.capacityNumber = scope.row.capacityNumber;
      this.deleteEasEntryId(scope.row);
      console.log(scope.row);
    },
    //车辆变更
    updateCapacity(scope) {
      this.oldCapacityNum = scope.row.capacityNumber;
      this.saleOrderMaterialId = scope.row.saleOrderMaterialId;
      this.lineSequence = scope.row.lineSequence;
      this.orderId = scope.row.orderId;
      this.$confirm("确定变更车辆吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "info"
      })
        .then(() => {
          this.capacityDialog = true;
        })
        .catch(() => {
          this.$message.info("取消变更");
        });
    },
    radioChangeCapcity(row) {
      this.newCapacityNum = row.capacityNumber;
      this.newCapacityId = row.capacityId;
    },
    updateCapacitySure() {
      let map = {
        orderId: this.orderId,
        saleOrderMaterialId: this.saleOrderMaterialId,
        lineSequence: this.lineSequence,
        capacityId: this.newCapacityId
      };
      let title = `确定将-${this.oldCapacityNum}-替换为-${this.newCapacityNum}?`;
      this.$confirm(title, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
        center: true
      }).then(() => {
        this.axios
          .post("/api/v1/ams/updateCapacityNumberInFactory", map)
          .then(res => {
            if (res.data.code == 200) {
              this.$message.success("变更成功");
              this.getOrderList();
              this.capacityDialog = false;
            } else {
              this.$message.success("变更失败，请联系管理员");
              this.capacityDialog = false;
            }
          })
          .catch(() => {
            this.$message.success("变更失败，请联系管理员");
            this.capacityDialog = false;
          });
      });
    },
    handleCapacity() {
      this.capacityOptions.requestUrl =
        "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248&index=" +
        this.inputCapacity1 +
        "&i=" +
        new Date();
    },
    updateAddress(scope) {
      this.capacityNumber = scope.row.capacityNumber;
      this.oldAddress = scope.row.completeAddress;
      this.orderSaleMap = scope.row;
      console.log(this.orderSaleMap);
      let title = `请选择对-${scope.row.capacityNumber}-进行的操作?`;
      let confirmText = `关闭-${scope.row.capacityNumber}-金蝶分录`;
      let cancelText = `不关闭-${scope.row.capacityNumber}-金蝶分录`;
      this.$confirm(title, "提示", {
        confirmButtonText: confirmText,
        cancelButtonText: cancelText,
        distinguishCancelAndClose: true,
        center: true,
        type: "info"
      })
        .then(() => {
          this.deleteEasEntryId(scope.row);
        })
        .catch(action => {
          console.log(action);
          if (action === "cancel") {
            this.noDeleteEasEntryId();
          }
        });
    },
    //删除金蝶分录
    deleteEasEntryId(row) {
      let title = `确定关闭-${this.capacityNumber}-金蝶分录?`;
      this.$confirm(title, {
        confirmButtonText: "是",
        cancelButtonText: "否",
        center: true,
        type: "warning"
      })
        .then(() => {
          let map = row;
          map.isCloseEas = Number(0);
          map.saleOrderId = this.$route.params.saleOrderId;
          this.axios
            .post("/api/v1/ams/updateCarAddress", map)
            .then(res => {
              if (res.data.code == "200") {
                this.$message.success("关闭成功");
                this.getOrderList();
              } else {
                this.$message.error("关闭失败");
                this.getOrderList();
              }
            })
            .catch(() => {
              this.$message.error("关闭失败");
              this.getOrderList();
            });
        })
        .catch(() => {
          this.$message.info("取消删除");
        });
    },
    //不删除金蝶分录，继续选择
    noDeleteEasEntryId() {
      let title = `请选择对-${this.capacityNumber}-进行的操作?`;
      let confirmText = `仅更换-${this.capacityNumber}-的收货地址`;
      this.$confirm(title, {
        confirmButtonText: confirmText,
        cancelButtonText: "更换地址且换车",
        center: true,
        distinguishCancelAndClose: true,
        type: "info"
      })
        .then(() => {
          this.onlyUpdateAddress();
        })
        .catch(action => {
          if (action === "cancel") {
            this.updateAddressAndCar();
          }
        });
    },
    onlyUpdateAddress() {
      let title = `仅更换-${this.capacityNumber}-的收货地址吗`;
      this.$confirm(title, {
        confirmButtonText: "是",
        cancelButtonText: "否",
        center: true,
        type: "warning"
      }).then(() => {
        this.addressDialog = true;
        this.isNewCar = 1;
      });
    },
    //收货地址弹出层
    handleSelect1(item) {
      console.log("获取所有下拉值!");
    },
    //以下是收货地址边输边查搜索
    querySearch1(queryString, cb) {
      this.axios
        .post(
          "/api/v1/ams/getRealAddressByLike?addressId=" +
            this.addressId +
            "&index=" +
            queryString
        )
        .then(res => {
          if (res.data.code == "200") {
            var restaurants = res.data.data;
            var results = queryString
              ? restaurants.filter(this.createFilter1(queryString))
              : restaurants;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        });
    },
    createFilter1(queryString) {
      return restaurants => {
        return (
          restaurants.value.toLowerCase().indexOf(queryString.toLowerCase()) >
          -1
        );
      };
    },
    //以上是收货地址边输边查搜索
    //监听收货地址模态框点击事件
    currentRadioChange1(radioList) {
      this.addressId = radioList.addressId;
      this.address = radioList.address;
    },
    //确定收货地址事件
    addAddressClick() {
      let map = this.orderSaleMap;
      map.isCloseEas = Number(1);
      map.isNewCar = Number(this.isNewCar);
      map.saleOrderId = this.$route.params.saleOrderId;
      map.place = this.place;
      map.shipperAddressId = this.addressId;
      this.axios.post("/api/v1/ams/updateCarAddress", map).then(res => {
        console.log(res.data);
        if (res.data.code == "200") {
          this.$message.success("修改成功");
          this.getOrderList();
        } else {
          this.$message.error("修改失败");
          this.getOrderList();
        }
      });
      this.addressDialog = false;
      //初始化
      this.addressId = null;
      this.province = null;
      this.cityList = [];
      this.city = null;
      this.countyList = [];
      this.county = null;
      //暂存具体地址
      this.place1 = this.place;
      this.place = null;
      this.addresText = null;
    },
    updateAddressAndCar() {
      this.$confirm("确定修改收货地址且换车?将会解除授权！", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning"
      })
        .then(() => {
          this.addressDialog = true;
          this.isNewCar = 0;
        })
        .catch(() => {
          this.$message.info("取消");
        });
    },
    //获取所有省数据
    getAllProvince() {
      this.axios.post("/api/v1/uc/getAllProvince").then(res => {
        this.provinceList = res.data.data;
      });
    },
    //省改变
    onchangeProvince() {
      this.axios
        .post(
          "/api/v1/uc/getDistrictByProvince?addressProvince=" + this.province
        )
        .then(res => {
          this.cityList = res.data.data;
        });
    },
    //市改变
    onchangeCity() {
      this.axios
        .post("/api/v1/uc/getTownByDistrict?addressDistrict=" + this.city)
        .then(res => {
          this.countyList = res.data.data;
        });
    },
    //县(区)改变
    onchangeCounty() {
      let mapValue = {
        addressProvince: this.province,
        addressDistrict: this.city,
        addressTown: this.county
      };
      this.axios.post("/api/v1/uc/getPlaceByAllAddress", mapValue).then(res => {
        this.addressId = res.data.data[0].addressId;
      });
    },
    rowDbClick1(row) {
      this.selectedMaterialId = row.materialId;
      this.selectedMaterialName = row.materialName;
      this.selectedMaterialSpecification = row.materialSpecification;
      this.selectedMaterialModel = row.materialModel;
      this.selectedMaterialCode = row.materialCode;
      this.makeSureChange();
    },
    //更改行物资
    changeMaterial(index) {
      this.option1.requestUrl =
        "/api/v1/uc/getSteelMaterial?apiId=244&i=" + new Date();
      this.currentTable = true;
      this.materialNameText = null;
      this.materialSpecificationText = null;
      this.isOpenDrawer = 1;
      this.indexChangeMaterial = index;
    },
    //单选一行物资
    currentRadioChange(row) {
      this.selectedMaterialId = row.materialId;
      this.selectedMaterialName = row.materialName;
      this.selectedMaterialSpecification = row.materialSpecification;
      this.selectedMaterialModel = row.materialModel;
      this.selectedMaterialCode = row.materialCode;
    },
    //确认物资修改
    makeSureChange() {
      if (!this.selectedMaterialId) {
        this.$message.warning("请先选择一个物资！");
      } else {
        if (!this.selectedMaterialSpecification) {
          this.selectedMaterialSpecification = "";
        }
        if (!this.selectedMaterialModel) {
          this.selectedMaterialModel = "";
        }
        //记录是理重还是磅重
        var getisPound = 1;
        //判断物资是否为盘螺或非定尺
        if (
          this.selectedMaterialName.includes("盘螺") ||
          this.selectedMaterialName.includes("乱尺") ||
          this.selectedMaterialName.includes("盘元") ||
          this.selectedMaterialName.includes("盘圆")
        ) {
          getisPound = 0;
        } else {
          getisPound = 1;
        }
        this.materialList[
          this.indexChangeMaterial
        ].materialId = this.selectedMaterialId;
        this.materialList[
          this.indexChangeMaterial
        ].materialName = this.selectedMaterialName;
        this.materialList[this.indexChangeMaterial].Specification =
          this.selectedMaterialSpecification + this.selectedMaterialModel;
        this.materialList[
          this.indexChangeMaterial
        ].materialCode = this.selectedMaterialModel;
        this.materialList[this.indexChangeMaterial].isPound = getisPound;
        //确定更改的物资有无米数
        if (
          this.selectedMaterialName.includes("盘螺") ||
          this.selectedMaterialName.includes("盘圆") ||
          this.selectedMaterialName.includes("盘元")
        ) {
          this.materialList[this.indexChangeMaterial].haveMeter = 0;
          this.materialList[this.indexChangeMaterial].meterNumber = null;
        } else {
          if (this.selectedMaterialName.includes("米")) {
            this.materialList[this.indexChangeMaterial].haveMeter = 1;
            this.materialList[this.indexChangeMaterial].isDisable = 1;
            this.materialList[this.indexChangeMaterial].meterNumber = Number(
              this.selectedMaterialName.replace(/[^0-9]/gi, "")
            );
          } else {
            this.materialList[this.indexChangeMaterial].haveMeter = 1;
            this.materialList[this.indexChangeMaterial].isDisable = 0;
            this.materialList[this.indexChangeMaterial].meterNumber = null;
          }
        }
        //重新初始化
        this.indexChangeMaterial = null;
        this.selectedMaterialId = null;
        this.selectedMaterialName = null;
        this.selectedMaterialSpecification = null;
        this.selectedMaterialModel = null;
        this.selectedMaterialCode = null;
        //关闭弹出框
        this.currentTable = false;
        this.isOpenDrawer = 0;
        //初始化查询数据
        this.materialNameText = null;
        this.materialSpecificationText = null;
      }
    },
    cancelUpdateMaterial() {
      this.getOrderList();
      this.dialogTableVisible = false;
    },
    makeSureUpdateMaterial() {
      let i = 0;
      if (this.materialList.length == 0) {
        this.$message.error("物资信息不能为空");
        return;
      }
      this.materialList.forEach(e => {
        if (!/(^[1-9]\d*$)/.test(e.meterNumber) && e.meterNumber != null) {
          e.meterNumber = null;
          i++;
        }
      });
      if (i != 0) {
        i = 0;
        this.$message.error("米数请输入正整数");
        return;
      }
      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;
      }
      this.dialogTableVisible = false;
      this.saleOrderId = this.$route.params.saleOrderId;
      let mapVal = {
        orderId: this.orderId,
        saleOrderMaterialId: this.saleOrderMaterialId,
        saleOrderId: this.saleOrderId,
        mapList: this.materialList
      };
      console.log(mapVal);
      this.axios
        .post("/api/v1/ams/updateAllMaterialInSale", mapVal)
        .then(res => {
          console.log(res.data);
          if (res.data.code == "200") {
            this.$message.success("修改成功！");
            this.getOrderList();
          } else {
            this.$message.warning("修改失败");
            this.getOrderList();
          }
        });
    },
    makeSureMaterial() {
      //记录是理重还是磅重
      var 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;
        }
        //若物资为带有盘螺、盘圆、盘元，默认无米数
        if (
          e.materialName.includes("盘螺") ||
          e.materialName.includes("盘圆") ||
          e.materialName.includes("盘元")
        ) {
          var addmap = {
            orderId: this.orderId,
            capacityNumber: this.capacityNumber,
            materialName: e.materialName,
            Specification: e.materialSpecification + e.materialModel,
            materialCode: e.materialCode,
            orderPlanWeight: null,
            meterNumber: null,
            isPound: getisPound,
            //物资Id
            materialId: e.materialId,
            haveMeter: 0
          };
        } else {
          if (e.materialName.includes("米")) {
            var addmap = {
              capacityNumber: this.capacityNumber,
              orderId: this.orderId,
              materialName: e.materialName,
              Specification: e.materialSpecification + e.materialModel,
              materialCode: e.materialCode,
              orderPlanWeight: null,
              haveMeter: 1,
              meterNumber: e.materialName.replace(/[^0-9]/gi, ""),
              isPound: getisPound,
              //物资Id
              materialId: e.materialId,
              isDisable: 1
              //有米数
            };
            console.log(addmap);
          } else {
            var addmap = {
              capacityNumber: this.capacityNumber,
              orderId: this.orderId,
              materialName: e.materialName,
              Specification: e.materialSpecification + e.materialModel,
              materialCode: e.materialCode,
              orderPlanWeight: null,
              meterNumber: null,
              isPound: getisPound,
              haveMeter: 1,
              //物资Id
              materialId: e.materialId
            };
          }
        }
        this.materialList.push(addmap);
        this.materialList.forEach(e => {
          this.$set(e, "capacityNumber", this.capacityNumber);
        });
        this.getSpanArr1(this.materialList);
        console.log(this.materialList);
      });
      this.materialList1 = [];
      this.table1 = false;
    },
    rowDbClick(row) {
      this.materialList1 = [];
      this.materialList1.push(row);
      this.makeSureMaterial();
    },
    openDrawer() {
      this.option.requestUrl =
        "/api/v1/uc/getSteelMaterial?apiId=244&i=" + new Date();
      this.materialModelText = null;
      this.materialSpecificationText = null;
      this.materialNameText = null;
      this.table1 = true;
    },
    //返回选中的物资信息
    selectionChange(selection) {
      this.materialList1 = [];
      this.materialList1 = selection;
    },
    //物资模态框查询
    onclick() {
      this.option.requestUrl =
        "/api/v1/uc/getSteelMaterial?apiId=244&materialNameText=" +
        this.materialNameText +
        "&materialSpecificationText=" +
        this.materialSpecificationText +
        "&i=" +
        new Date();
    },
    onclick1() {
      this.option1.requestUrl =
        "/api/v1/uc/getSteelMaterial?apiId=244&materialNameText=" +
        this.materialNameText +
        "&materialSpecificationText=" +
        this.materialSpecificationText +
        "&i=" +
        new Date();
    },
    //开关是否删除
    showDelete() {
      this.isDelete = true;
    },
    deleteMaterialRow(index, rows) {
      rows.splice(index, 1);
      this.status = 1;
    },
    updateMaterial(scope) {
      this.materialList = [];
      this.materialList = this.tableData.filter((item, index) => {
        return item.orderId == scope.row.orderId;
      });
      this.materialList.forEach(e => {
        this.capacityNumber = e.capacityNumber;
        this.orderId = e.orderId;
        if (
          e.materialName.includes("盘螺") ||
          e.materialName.includes("盘圆") ||
          e.materialName.includes("盘元")
        ) {
          e.haveMeter = 0;
        } else {
          e.haveMeter = 1;
          if (e.materialName.includes("米")) {
            e.isDisable = 1;
          }
        }
      });
      this.getSpanArr1(this.materialList);
      this.title = "处理" + "-" + this.capacityNumber + "-" + "异常物资信息";
      this.dialogTableVisible = true;
    },
    getOrderList() {
      this.axios
        .post(
          "/api/v1/oms/getDriverInfoForSale1?saleOrderId=" +
            this.$route.params.saleOrderId +
            "&i=" +
            new Date()
        )
        .then(res => {
          console.log(res.data.data);
          this.tableData = [];
          this.tableData = res.data.data;
          // this.orderId = this.tableData[0].orderId;
          // this.lineSequence = this.tableData[0].lineSequence;
          // this.saleOrderMaterialId = this.tableData[0].saleOrderMaterialId;
          this.getSpanArr(this.tableData);
        });
    },
    back() {
      this.$router.push("/saleOrderSteel");
    },
    //合并方法
    getSpanArr1(data) {
      //每次调用方法初始化
      this.spanArr1 = [];
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr1.push(1);
          this.pos1 = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].saleOrderMaterialId === data[i - 1].saleOrderMaterialId) {
            this.spanArr1[this.pos1] += 1;
            this.spanArr1.push(0);
          } else {
            this.spanArr1.push(1);
            this.pos1 = i;
          }
        }
      }
    },
    objectSpanMethod1({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.spanArr1[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    },
    //记录每一行的合并数
    getSpanArr(data) {
      //每次调用方法初始化
      this.spanArr = [];
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
          data[i].group = i;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].orderId === data[i - 1].orderId) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
            data[i].group = data[i - 1].group;
          } else {
            this.spanArr.push(1);
            this.pos = i;
            data[i].group = data[i - 1].group + 1;
          }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (
        columnIndex === 0 ||
        columnIndex === 1 ||
        columnIndex === 2 ||
        columnIndex === 3 ||
        columnIndex === 4
      ) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.updateSteelDetails {
  .top {
    margin-top: 20px;
    margin-left: 20px;
    .el-input {
      width: 250px;
    }
  }
  .table {
    margin-top: 20px;
    margin-left: 20px;
  }
  .dialog {
    .buttonx {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .addressDialog {
    .addressDialog {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .button-box {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
