import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { getAllRolesList, queryUserByDepId } from '/@/views/system/user/user.api';
const objTmp={1:"year",2:"quarter",3:"month"}
const valueFormat={1:"YYYY",2:"YYYYqQ",3:"YYYY-MM"}
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '报表名称',
    align: 'left',
    dataIndex: 'name',
    width: "20%",
    resizable:true
  },
  {
    title: '报表类型',
    align: 'left',
    dataIndex: 'tp',
    customRender: ({ text }) => {
      return render.renderDict(text, 'report_tp');
    },
    width: "7%",
    resizable:true
  },
  {
    title: '填报角色',
    align: 'left',
    dataIndex: 'enterRole',
    width:0,
    ifShow: false,
    resizable:true
  },
  {
    title: '负责人',
    align: 'left',
    dataIndex: 'respUser_dictText',
    width: "7%",
    resizable:true
  },
  {
    title: '联系电话',
    align: 'left',
    dataIndex: 'enterTel',
    width: "9%",
    resizable:true
  },
  {
    title: '统计时段',
    align: 'left',
    dataIndex: 'enterDate',
    customRender: ({ text }) => {
      return !text ? '' : text.length > 10 ? text.substr(0, 10) : text;
    },
    width: "9%",
    resizable:true
  },
  {
    title: '开始日期',
    align: 'center',
    dataIndex: 'startDate',
    customRender: ({ text }) => {
      return !text ? '' : text.length > 10 ? text.substr(0, 10) : text;
    },

    ifShow: false,
    resizable:true
  },
  {
    title: '结束日期',
    align: 'center',
    dataIndex: 'endDate',
    customRender: ({ text }) => {
      return !text ? '' : text.length > 10 ? text.substr(0, 10) : text;
    },
    ifShow: false,
    resizable:true
  },
  {
    title: '报告状态',
    align: 'left',
    dataIndex: 'sta',
    ifShow: true,
    width: "8%",
    resizable:true,
    customRender: ({ text }) => {
      let color = "red";
      if(!text||text===0) { color = "red";}
      if(text===1) { color = "green"}
      if(text===2) { color = "orange"}
      if(text===3) { color = "red"}
      if(text===4) { color = "gray"}
      if(text===9) {color = "blue"}
      return render.renderTag(render.renderDict(text, 'fxc_report_sta'), color);
    },
  },
  {
    title: '已分配数',
    align: 'left',
    dataIndex: 'allocNum',
    ifShow: true,
    width: "6%",
    resizable:true
  },
  {
    title: '总节点',
    align: 'left',
    dataIndex: 'tplItemNum',
    ifShow: true,
    width: "6%",
    resizable:true
  },
  {
    title: '填报数',
    align: 'left',
    dataIndex: 'fillStaNum',
    ifShow: true,
    width: "6%",
    resizable:true
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '报表名称',
    field: 'name',
    component: 'Input',
    // helpMessage: '前后输入*号可以实现模糊查询',
  },{

  //   label: '状态',
  //   field: 'sta',
  //   component: 'JDictSelectTag',
  //   componentProps: {
  //     dictCode: 'fxc_report_sta',
  //     placeholder: '选择状态',
  //   },
  // },
  // {
    label: '报表类型',
    field: 'tp',
    component: 'JDictSelectTag',
    colProps: { lg: 5 },
    // itemProps:{labelCol:{xs: {span: 24},sm: {span: 12},},wrapperCol:{xs: {span: 24},sm: {span: 16},}},
    componentProps: {
      dictCode: 'report_tp',
      placeholder: '请选择',
      stringToNumber: true,
    },


  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '报表名称',
    field: 'name',
    component: 'Input',
    required: true,
  },
  {
    label: '报表类型',
    field: 'tp',
    component: 'JDictSelectTag',
    componentProps: ({ formActionType, formModel }) => {
      return {
      dictCode: 'report_tp',
      placeholder: '请选择',
      stringToNumber: true,
      sync: true,
        onChange: async (tp) => {
          const { updateSchema } = formActionType;
    
          updateSchema([
              {
                field: 'enterDate',
                component:'DatePicker',
                componentProps: { 
                  picker:objTmp[tp],
                  valueFormat: valueFormat[tp],
                  format: valueFormat[tp],  
                  onChange: (val) => {
                    let date = new Date(val);
                    let year = date.getFullYear();
                    let month = date.getMonth()+1;
                    let stDate = "";
                    let endDate="";
                  if(formModel.tp==3) {
                      let firstDate = year + "-" + (month<10?'0'+month:month) +"-01";
                      let lastDate = new Date(year, month, 0).getDate();
                      stDate = firstDate;
                      endDate =  year + "-" + (month<10?'0'+month:month) + "-" + lastDate

                    } else  if(formModel.tp==2) {
                      let firstDate = date;
                      let quarter = Math.floor((firstDate.getMonth() + 3) / 3); 
                      let lastMonth = quarter * 3; 
                      let lastDate = new Date(year, lastMonth, 0).getDate(); 
                      stDate = firstDate.toISOString().slice(0, 10)
                      endDate = year + "-" + (lastMonth<10?'0'+lastMonth:lastMonth) + "-" + lastDate
                    
                    } else if(formModel.tp==1) {
                      stDate = year + "-01-01";
                      endDate = year + "-12-31"
                    }
              
                    formModel.startDate=stDate
                    formModel.endDate= endDate
                  }
                  },
                },
            ]);
            formModel.enterDate=''
            formModel.startDate= ""
            formModel.endDate= ""
          }
        }
    },
    required: true,
  },
  {
    label: '统计时段',
    field: 'enterDate',
    component: 'DatePicker',
    componentProps: ({ formActionType, formModel }) => {   
      const tp = formModel.tp
      console.log(tp)
      console.log(objTmp[tp])
      console.log(valueFormat[tp])
      return {
 
        picker:objTmp[tp],
        valueFormat: valueFormat[tp],
        format: valueFormat[tp] 
      } 
    },
    required: true,
  },
  {
    label: '开始日期',
    field: 'startDate',
    dynamicDisabled: false,
    component: 'DatePicker',
      componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: "YYYY-MM-DD",
    },
    required: true,
  },
  {
    label: '结束日期',
    field: 'endDate',
    component: 'DatePicker',
    dynamicDisabled: false,
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: "YYYY-MM-DD",
    },
    required: true,
  },
  {
    label: '填报角色',
    field: 'enterRole',
    component: 'ApiSelect',
    componentProps: {
      mode: 'radio',
      api: getAllRolesList,
      labelField: 'roleName',
      valueField: 'id',
    },
    required: true,
  },

  {
    label: '填报部门',
    field: 'respDepart',
    required: true,
    component: 'JSelectDept',
    componentProps: ({ formActionType, formModel }) => {
   
      return {
        sync: false,
        checkStrictly: true,
        defaultExpandLevel: 2,
        multiple:false,
        onSelect: async (options, values) => {
          // const depId = values.value[0]
          // if(depId.length==32){
          //   const users = await queryUserByDepId({id:depId});
          //   const newArray = users.map(user => ({
          //     label: user.realname,
          //     value: user.id
          //   }));
          //   const { updateSchema } = formActionType;
          //   updateSchema([
          //     {
          //       field: 'respUser',
          //       componentProps: { options: newArray },
          //     },
          //   ]);
          // }
        },
      };
    },
  },
  {
    label: '负责人',
    field: 'respUser',
    component: 'JSelectUserByDept',
    componentProps:{
        labelKey:'realname',
        rowKey:'username'
    },
    required: true,
  },

  {
    label: '联系电话',
    field: 'enterTel',
    component: 'Input',
    required: true,
  },
  
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];


//表单数据
export const formImportDataSchema: FormSchema[] = [
  {
    label: 'XML目录',
    field: 'folderPath',
    component: 'Input',
    required: true,
  },
]

/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
