<template>
    <div>
      <div v-if="zjtype == 1 && 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;">
        <template v-if="zjtype == 1">
            <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>
        </template>
        <template v-else>
          <el-button 
            type="primary" 
            plain 
            icon="Plus" 
            @click="showpop(1)">新增</el-button>
        </template>
        
          
        <el-table 
          style="width:100%;margin-top: 10px;"
          :data="yysList"
          border
          stripe
          :max-height="zjtype == 1?'600px':'200px'"
        >
            
            <el-table-column label="机器人编码" min-width="200" align="center" prop="code" :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.code}}</div>
              </template>
            </el-table-column>
            <el-table-column v-if="zjtype == 1" label="换电站" min-width="200" align="center" prop="stationName" :show-overflow-tooltip="true" />
            <el-table-column label="运行模式" min-width="100" align="center" prop="runMode" :show-overflow-tooltip="true">
                <template #default="scope">
                    <div v-if="scope.row.runMode == 1">就地</div>
                    <div v-else-if="scope.row.runMode == 2">远程</div>
                </template>
            </el-table-column>
            <el-table-column label="状态" min-width="100"  width="100" align="center" prop="status" :show-overflow-tooltip="true">
                <template #default="scope">
                    <div v-if="scope.row.status == 1">正常</div>
                    <div v-else>禁用</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="100" 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 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="600px" 
          append-to-body
          :close-on-click-modal="false"
        >
        <el-form ref="yysRef" :model="addata" :rules="rules" label-width="110px">
          <el-row>

            <el-col v-if="zjtype == 1" :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.native="gethdzname(n.name)" />
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="机器人编码" prop="code">
                    <el-input v-model="addata.code" placeholder="请输入机器人编码" clearable />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="状态" prop="status">
                <el-radio-group v-model="addata.status">
                    <el-radio value="1">正常</el-radio>
                    <el-radio value="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="运行模式" prop="runMode">
                <el-radio-group v-model="addata.runMode">
                    <el-radio 
                     v-for="n in yxtype"
                     :value="n.dictValue">{{n.dictName}}</el-radio>
                </el-radio-group>
                </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" name="UserPage">
        import { 
            gethdzlist
        } from '@/api/swapstation/hdz'

        import { 
            getjqrlist, 
            addhdz, 
            update,
            delhdz
        } from '@/api/swapstation/robot'
       import { 
        getdict
       } from '@/api/systemSet/dict'
      import { ref,reactive} from 'vue'
      import { ElMessage, ElMessageBox } from 'element-plus'

      let search = ref(true)

      const props = withDefaults(defineProps<{
        zjtype?:string|number
        stationCode?:string
        stationName?:string
        list?:any[]
      }>(), {
        zjtype:1,
        stationCode:'',
        stationName:'',
        list:() => []
      })

      const emit = defineEmits<{
        update: []
      }>()

      let queryParams = reactive({
        pageSize:20,	      //每页条数
        pageNo:1,	          //页数
        stationCode:props.zjtype == 1?'':props.stationCode       //换电站编码  ??
      })
      
      //查询
      let total = ref(0)
      let yysList = ref<any>([])
      function handBtnqu(){
        queryParams.pageNo = 1
        getyys()
      }
      
      //初始查询换电站
      let yunList = ref<any>([])
      if(props.zjtype == 1){
        gethdzlist({
          pageSize:20,	      //每页条数
          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()
            }
        })
      }else{
        //queryParams.stationCode = props.stationCode

        if(props.list.length > 0){
          yysList.value = props.list
        }
        //getyys()
      }
      
      
      //初始查询字典类型
      //机器人运行模式类型
      let yxtype = ref<any>([])
      getdict({
        typeCode:'robotyxms'
      }).then(rps => {
        yxtype.value = rps.data
      })  

      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 slecas = ref('')
      let addata = reactive<any>({
        pkId:'',
        stationCode:'',	     //换电站编码
        code:'',	           //机器人编码
        status:'',	         //状态：1-正常 2-禁用
        runMode:'',	         //运行模式：1-就地，2-远程
        stationName:''
      })
       function gethdzname(n:string) {
        addata.stationName = n
       }
      const rules = ref({
        stationCode:[
            { required: true, message: "请选择换电站", trigger: "blur" }
        ],
        code:[
          { required: true, message: "请输入机器人编码", trigger: "blur" },
        ],
        status:[
          { required: true, message: "请输选择状态", trigger: "blur" },
        ]
      })



      function showpop(num:any,row?:any){
        type.value = num
        openpop.value = true
        ystit.value = num == 1?'添加机器人':'修改机器人'
        yysRef.value?.resetFields()
        if(row){
          addata.pkId = row.pkId
          addata.stationCode = row.stationCode	
          addata.stationName = row.stationName     
          addata.code = row.code	                 
          addata.status = row.status+''	             
          addata.runMode = row.runMode+''	                     

        }else{
          addata.pkId = ''
          addata.stationCode = props.zjtype == 1?'':props.stationCode
          addata.stationName = props.zjtype == 1?'':props.stationName
          addata.code = ''
          addata.status = ''
          addata.runMode = ''      
                               
        }
      }
      function savehand() {
        if(type.value == 1){
            return addhdz(addata)
        }
        return update(addata)
        
      }
      //保存
      function handleAdd() {
        yysRef.value?.validate((valid:Boolean) => {
            if (valid) {
                savehand().then(() => {
                ElMessage({
                    type: 'success',
                    message: type.value == 1?'添加成功':'修改成功'
                })
                setTimeout(()=> {
                    openpop.value = false
                    getyys()
                    emit('update')
                },600)
            })
            }
        })
      }

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

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

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