"use strict";
const events =[
   "app.record.index.show",
   "app.record.index.edit.show"
]

kintone.events.on(events,(event)=>{
   onRecordIndexEvent(event);
   return event;
});

function onRecordIndexEvent(event){
   console.log(event.type,event);
   return event;
}


//レコード表示
kintone.events.on('app.record.detail.show', (event) => {
   showRecode(event);
   return event;
});

//レコード編集時
kintone.events.on('app.record.edit.show', (event) => {
   showRecodeEdit(event);
   return event;
});

//新規作成
kintone.events.on('app.record.create.show', (event) => {
   newRecord(event);
   return event;
});

function showRecode(event){
   console.log("app.record.detail.show",event);
   let fld=kintone.app.record.getFieldElement('txtname');
   fld.style.backgroundColor="red";
}

function newRecord(event){
   console.log("app.record.create.show",event);
   //kintone.app.record.setFieldShown("名前",false);
   //let fld = kintone.app.record.getFieldElement("txtname");
   //kintone.app.record.setFieldShown("txtname",false);
   kintone.events.on('app.record.create.change.radio', (event)=>{
      radioChnage(event);
      return event;
   });
}

function showList(event){
   console.log("app.record.index.show",event);
}

function showRecodeEdit(event){
   kintone.events.on('app.record.edit.change.radio', (event)=>{
      radioChnage(event);
      return event;
   });

   let record = event.record;
   let statusValue =record["radio"].value;
   switch (statusValue) {
      case "非表示":
         kintone.app.record.setFieldShown("txtname",false);
         break;
      case "無効":
         kintone.app.record.setFieldShown("txtname",true);
         event.record.txtname.disabled = true;
         break;
      default:
         event.record.txtname.disabled = false;
         kintone.app.record.setFieldShown("txtname",true);
         break;
   }     
   return event;
}

function radioChnage(event){
   let changeField =event.changes.field;
   let statusValue =changeField.value;
   let record = event.record;
   switch (statusValue) {
      case "非表示":
         kintone.app.record.setFieldShown("txtname",false);
         break;
      case "無効":
         kintone.app.record.setFieldShown("txtname",true);
         record.txtname.disabled = true;
         break;
      default:
         record.txtname.disabled = false;
         kintone.app.record.setFieldShown("txtname",true);
         break;
   }
   return event;     
}