// 计费详单
<template>
  <div class="steel_inbound">
    <div class="sache">
      <el-input placeholder="请输入内容" v-model="input" clearable> </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
    </div>
    <div class="table">
      <el-tabs v-model="activeName">
        <!-- 未结算 -->
        <el-tab-pane label="未结算" name="first">
        <dilTable v-bind.sync="options1">
            <el-table-column fixed="right" label="操作" width="145">
                <template slot-scope="scope">
                  <el-button v-if="ssoId==null" type="text" size="small" @click="priceValueUpdate(scope.row)">修改详单地址</el-button>
                  <!-- <el-button type="text" size="mini" @click="getclick(scope)">查看</el-button> -->
                  <el-button type="text" size="mini" @click="addDamage(scope.row)">增加途损</el-button>
                </template>
            </el-table-column>
        </dilTable>
        </el-tab-pane>
        <!-- 已结算 -->
        <el-tab-pane label="已结算" name="second">
        <dilTable v-bind.sync="options2">
            <!-- <el-table-column fixed="right" label="操作" width="40">
                <template slot-scope="scope">
                  <el-button type="text" size="mini" @click="getclick(scope)">查看</el-button>
                </template>
            </el-table-column> -->
        </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 修改详单单价弹出框 -->
    <el-dialog title="修改详单地址" :visible.sync="dialogFormVisible" append-to-body>
      <!-- 内层单价选择弹出框 -->
      <el-dialog
      width="40%"
      title="地址筛选"
      :visible.sync="addressDrawer"
      append-to-body>

      <el-input v-model="addresText" placeholder="请输入内容" style="margin-top: 10px; margin-left: 20px; width: 250px"></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="addAddressClick">确定</el-button>
         </el-form>
         </div>

      <div class="tablecls">
        <!-- 查询所有的运输单价 -->
        <dilTable
          v-bind.sync="Address"
          @radio-change="currentRadioChange2"
        ></dilTable>
      </div>
      </el-dialog>
      <!-- 内层单价选择弹出框 -->

      <el-form>
        <el-form-item label="原详单地址" :label-width="formLabelWidth">
          <el-input style="width:250px" v-model="originalAddress" disabled></el-input>
          <label>原单价</label>
          <el-input style="width:100px" v-model="originalPriceValue" disabled></el-input>
          
        </el-form-item>
        <el-form-item label="新详单地址" :label-width="formLabelWidth">
          <el-input style="width:250px" v-model="newAddress" disabled></el-input>
          <label>现单价</label>
          <el-input style="width:100px" v-model="newPriceValue" disabled></el-input>
          <el-button type="primary" placeholder="()" @click="select">浏览</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="makeSure">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 增加途损 -->
      <el-dialog title="增加途损" width="30%" :visible.sync="dialogDamage">
    <el-form>
      <el-form-item label="达钢的干基" :label-width="formLabelWidth">
        <el-input v-model="DaDryBasis"></el-input>
      </el-form-item>
      <el-form-item label="九江收货的干基" :label-width="formLabelWidth">
        <el-input v-model="JiuDryBasis"></el-input>
      </el-form-item>
      <el-form-item label="单价" :label-width="formLabelWidth">
        <el-input v-model="Fee"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogDamage = false">取 消</el-button>
      <el-button type="primary" @click="makeSureAdd">确 定</el-button>
    </div>
    </el-dialog>

  </div>
</template>

<script>
import { getCookie } from "@/utils/util.js";
export default {
  data(){
    return{
      //已选中 省市县(区)
      address:null,
      //具体收货地址
      place:null,
      //收货地址id
      addressId:null,
      //省市县(区)查询值
      addresText:null,
      //达钢的干基
      DaDryBasis:null,
      //九江收货的干基
      JiuDryBasis:null,
      //单价
      Fee:null,
      //途损费
      damageFee:null,
      //加载
      selectLineLoading:false,
      input:"",
      activeName:"first",
      ssoId:null,
      //详单Id
      detailsId:null,
      options1:{
        // first请求数据的地址
        requestUrl: "",
      },
      options2:{
        // second请求数据的地址
        requestUrl: "",
      },
      //是否打开修改详单地址弹出框
      dialogFormVisible:false,
      //是否打开增加途损弹出框
      dialogDamage:false,
      //原详单单价
      originalPriceValue:null,
      //新详单单价
      newPriceValue:null,
      //原详单地址
      originalAddress:null,
      //新详单地址
      newAddress:null,
      //新单价Id
      priceId:null,
      //是否打开运输单价的模态框
      addressDrawer:false,
      //运输单价的表格
      Address: {
        requestUrl: "/api/v1/ams/getAddressDeliveryAddress?apiId=255",
        selectionType: "radio",
      },
      // addressText:null,
      //缓存当前选中的运输单价
      priceMap:{},
      formLabelWidth: '125px'
    }
  },
  created(){
     this.options1GetRequestUrl();
     this.options2GetRequestUrl();
  },
  methods:{
    //已结算数据
    options1GetRequestUrl(){
    if(getCookie("orgCode") == "chengyunshang"){
      this.ssoId=getCookie('userId');
      this.options1.requestUrl = "/api/v1/bms/getUnFinishedTruckDetailsOrderList?apiId=176&orderType=1&carrierSsoId="+ getCookie('userId');
    }else{
      this.options1.requestUrl = "/api/v1/bms/getUnFinishedTruckDetailsOrderList?apiId=176&orderType=1&carrierSsoId=" + this.ssoId;
    }
    },
    //未结算数据
    options2GetRequestUrl(){
    if(getCookie("orgCode") == "chengyunshang"){
      this.options2.requestUrl = "/api/v1/bms/getTruckDetailsOrderList?apiId=176&orderType=2&carrierSsoId="+ getCookie('userId');
    }else{
      this.options2.requestUrl = "/api/v1/bms/getTruckDetailsOrderList?apiId=176&orderType=2&carrierSsoId=" + this.ssoId;
    }
    },
    //收货地址弹出层
    handleSelect1(item) {
        console.log("获取所有下拉值!");
    },
    //查询全国省市县
    selectAddresClick(){
      if(this.addresText){
        this.Address.requestUrl = "/api/v1/ams/getAddressDeliveryAddress?apiId=255&con="+this.addresText;
      }else{
        this.Address.requestUrl = "/api/v1/ams/getAddressDeliveryAddress?apiId=255&i=" + new Date();
      }
    },
    //以下是收货地址边输边查搜索
    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
        );
      };
    },
    //以上是收货地址边输边查搜索

     toInsert() {
      this.$router.push("/insertCheckInventory");
    },
    // getclick(scope){
		//   this.$router.push("/getTransportResult/"+scope.row.orderId)
	  // },
    //增加途损
    addDamage(row){
      this.detailsId = row.detailsId;
      this.dialogDamage = true;
    },
    makeSureAdd(){
      if(!this.DaDryBasis){
        this.$message.error('达钢的干基还未填!')
      }else if(!this.JiuDryBasis){
        this.$message.error('九江收货的干基还未填!')
      }else if(!this.Fee){
        this.$message.error('单价还未填!')
      }else{
        this.dialogDamage = false;
        //计算途损 [（达钢的干基-九江收货的干基）/达钢的干基 - 0.03] * 单价 > 0, 扣除途损
        this.damageFee = ((this.DaDryBasis - this.JiuDryBasis) / this.DaDryBasis - 0.03) * this.Fee;
        if(this.damageFee>0){
          let map = {
        detailsId: this.detailsId,
        lossFees: this.damageFee.toFixed(2)
        }
        //是否确定增加途损费!
          this.$confirm("是否确定增加途损费", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
          })
          .then(() => {
            //初始化增加途损费对话框中的信息
            this.damageFee=null,
            this.axios.post('/api/v1/bms/addLossFeeOfCoke',map).then((res)=>{
                if(res.data.code == '0'){
                this.$message.success('增加途损费成功!')
                //重置未结算详单表单信息
                this.options1.requestUrl = "/api/v1/bms/getUnFinishedTruckDetailsOrderList?apiId=176&orderType=1&i=" + new Date();
              }else{
                this.$message.error('增加途损费失败,请联系管理员!')
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消修改详单单价!",
            });
          });
        }
      }
      
    },
    onclick(){
      console.log("查询待定值")
      // this.options.requestUrl = "/api/v1/bms/getTruckDetailsOrderList?apiId=176&orderType=1&con="+this.input;
    },
    //修改详单单价
    priceValueUpdate(row){
      this.originalAddress = row.realAddress;
      this.originalPriceValue = row.priceValue;
      this.detailsId = row.detailsId;
      this.dialogFormVisible = true
    },
    //运输订单点击浏览的事件
    select(){
      this.addressDrawer = true;
    },
    //运输单价模态窗口的框计算
    selectAddressClick(){
      this.Price.requestUrl = '/api/v1/ams/getAddressDeliveryAddress?apiId=255&con='+this.addressText;
    },
    //运输单价模态窗口的点击事件
    addAddressClick(){
      //获取运输单价和运输单价ID
      let map = {
        place: this.place,
        addressId: this.addressId
      }
      this.axios.post('/api/v1/ams/getPriceMap',map)
      .then((res)=>{
          if(res.data.data.priceId==-1){
            this.newPriceValue = '未录单价!';
            this.priceId = null;
          }else{
            //赋值运输单价和运输单价ID
            this.newPriceValue = res.data.data.priceValue;//this.priceMap.priceValue
            this.priceId = res.data.data.priceId;//this.priceMap.priceId
          }
      });
      //priceMap
      this.newAddress = this.address+this.place;
      this.addressDrawer = false;
    },
    //运输单价模态框单选选中的事件
    currentRadioChange2(val){
      //若具体地址有值，清空值
      if(this.place!=null){
        this.place=null;
      }
      this.address = val.address,
      this.addressId = val.addressId;
      // this.priceMap = {};
      // this.priceMap = val;
    },
    //确认修改详单单价
    makeSure(){
      //若没有单价Id
      if(this.priceId==null){
        this.$message.error('该区域没有录入单价,不可修改!')
      }else{
      this.dialogFormVisible = false;
      let mapValue = {
        detailsId: this.detailsId,
        priceId: this.priceId
      }
      //是否确定修改详单地址!
      this.$confirm("是否确定修改详单地址", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
      .then(() => {
        //初始化修改详单地址对话框的信息
        this.newPriceValue=null,
        this.newAddress=null,
        this.address=null,
        this.addresText=null,
        this.addressId=null,
        this.priceId=null,
        this.axios.post('/api/v1/bms/updateDetailsOrder',mapValue).then((res)=>{
            if(res.data.code == '0'){
            this.$message.success('修改详单单价成功!')
            //重置未结算详单表单信息
            this.options1.requestUrl = "/api/v1/bms/getUnFinishedTruckDetailsOrderList?apiId=176&orderType=1&i=" + new Date();
          }else{
            this.$message.error('修改详单单价失败,请联系管理员!')
          }
        });
      })
      .catch(() => {
        this.$message({
          type: "info",
          message: "取消修改详单单价!",
        });
      });
      }
    }
  }
}
</script>

<style lang="scss" scode>
.steel_inbound{
  .sache{
    padding: 1.25rem 0.375rem;
			.el-input {
			width: 20%;
			margin-right: 1.25rem;
			}
  }
}
</style>