<template>
    <div>
      <div v-if="search" class="app-container" style="margin-bottom: 12px;padding-bottom: 5px;border-radius: 10px;">
        <el-form
          ref="querform" 
          :model="queryParams" 
          :inline="true" 
        >
            <el-form-item label="换电站" prop="stationCode">
                <el-select v-model="queryParams.stationCode" placeholder="请选择换电站" style="width: 200px;">
                    <el-option v-for="n in yunList" :label="n.name" :value="n.code" />
                </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handBtnqu">搜索</el-button>
              <!-- <el-button icon="Refresh" @click="resetQuery">重置</el-button> -->
            </el-form-item>
          </el-form>
      </div>
      <div class="app-container" style="margin-bottom: 12px;border-radius: 10px;">

          <div style="display: flex;justify-content: space-between;">
          <el-button 
          type="primary" 
          plain 
          icon="Plus" 
          @click="showpop(1)">新增</el-button>
          <el-tooltip class="item" effect="dark" :content="search? '隐藏搜索' : '显示搜索'" placement="top">
            <el-button size="mini" circle icon="Search" @click="search = !search" />
          </el-tooltip>
        </div>

        <el-table 
          style="width:100%;margin-top: 10px;"
          :data="yysList"
          border
          stripe
          :max-height="search?'600px':'680px'"
        >
            <el-table-column label="费用标准名称" min-width="200" align="center" prop="name" :show-overflow-tooltip="true">
              <template #default="scope">
                <div @click="showpop(2,scope.row)" style="display: inline-block;color:#16599d;border-bottom: 1px solid #417ebb;cursor:pointer">{{scope.row.name}}</div>
              </template>
            </el-table-column>   
            <el-table-column label="开始日期" min-width="100" align="center" prop="dayBegin" :show-overflow-tooltip="true">
              <template #default="scope">	  
                <div>{{scope.row.dayBegin.replace(/-+/g,'').replace(/^(\d{4})*(\d{2})(\d{2})$/, '$2-$3')}}</div>
              </template>	  
            </el-table-column>
            <el-table-column label="结束日期" min-width="100" align="center" prop="dayEnd" :show-overflow-tooltip="true">
              <template #default="scope">	  
                <div>{{scope.row.dayEnd.replace(/-+/g,'').replace(/^(\d{4})*(\d{2})(\d{2})$/, '$2-$3')}}</div>
              </template>
            </el-table-column>
            <el-table-column label="换电站" min-width="250" align="center" prop="stationName" :show-overflow-tooltip="true" />
            <el-table-column label="正常换电服务费(元)" min-width="140" align="center" prop="commonRemainFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.commonRemainFee/100}}</div>
              </template>
            </el-table-column>
            <el-table-column label="换电时正常电量范围" min-width="180" align="center" prop="commonRemainSocRange" :show-overflow-tooltip="true" />
            <el-table-column label="换电时电量剩余过多的soc界定" min-width="220" align="center" prop="moreRemainSoc" :show-overflow-tooltip="true" />
            <el-table-column label="换电时电量剩余过多的服务费(元)" min-width="220" align="center" prop="moreRemainFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.moreRemainFee/100}}</div>
              </template>
            </el-table-column>
            <el-table-column label="换电时电量剩余一般的soc界定" min-width="220" align="center" prop="fewRemainSoc" :show-overflow-tooltip="true" />
            <el-table-column label="换电时电量剩余一般的服务费(元)" min-width="220" align="center" prop="fewRemainFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.fewRemainFee/100}}</div>
              </template>
            </el-table-column>

            <el-table-column label="每公里收费(元)" min-width="120" align="center" prop="eachKmFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.eachKmFee/100}}</div>
              </template>
            </el-table-column>

            <el-table-column label="每SOC收费(元)" min-width="120" align="center" prop="eachSocFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.eachSocFee/100}}</div>
              </template>
            </el-table-column>

            <el-table-column label="每度电收费(元)" min-width="120" align="center" prop="eachKwhFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.eachKwhFee/100}}</div>
              </template>
            </el-table-column>


            <el-table-column label="操作" align="center" width="100" fixed="right">
              <template #default="scope">
                <!-- <el-button link type="primary" icon="Collection" @click="bzmingx(scope.row.pkId)">标准明细</el-button> -->
                <!-- <el-button link type="primary" icon="Edit" @click="showpop(2,scope.row)">查看 / 修改</el-button> -->
                <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- <div v-if="total > 10" style="display: flex;justify-content: right;padding-top:20px;">
            <el-pagination
              v-model:current-page="queryParams.pageNo"
              v-model:page-size="queryParams.pageSize"
              :page-sizes="[10, 20, 30, 40]"
              background
              size="small"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="getyys"
            />
          </div> -->
          
      </div>
  
    
      <!-- 添加或修改用户配置对话框 -->
        <el-dialog 
          v-model="openpop" 
          :title="ystit" 
          width="750px" 
          append-to-body
          :close-on-click-modal="false"
        >
        <div style="padding: 20px;">
          <el-form 
            ref="yysRef" 
            :model="addata" 
            :rules="rules"
            label-position="top" 
            label-width="200px">
              <el-row :gutter="30">
                <el-col :span="12">
                    <el-form-item label="换电站" prop="stationCode">
                        <el-select 
                         v-model="addata.stationCode" 
                         placeholder="请选择换电站"
                        >
                            <el-option 
                             v-for="n in yunList" 
                             :label="n.name" 
                             :value="n.code"
                             @click="getlabel(n.name)" 
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                
                <el-col :span="12">
                  <el-form-item label="费用标准名称" prop="name">
                    <el-input 
                      v-model="addata.name" 
                      placeholder="请输入名称" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="开始日期" prop="dayBegin">
                    <el-date-picker
                        v-model="addata.dayBegin"
                        format="MM-DD"
                        value-format="MMDD"
                        style="width: 100%;" 
                        type="date"
                        placeholder="请选择日期"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="结束日期" prop="dayEnd">
                    <el-date-picker
                        format="MM-DD"
                        v-model="addata.dayEnd"
                        value-format="MMDD"
                        style="width: 100%;" 
                        type="date"
                        placeholder="请选择日期"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="正常换电服务费(元)" prop="commonRemainFee">
                    <el-input 
                      v-model="addata.commonRemainFee" 
                      placeholder="请输入正常换电服务费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="换电时正常电量范围" prop="commonRemainSocRange">
                    <el-input 
                      v-model="addata.commonRemainSocRange" 
                      placeholder="请输入换电时正常电量范围" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="换电时电量剩余过多的soc界定" prop="moreRemainSoc">
                    <el-input 
                      v-model="addata.moreRemainSoc" 
                      placeholder="请输入换电时电量剩余过多的soc界定" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="换电时电量剩余过多的服务费(元)" prop="moreRemainFee">
                    <el-input 
                      v-model="addata.moreRemainFee" 
                      placeholder="请输入换电时电量剩余过多的服务费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="换电时电量剩余一般的soc界定" prop="fewRemainSoc">
                    <el-input 
                      v-model="addata.fewRemainSoc" 
                      placeholder="请输入换电时电量剩余一般的soc界定" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="换电时电量剩余一般的服务费(元)" prop="fewRemainFee">
                    <el-input 
                      v-model="addata.fewRemainFee" 
                      placeholder="请输入换电时电量剩余一般的服务费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="每公里收费(元)" prop="eachKmFee">
                    <el-input 
                      v-model="addata.eachKmFee" 
                      placeholder="请输入每公里收费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="每SOC收费(元)" prop="eachSocFee">
                    <el-input 
                      v-model="addata.eachSocFee" 
                      placeholder="请输入每SOC收费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="每度电收费(元)" prop="eachKwhFee">
                    <el-input 
                      v-model="addata.eachKwhFee" 
                      placeholder="请输入每度电收费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
  
        </div>
        
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="handleAdd">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>

      <!--费用标准明细-->
      <el-drawer 
        v-model="drawer" 
        :with-header="false"
        size="88%"
      >
        <tit @close="handleClose" :name="bzName" />
        <el-row :gutter="25">
          <el-col :span="8">
            <div style="padding: 0;border: 2px solid #73b9c9;border-radius: 10px;">
              <div class="mktit jqbg">费用标准</div>
              <el-form 
                ref="yysRef" 
                :model="addata" 
                :rules="rules"
                label-position="top" 
                label-width="200px">
                  <el-row :gutter="30" style="padding:20px">
                    <el-col :span="24">
                        <el-form-item label="换电站" prop="stationCode">
                            <el-select v-model="addata.stationCode" placeholder="请选择换电站">
                                <el-option 
                                  v-for="n in yunList" 
                                  :label="n.name" 
                                  :value="n.code"
                                  @click="getlabel(n.name)"  
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                    <el-form-item label="费用标准名称" prop="name">
                      <el-input 
                        v-model="addata.name" 
                        placeholder="请输入名称" 
                        maxlength="50" 
                      />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="开始日期" prop="dayBegin">
                        <el-date-picker
                            v-model="addata.dayBegin"
                            format="MM-DD"
                            value-format="MMDD"
                            style="width: 100%;" 
                            type="date"
                            placeholder="请选择日期"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="结束日期" prop="dayEnd">
                        <el-date-picker
                            v-model="addata.dayEnd"
                            format="MM-DD"
                            value-format="MMDD"
                            style="width: 100%;" 
                            type="date"
                            placeholder="请选择日期"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="正常换电服务费(元)" prop="commonRemainFee">
                        <el-input 
                          v-model="addata.commonRemainFee" 
                          placeholder="请输入正常换电服务费" 
                          maxlength="50" 
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="换电时正常电量范围" prop="commonRemainSocRange">
                        <el-input 
                          v-model="addata.commonRemainSocRange" 
                          placeholder="请输入换电时正常电量范围" 
                          maxlength="50" 
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="换电时电量剩余过多的soc界定" prop="moreRemainSoc">
                        <el-input 
                          v-model="addata.moreRemainSoc" 
                          placeholder="请输入换电时电量剩余过多的soc界定" 
                          maxlength="50" 
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="换电时电量剩余过多的服务费(元)" prop="moreRemainFee">
                        <el-input 
                          v-model="addata.moreRemainFee" 
                          placeholder="请输入换电时电量剩余过多的服务费" 
                          maxlength="50" 
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="换电时电量剩余一般的soc界定" prop="fewRemainSoc">
                        <el-input 
                          v-model="addata.fewRemainSoc" 
                          placeholder="请输入换电时电量剩余一般的soc界定" 
                          maxlength="50" 
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="换电时电量剩余一般的服务费(元)" prop="fewRemainFee">
                        <el-input 
                          v-model="addata.fewRemainFee" 
                          placeholder="请输入换电时电量剩余一般的服务费" 
                          maxlength="50" 
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                  <el-form-item label="每公里收费(元)" prop="eachKmFee">
                    <el-input 
                      v-model="addata.eachKmFee" 
                      placeholder="请输入每公里收费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="每SOC收费(元)" prop="eachSocFee">
                    <el-input 
                      v-model="addata.eachSocFee" 
                      placeholder="请输入每SOC收费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="每度电收费(元)" prop="eachKwhFee">
                    <el-input 
                      v-model="addata.eachKwhFee" 
                      placeholder="请输入每度电收费" 
                      maxlength="50" 
                    />
                  </el-form-item>
                </el-col>
                    <el-col :span="24" style="text-align: center;">
                      <el-button type="success" @click="handleAdd">确 定</el-button>
                      <el-button @click="resetupdate">重 置</el-button>
                    </el-col>
                  </el-row>
              </el-form>
            </div>
            
          </el-col>
          <el-col :span="16">
            <div style="height: 100%;margin-bottom: 20px;border: 1px solid #98c3ef; border-radius: 10px;">
              <div class="mktit">费用标准明细</div>
              <costdetails 
                v-if="drawer" 
                style="padding: 20px;"
                :stationCode='stationCode'
                :standardId="standardId" 
                :list="mxlist"
              />
            </div> 
          </el-col>
        </el-row>
      </el-drawer>
      
    </div>
  </template>
  <script setup lang="ts" name="UserPage">
      import { 
        gethdzlist
      } from '@/api/swapstation/hdz'

      import { 
          getjqrlist, 
          addhdz, 
          update,
          delhdz
      } from '@/api/swapstation/swapstation'
      
      import { setNum } from '@/utils/validate'

      import { ref,reactive,watch } from 'vue'
      import { ElMessage, ElMessageBox } from 'element-plus'
      import Tit from '@/views/components/detailstit.vue'
      import Costdetails from './costdetails.vue'

      function timestamp() {
        return new Date().getTime()
      }

      let search = ref(true)

      let queryParams = reactive({
        // pageSize:20,	      //每页条数
        // pageNo:1,	          //页数
        stationCode:''      //换电站编码  
      })
      
      //查询
      let total = ref(0)
      let yysList = ref<any>([])
      function handBtnqu(){
        //queryParams.pageNo = 1
        getyys()
      }
      
      //初始查询换电站
      let yunList = ref<any>([])
      gethdzlist({
        pageSize:200,	      //每页条数
        pageNo:1,	          //页数
        proxyId:'',	        //归属运营商ID
        name:'',	          //站点名称
        code:'',	          //站点编码
        status:'',	        //状态：1-正常营业，2-正常停运，3-故障停运，4-指令停运，9-其它
        type:'',	          //站点类型ID
      }).then(rps => {
          if(rps.data){
              let list = (rps.data as any).records
              yunList.value = list?list:[]
              queryParams.stationCode = list[0].code
              getyys()
          }
      })

      function getyys() {
        yysList.value.splice(0)
        getjqrlist(queryParams).then(rps => {
            if(rps.data){
              yysList.value = rps.data
            }
        })
      }

      // function handleSizeChange(val:number) {
      //   queryParams.pageSize = val
      //   getyys()
      // }
      
      //重置
      let querform = ref()
      function resetQuery() {
        querform.value?.resetFields()
      }

      
      //新增、修改
      let type = ref(1)
      let openpop = ref(false)
      let ystit = ref('添加费用标准')
      let yysRef = ref()
      let standardId = ref('')
      let bzName = ref('')
      let stationCode = ref('')
      let stationName = ref('')
      let addata = reactive<any>({
        pkId:'',
        stationCode:'',	              //换电站编码
        dayBegin:'',	                //开始时间
        dayEnd:'',	                  //结束时间
        name:'',                      //费用标准名称
        stationName:'',               //换电站名称
        commonRemainFee:'',	          //正常换电服务费
        commonRemainSocRange:'',	    //换电时正常电量范围
        moreRemainSoc:'',	            //换电时电量剩余过多的soc界定
        moreRemainFee:'',	            //换电时电量剩余过多的服务费
        fewRemainSoc:'',	            //换电时电量剩余一般的soc界定
        fewRemainFee:'',	            //换电时电量剩余一般的服务费
        eachKmFee:'',	                //每公里收费
        eachSocFee:'',	              //每SOC收费
        eachKwhFee:''	                //每度电收费
      })

      const rules = ref({
        stationCode:[
            { required: true, message: "请选择换电站", trigger: "blur" }
        ],
        name:[
        { required: true, message: "请输入名称", trigger: "blur" }
        ],
        dayBegin:[
          { required: true, message: "请选择开始时间", trigger: "blur" },
        ],
        dayEnd:[
          { required: true, message: "请选择结束时间", trigger: "blur" },
        ]
      })

      watch(() => addata.commonRemainFee, v => {
        addata.commonRemainFee = setNum(v)
      })

      watch(() => addata.moreRemainFee, v => {
        addata.moreRemainFee = setNum(v)
      })
      
      watch(() => addata.fewRemainFee, v => {
        addata.fewRemainFee = setNum(v)
      })

      watch(() => addata.eachKmFee, v => {
        addata.eachKmFee = setNum(v)
      })

      watch(() => addata.eachSocFee, v => {
        addata.eachSocFee = setNum(v)
      })

      watch(() => addata.eachKwhFee, v => {
        addata.eachKwhFee = setNum(v)
      })

      //获取换电站名称
      function getlabel(n:string){
        addata.stationName = n
      }


      let mxlist = ref<any[]>([])
      function showpop(num:any,row?:any){
        type.value = num
        
        if(type.value == 1){
          openpop.value = true
        }else if(type.value == 2){
          drawer.value = true
        }

        ystit.value = num == 1?'添加费用标准':'修改费用标准'
        yysRef.value?.resetFields()
        if(row){
          addata.pkId = row.pkId
          
          addata.stationCode = row.stationCode	             
          addata.dayBegin = row.dayBegin.replace(/^(\d{4})*(\d{2})(\d{2})$/, '$2$3')	                
          addata.dayEnd = row.dayEnd.replace(/^(\d{4})*(\d{2})(\d{2})$/, '$2$3')	      
          addata.stationName = row.stationName
          addata.name = row.name            
          addata.commonRemainFee = row.commonRemainFee?row.commonRemainFee/100:0	        
          addata.commonRemainSocRange = row.commonRemainSocRange	    
          addata.moreRemainSoc = row.moreRemainSoc	          
          addata.moreRemainFee = row.moreRemainFee?row.moreRemainFee/100:0	          
          addata.fewRemainSoc = row.fewRemainSoc	            
          addata.fewRemainFee = row.fewRemainFee?row.fewRemainFee/100:0

          addata.eachKmFee = row.eachKmFee?row.eachKmFee/100:0	                
          addata.eachSocFee = row.eachSocFee?row.eachSocFee/100:0	              
          addata.eachKwhFee =	row.eachKwhFee?row.eachKwhFee/100:0                

          standardId.value = row.pkId
          bzName.value = row.name
          stationName.value = row.stationName
          stationCode.value = row.stationCode
          mxlist.value = row.detailList
        }else{
          addata.pkId = ''
          addata.stationCode = ''	             
          addata.dayBegin = ''	                
          addata.dayEnd = ''	
          addata.stationName = ''
          addata.name = ''                     
          addata.commonRemainFee = ''	        
          addata.commonRemainSocRange = ''	    
          addata.moreRemainSoc = ''	          
          addata.moreRemainFee = ''	          
          addata.fewRemainSoc = ''	            
          addata.fewRemainFee = ''      
          addata.eachKmFee = ''	                
          addata.eachSocFee = ''	              
          addata.eachKwhFee =	''        
        }
      }
      function savehand() {
        //后端单位为分 前端单位为元  涉及钱的需乘100
        let addObj = Object.assign({},addata,{
          commonRemainFee:addata.commonRemainFee*100,
          moreRemainFee:addata.moreRemainFee*100,
          fewRemainFee:addata.fewRemainFee*100,
          eachKmFee:addata.eachKmFee*100,
          eachSocFee:addata.eachSocFee*100,
          eachKwhFee:addata.eachKwhFee*100
        })
        if(type.value == 1){
            return addhdz(addObj)
        }
        return update(addObj)
        
      }
      //保存
      function handleAdd() {
        yysRef.value?.validate((valid:Boolean) => {
            if (valid) {
                savehand().then(() => {
                ElMessage({
                    type: 'success',
                    message: type.value == 1?'添加成功':'修改成功'
                })
                if(type.value == 2){
                  bzName.value = addata.name
                  stationName.value = addata.stationName
                  stationCode.value = addata.stationCode
                }
                setTimeout(()=> {
                    if(type.value == 1){
                      openpop.value = false
                    }
                    getyys()
                },600)
            })
            }
        })
      }

    //取消
    function cancel() {
      openpop.value = false
    }

      //删除
    function handleDelete(row:any) {
      ElMessageBox.confirm(
        `确定删除：${row.name}`,
        '温馨提示',
        {
            // confirmButtonText: '',
            // cancelButtonText: 'Cancel',
            type: 'warning',
        }
      ).then(() => {
        delhdz(row.pkId).then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功'
            })
            getyys()
          })
      })
    }

    //费用标准明细
    let drawer = ref(false)
    //let standardId = ref('')
    function handleClose() {
      drawer.value = false
    }

    function resetupdate() {
      yysRef.value?.resetFields()
    }
    // function bzmingx(id:string) {
    //   drawer.value = true
    //   standardId.value = id
    // }
  </script>
  <style scoped>
  .el-divider--horizontal{
    border-color:#5b98cd;
  }
  .mktit{
    width: 100%; 
    height: 40px;
    line-height: 40px;
    border-radius: 10px 10px 0 0;
    font-size: 18px;
    font-weight: bold;
    color: #fff;
    text-align: center;
    background: #98c3ef;
  }
  .jqbg{
    background: #73b9c9;
  }
  </style>