<template>
    <div>
      <div style="padding:10px 0 20px;">
        <el-button 
          type="primary" 
          plain 
          icon="Plus" 
          @click="showpop(1)">新增</el-button>
        <el-table 
          style="width:100%;margin-top: 10px;"
          :data="yysList"
          border
          stripe
          highlight-current-row
          max-height="650px"
        >
            <el-table-column min-width="100" label="开始时间" align="center" prop="timeBegin" :show-overflow-tooltip="true" />
            <el-table-column min-width="100" label="结束时间" align="center" prop="timeEnd" :show-overflow-tooltip="true" />
            <el-table-column min-width="100" label="每公里收费(元)" align="center" prop="eachKmFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.eachKmFee?scope.row.eachKmFee/100:''}}</div>
              </template>
            </el-table-column>
            <el-table-column min-width="100" label="每SOC收费(元)" align="center" prop="eachSocFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.eachSocFee?scope.row.eachSocFee/100:''}}</div>
              </template>
            </el-table-column>
            <el-table-column min-width="100" label="每度电收费(元)" align="center" prop="eachKwhFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.eachKwhFee?scope.row.eachKwhFee/100:''}}</div>
              </template>
            </el-table-column>
            <el-table-column min-width="100" label="谷段服务费(元)" align="center" prop="serviceFee" :show-overflow-tooltip="true">
              <template #default="scope">
                <div>{{scope.row.serviceFee?scope.row.serviceFee/100:''}}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150" fixed="right">
              <template #default="scope">
                <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>
  
    
      <!-- 添加或修改用户配置对话框 -->
        <el-dialog 
          v-model="openpop" 
          :title="ystit" 
          width="750px" 
          append-to-body
          :close-on-click-modal="false"
        >
        <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="timeBegin">
                <!-- <el-time-picker 
                  v-model="addata.timeBegin" 
                  placeholder="请选择开始时间" 
                  value-format="HH:mm:ss"
                  style="width: 100%;" 
                /> -->

                <el-time-picker
                  v-model="tiem"
                  :editable="false"
                  is-range
                  range-separator="至"
                  value-format="HH:mm:ss"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  @change="tiemchange"
                />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="结束时间" prop="timeEnd">
                <el-time-picker 
                  v-model="addata.timeEnd" 
                  placeholder="请选择结束时间" 
                  value-format="YYYY-MM-DD" 
                  style="width: 100%;" 
                />
              </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-col :span="12">
              <el-form-item label="谷段服务费(元)" prop="serviceFee">
                <el-input 
                  v-model="addata.serviceFee" 
                  placeholder="请输入谷段服务费" 
                  maxlength="50" 
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="handleAdd">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  <script setup lang="ts">
      import { 
        getmxlist, 
        addhdz, 
        update,
        delhdz
      } from '@/api/swapstation/costdetails'

      import { setNum } from '@/utils/validate'
      import { ref,reactive,watch} from 'vue'
      import { ElMessage, ElMessageBox } from 'element-plus'

      const props = defineProps<{
        stationCode:String
        standardId:String
        list:any[]
      }>()

      let queryParams = reactive({
        standardId:props.standardId       //收费规则id 
      })
      
      //查询
      let yysList = ref<any>([])
      if(props.list.length > 0){
        yysList.value = props.list
      }

      function getyys() {
        yysList.value.splice(0)
        getmxlist(queryParams).then(rps => {
            if(rps.data){
              yysList.value = rps.data
            }
        })
      }
      
      //新增、修改
      let type = ref(1)
      let openpop = ref(false)
      let ystit = ref('添加费用标准明细')
      let yysRef = ref()
      let addata = reactive<any>({
        stationCode:props.stationCode,	          //换电站编码
        standardId:props.standardId,	            //收费规则ID
        timeBegin:'',	                            //开始时间
        timeEnd:'',	                              //结束时间
        pkId:'',
        eachKmFee:'',	                            //每公里收费
        eachSocFee:'',	                          //每SOC收费
        eachKwhFee:'',	                          //每度电收费
        serviceFee:''	                            //谷段服务费
      })

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

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

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

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

      const rules = ref({
        stationCode:[
            { required: true, message: "请选择换电站", trigger: "blur" }
        ],
        standardId:[
            { required: true, message: "请选择收费规则", trigger: "blur" }
        ],
        timeBegin:[
          { required: true, message: "请选择开始和结束时间", trigger: "blur" },
        ],
        timeEnd:[
          { required: true, message: "请选择结束时间", trigger: "blur" },
        ]
      })

      //时间选框确定事件
      let tiem = ref<any[]>([])
      function tiemchange(v:any) {
        if(v){
          addata.timeBegin = v[0]	      //开始时间
          addata.timeEnd = v[1]	        //结束时间
        }else{
          addata.timeBegin = ''
          addata.timeEnd = ''
        }
        
      }

      function showpop(num:any,row?:any){
        type.value = num
        openpop.value = true
        ystit.value = num == 1?'添加费用标准明细':'修改费用标准明细'
        yysRef.value?.resetFields()
        if(row){
          tiem.value.push(row.timeBegin)
          tiem.value.push(row.timeEnd)
          addata.pkId = row.pkId	                    
          addata.timeBegin = row.timeBegin	                      
          addata.timeEnd = row.timeEnd	                          
          addata.eachKmFee = row.eachKmFee?row.eachKmFee/100:0	                      
          addata.eachSocFee = row.eachSocFee?row.eachSocFee/100:0	                    
          addata.eachKwhFee = row.eachKwhFee?row.eachKwhFee/100:0	                    
          addata.serviceFee = row.serviceFee?row.serviceFee/100:0	        

        }else{
          tiem.value.splice(0)
          addata.pkId = ''
          addata.timeBegin = ''
          addata.timeEnd = ''
          addata.eachKmFee = ''
          addata.eachSocFee = ''
          addata.eachKwhFee = ''
          addata.serviceFee = ''        
        }
      }
      function savehand() {
        let addObj = Object.assign({},addata,{
          eachKmFee:addata.eachKmFee*100,
          eachSocFee:addata.eachSocFee*100,
          eachKwhFee:addata.eachKwhFee*100,
          serviceFee:addata.serviceFee*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?'添加成功':'修改成功'
                })
                setTimeout(()=> {
                    openpop.value = false
                    getyys()
                },600)
              })
            }
        })
      }

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

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

    getyys()
  </script>
  <style scoped>
  .el-divider--horizontal{
    border-color:#5b98cd;
  }
  </style>