<template>
  <!-- 添加汽运单价 111-->
  <div class="addTransportPrice">
    <page-title>新增</page-title>
    <div class="form">
      <div class="form_box">
        <dil-form :formId="287" v-model="form1" ref="from1"></dil-form>
      </div> 
      <div class="liulan">
          <el-button type="primary" class="btn" @click="oilPriceFormulaClick">浏览</el-button>
          <el-button type="primary" class="btn1" @click="addressClick">浏览</el-button>
          <!-- <el-button type="primary" class="btn2">浏览</el-button> -->
      </div>
    </div>
    <div class="elForm">
      <el-form :inline="true" class="demo-form-inline" label-width="80px">
        <el-form-item label="所属承运商">
          <el-autocomplete
            class="inline-input"
            v-model="state"
            :fetch-suggestions="querySearch"
            placeholder="请输入承运商名称"
            :trigger-on-focus="false"
            @select="handleSelect"
          >
            <template slot-scope="{ item }">
              <div class="name" v-if="item.carrierName">
                {{ item.carrierName }}
              </div>
            </template>
          </el-autocomplete>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="makeSure">确定</el-button>
    </div>
    <!-- 油价联动公式模态框 -->
    <el-drawer
      title="请选择油价联动公式"
      :visible.sync="oilPriceFormulaDrawer"
      :direction="direction"
      :before-close="handleClose"
      close-on-press-escape
      modal
      wrapperClosable
      size="70%"
    >
      <div class="selectText">
        <!-- <span></span> -->
        <el-input v-model="LineText" placeholder="请输入内容"></el-input>
        <el-button type="primary" @click="selectLineClick" :loading="selectLineLoading"><i class="el-icon-search"></i>查询</el-button>
        <el-button type="primary" @click="addoilPriceFormula">确定</el-button>
      </div>
      <div class="LineTable">
        <vue-scroll :ops="ops" style="width:100%;height:100%">
          <dilTable 
            v-bind.sync="oilPriceFormula"
            @radio-change="currentRadioChange"
            :isKuang="isKuang"
            :loading="tableLoading"
            :drawer="oilPriceFormulaDrawer"
            @func="func"
          ></dilTable>
        </vue-scroll>
      </div>
    </el-drawer>
    <!-- 地址模态框 -->
    <el-drawer
      title="请选择收货地址"
      :visible.sync="addresDrawer"
      :direction="direction"
      :before-close="handleClose"
      close-on-press-escape
      wrapperClosable
      modal
      size="40%"
    >
      <div class="selectText">
        <!-- <span></span> -->
        <el-input v-model="addresText" placeholder="请输入内容"></el-input>
        <el-button type="primary" @click="selectAddresClick" :loading="selectLineLoading"><i class="el-icon-search"></i>查询</el-button>
         <!-- 已选中 省市(区)县 -->
         <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 class="el-form-item__content" v-model="address" placeholder="请输入内容" :disabled="true"></el-input>
          </el-form-item>
         </el-form>
         <!-- 具体收货地址 -->
         <div v-if="address != null">
           <el-form :inline="true" style="margin-top: 0.5rem;" >

          <el-form-item 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-button type="primary" @click="specificAddress">确定</el-button>
         </el-form>
         </div>
         
      </div>
      <div class="LineTable">
        <vue-scroll :ops="ops" style="width:100%;height:100%">
          <dilTable 
            v-bind.sync="addres"
            @radio-change="currentRadioChange1"
            :isKuang="isKuang"
            :loading="tableLoading"
            :drawer="addresDrawer"
            @func="func"
          ></dilTable>
        </vue-scroll>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { sjTime,isNumber } from "@/utils/sharedJsFile";
export default {
  components: { PageTitle },
  data() {
    return {
      //已选中 省市县(区)
      address:null,
      //具体收货地址
      place:null,
      //承运商名称
      state: null,
      //承运商id
      carrierIds: null,
      //是否打开收货地址模态框
      addresDrawer:false,
      //收货地址查询条件
      addresText:null,
      //收货地址id
      addressId:null,
      //收货地址模态框数据
      addressDetailed :null,
      addres:{
        requestUrl:"",
        selectionType: "radio",
      },

      //是否点击查询
      isKuang:false,
      //表格加载状态
      tableLoading:false,
      //公式内容
      formulaString:null,
      //油价联动公式Id
      oilPriceFormulaId:null,
      //是否打开油价联动公式的模态窗口
      oilPriceFormulaDrawer:false,
      //油价联动公式查询条件
      LineText:null,
      //油价联动公式模态框查询按钮状态
      selectLineLoading:false,
      //油价联动公式模态窗口的数据
      oilPriceFormula:{
        requestUrl:"",
        selectionType: "radio",
      },
      //模态窗口的打开的方向
      direction: 'rtl',
      //表单数据
      form1: {
        priceDate:new Date(),
        oilTypeId:10,
        capacityTypeId:1
      },
      //提交给后端的数据
      // map:{},
      ops: {
        vuescroll: {},
        scrollPanel: {},
        rail: {
          keepShow: true
        },
        bar: {
          hoverStyle: true,
          onlyShowBarOnScroll: false, //是否只有滚动的时候才显示滚动条
          background: "#5f9ea0",//滚动条颜色
          opacity: 0.8,//滚动条透明度
          "overflow-x": "hidden"
        }
      }
    };
  },
  mounted() {
    this.axios
      .post(
        "/api/v1/rms/getCarrierNameBySSOId?carrierSSOId=" + this.carrierUserId
      )
      .then((res) => {
        if (res.data.code == "200") {
          console.log(res.data.data);
          if (res.data.data) {
            this.state = res.data.data.carrierName;
            this.carrierIds = res.data.data.carrierId;
          }
        }
      });
  },
  methods: {
    func(res){
        this.selectLineLoading = false;
        this.isKuang = false;
        this.tableLoading = false;
    },
    selectAddresClick(){
      this.tableLoading = true;
      this.selectLineLoading = true;
      this.isKuang = true;
      if(this.addresText){
        this.addres.requestUrl = "/api/v1/ams/getAddressDeliveryAddress?apiId=255&con="+this.addresText;
      }else{
        this.addres.requestUrl = "/api/v1/ams/getAddressDeliveryAddress?apiId=255&i=" + new Date();
      }
    },
    selectLineClick(){
      this.tableLoading = true;
      this.selectLineLoading = true;
      this.isKuang = true;
      if(this.LineText){
        this.oilPriceFormula.requestUrl = "api/v1/uc/getOilFormula?apiId=444&con="+this.LineText;
      }else{
        this.oilPriceFormula.requestUrl = "api/v1/uc/getOilFormula?apiId=444&i=" + new Date();
      }
    },
    //承运商弹出层
    handleSelect(item) {
      this.carrierIds = item.carrierId;
      item.carrierName = this.state;
    },
    //收货地址弹出层
    handleSelect1(item) {
        console.log("获取所有下拉值!");
    },
    //以下是承运商边输边查搜索
    querySearch(queryString, cb) {
      this.axios
        .post("/api/v1/uc/getCarrierMesByLike?index=" + queryString)
        .then((res) => {
          if (res.data.code == "200") {
            // console.log(res.data.data);
            var restaurants = res.data.data;
            // console.log(restaurants, "restaurants");
            var results = queryString
              ? restaurants.filter(this.createFilter(queryString))
              : restaurants;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        });
    },
    createFilter(queryString) {
      return (restaurants) => {
        return (
          restaurants.value.toLowerCase().indexOf(queryString.toLowerCase()) >
          -1
        );
      };
    },
    //以上是承运商边输边查搜索

    //以下是收货地址边输边查搜索
    querySearch1(queryString, cb){
      this.axios
        .post("/api/v1/ams/getRealAddressByLike?addressId="+this.addressId + "&index=" + queryString)
        .then((res) => {
          if (res.data.code == "200") {
            console.log(res.data.data);
            var restaurants = res.data.data;
            // console.log(restaurants, "restaurants");
            var results = queryString
              ? restaurants.filter(this.createFilter1(queryString))
              : restaurants;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        });
    },
    createFilter1(queryString) {
      return (restaurants) => {
        return (
          restaurants.value.toLowerCase().indexOf(queryString.toLowerCase()) >
          -1
        );
      };
    },
    //以上是收货地址边输边查搜索
    
    //选定联动公式确定
    addoilPriceFormula(){
      let mapvalue = {
        shipperId : this.form1.shipperId,
        oilPriceFormula:this.formulaString,
        cargonameId : this.oilPriceFormulaId,
        capacityTypeId : this.form1.capacityTypeId,
        priceValue : this.form1.priceValue,
        priceDate : this.form1.priceDate,
        addressName:this.form1.addressName,
        type : this.form1.type,
        oilpriceBase : this.form1.oilpriceBase,
        oilTypeId : this.form1.oilTypeId,
        oilpriceChangeThreshold : this.form1.oilpriceChangeThreshold
      }
      this.form1 = mapvalue;
      this.oilPriceFormulaDrawer = false;
    },
    currentRadioChange(val){
      this.formulaString = val.formulaString;
      this.oilPriceFormulaId = val.formulaId;
    },
    currentRadioChange1(val){
      this.address = val.address,
      this.addressId = val.addressId;
    },
    //具体地址确定
    specificAddress(){
      let mapvalue = {
        shipperId : this.form1.shipperId,
        oilPriceFormula:this.formulaString,
        capacityTypeId : this.form1.capacityTypeId,
        cargonameId : this.oilPriceFormulaId,
        priceValue : this.form1.priceValue,
        priceDate : this.form1.priceDate,
        addressName:this.address +''+ this.place,
        type : this.form1.type,
        oilpriceBase : this.form1.oilpriceBase,
        oilTypeId : this.form1.oilTypeId,
        oilpriceChangeThreshold : this.form1.oilpriceChangeThreshold
      }
      this.form1 = mapvalue;
      this.addresDrawer = false;
    },
    //打开线路的模态窗口
    oilPriceFormulaClick(){
      this.oilPriceFormulaDrawer = true;
      this.oilPriceFormula.requestUrl = "/api/v1/uc/getOilFormula?apiId=444"
    },
    addressClick(){
      this.addresDrawer = true;
      this.addres.requestUrl = "/api/v1/ams/getAddressDeliveryAddress?apiId=255"
    },
    //模态窗口关闭时调用
    handleClose(){
      if(this.oilPriceFormulaDrawer){
        this.oilPriceFormulaDrawer = false;
        this.LineText = null;
      }
      if(this.addresDrawer){
        this.addresDrawer = false;
        this.addresText = null;
      }
    },
    makeSure() {
      let mapValue={
        shipperId : this.form1.shipperId,
        carrierId : this.carrierIds,
        cargonameId : this.oilPriceFormulaId,
        capacityTypeId : this.form1.capacityTypeId,
        priceValue : this.form1.priceValue,
        priceDate : sjTime(this.form1.priceDate),
        addressId : this.addressId,
        type : this.form1.type,
        oilpriceBase : this.form1.oilpriceBase,
        oilTypeId : this.form1.oilTypeId,
        oilpriceChangeThreshold : this.form1.oilpriceChangeThreshold,
        place: this.place
      }
      this.axios.post("/api/v1/ams/addAmsContractTransportPrice", mapValue).then((res) => {
          if (res.data.code == "200") {
            this.$router.go(-1);
          }
          this.$message({
            type: "success",
            message: "新增运输单价成功!",
          });
        });   
    },
    // 取消
    cancel() {
      this.$router.push('/transportPrice');
    },
  },
};
</script>
<style lang='scss'>
.elForm {
    margin-left: 30%;
  }
.addTransportPrice{
  .form{
    display: flex;
    .form_box{
      width: 320px;
      margin-left: 30%;
      margin-top: 30px;
      margin-right: 20px;
      .el-form{
        .preview-group{
          .el-form-item{
            .el-form-item__label{
              display: inline-block;
              width: 70px !important;
            }
            .el-form-item__content{
              .el-select{
                width: 250px;
              }
              .el-input{
                width: 250px;
              }
            }
          }
        }
      }
    }
    .liulan{
      width: 120px;
      margin-top: 30px;
      .el-button{
        width: 100px !important;
        margin: auto;
      }
      .btn{
        margin-top: 105px;
      }
      .btn1{
        margin-top: 13px;
      }
      .btn2{
        margin-top: 67px;
      }
    }
  }
  .button_box{
    margin-left: 45%;
  }
  .selectText{
    .el-input{
      width: 250px;
    }
  }
  .LineTable{
    width: 100%;
    height: 480px;
    overflow: hidden;
    margin-top: 10px;
  }
}
</style>
<style lang='scss'>
  /deep/.__bar-is-vertical {
    right: -1px !important;
  }
  /deep/.__bar-is-horizontal {
    display: none !important;
  }
</style>