import type { TableData } from 'lib/type/TableData'
import In18FormDialog from '../../components/In18FormDialog.vue'
import { LOCALES } from '../../data/const'

const data: TableData = {
  async fetchFun(self, data) {
    const res = await self.api?.getDataList(data)
    return res
  },
  addNods: [
    (self: any) => {
      return (
        self.bean &&
        <In18FormDialog
          show={self.bean.showEditorDialog}
          id={self.bean.id}
          form={self.bean.form}
          locales={self.bean.locales}
          onClose={() => {
            self.bean.showEditorDialog = false;
            self.bean.id = null;
          }}
          onSubmit={async (data) => {
            const params = {
              ...data,
              id: data.id ? String(data.id) : undefined,
              job_area: data.job_area ? String(data.job_area) : undefined,
              job_unit: data.job_unit ? String(data.job_unit) : undefined,
              job_type: data.job_type ? String(data.job_type) : undefined,
              recruit_count: data.recruit_count ? String(data.recruit_count) : undefined,
            }
            if(data.id) {
              await self.api?.updateData(params)
            } else {
              await self.api?.addData(params)
            }
            self.bean.showEditorDialog = false;
            self.bean.id = null;
            self.methods.fetchData()
          }}
          translateApi={self.api?.translate}
          detailApi={async (id: number) => {
            const res = await self.api?.getDetail(id);
            const data = res.data;
            Object.keys(data).forEach((langKey) => {
              const langData = data[langKey];
              Object.keys(langData).forEach((fieldKey) => {
                const field = self.bean.form.find((f:any) => f.key === fieldKey);
                if (fieldKey === 'title') {
                  data[langKey]['name'] = langData[fieldKey];
                }
                if(fieldKey === 'category') {
                  data[langKey]['job_type'] = langData[fieldKey].job_type;
                  data[langKey]['job_area'] = langData[fieldKey].job_area;
                  data[langKey]['job_unit'] = langData[fieldKey].job_unit;
                }
                if (!field) return;
                data[langKey][fieldKey] = langData[fieldKey];
              })
            })
            console.log('data', data)
            return data
          }}
        />
      )
    }
  ],
  launchTask: [
    async (self: any) => {
      if (self.bean) {
        self.bean.id = null;
        console.log('self.bean.jobTypes', self.bean.jobTypes)
        self.bean.form = [
          {
            name: '职业类别',
            key: 'job_type',
            type: 'select',
            value: '',
            getItems: () => self.bean.jobTypes,
            must: true,
            shouldTranslate: false,
          },
          {
            name: '业务领域',
            key: 'job_area',
            type: 'select',
            getItems: () => self.bean.jobAreas,
            value: '',
            must: true,
            shouldTranslate: false,
          },
          {
            name: '所属板块',
            key: 'job_unit',
            type: 'select',
            getItems: () => self.bean.jobUnits,
            value: '',
            must: true,
            shouldTranslate: false,
          },
          {
            name: '招聘人数',
            key: 'recruit_count',
            type: 'input',
            value: '',
            must: true,
            shouldTranslate: false,
          },
          {
            name: '城市',
            key: 'city',
            type: 'input',
            value: '',
            must: true,
            shouldTranslate: false,
          },
          {
            name: '岗位名称',
            key: 'name',
            type: 'input',
            value: '',
            must: true,
            shouldTranslate: true,
          },
          {
            name: '岗位描述',
            key: 'description',
            type: 'textarea',
            value: '',
            must: true,
            shouldTranslate: true,
          },
          {
            name: '任职需求',
            key: 'requirement',
            type: 'textarea',
            value: '',
            must: true,
            shouldTranslate: true,
          },
          {
            name: '联系方式',
            key: 'contract_info',
            type: 'input',
            value: '',
            must: true,
            shouldTranslate: true,
          },
        ];
        self.bean.locales = LOCALES
      }
    },
    async (self: any) => {
      if (self.bean) {
        const types = ['job_type', 'job_area', 'job_unit'] as const
        const results = await Promise.all(
          types.map((type) =>
            self.api?.getCategoryList({ page: 1, size: 1000, type })
          )
        )
        const toOptions = (items: any[]) => items.map((item: any) => ({ key: item.id, name: item.name }));
        
        [self.bean.jobTypes, self.bean.jobAreas, self.bean.jobUnits] = results.map(
          (res) => toOptions(res?.data?.items ?? [])
        )
      }
    }
  ],
  fliter: [
    {
      key: 'id',
      name: 'ID',
      type: 'input'
    },
    {
      key: 'lang',
      name: '语言',
      type: 'select',
      value: 'ZH',
      getItems: (self) => {
        return self.bean ? self.bean.locales : []
      }
    },
    {
      key: 'title',
      name: '职位名称',
      type: 'input',
    },
    {
      key: 'job_type',
      name: '职业类别',
      type: 'select',
      getItems: (self) => {
        return self.bean ? self.bean.jobTypes : []
      }
    },
    {
      key: 'job_area',
      name: '业务领域',
      type: 'select',
      getItems: (self) => {
        return self.bean ? self.bean.jobAreas : []
      }
    },
    {
      key: 'job_unit',
      name: '所属板块',
      type: 'select',
      getItems: (self) => {
        return self.bean ? self.bean.jobUnits : []
      }
    },
    {
      type: 'onlyFun',
      onlyFun: {
        title: '新建',
        type: ' ' as any,
        fun(self) {
          if (self.bean) {
            self.bean.id = null;
            self.bean.showEditorDialog = true;
          }
        }
      }
    },
  ],
  tableColumns: [
    {
      key: 'id',
      name: 'ID',
      width: '80px',
      showJson: '*'
    },
    {
      key: 'lang',
      name: '语言',
      width: '80px'
    },
    {
      key: 'title',
      name: '职位名称',
      width: '200px'
    },
    {
      key: 'category',
      name: '职业类型 | 业务领域 | 所属板块',
      renderBodyCell(data) {
        let item: any = {}
        try {
          item = JSON.parse(data.row.category)
        } catch (error) {
          item = data.row.category
        }
        return <div style="display: flex; gap: 5px;">
          <el-tag>{item.job_type_name}</el-tag>
          <el-tag type="warning">{item.job_area_name}</el-tag>
          <el-tag type="success">{item.job_unit_name}</el-tag>
        </div>
      },
    },
    {
      key: 'description',
      name: '岗位描述',
    },
    {
      key: 'requirement',
      name: '任职需求',
    },
    {
      key: 'recruit_count',
      name: '招聘人数',
      width: '80px'
    },
    {
      key: 'contract_info',
      name: '联系方式',
      width: '200px'
    },
    {
      key: 'create_time',
      name: '创建时间',
    },
    {
      key: 'table_tools',
      name: '操作',
      buttons: [
        {
          type: 'onlyFun',
          key: 'update',
          onlyFun: {
            title: '编辑',
            type: ' ' as any,
            fun(self, row) {
              if (self.bean) {
                self.bean.id = row.id;
                self.bean.showEditorDialog = true;
              }
            }
          }
        },
        {
          type: 'popoverConfirm',
          key: 'delete',
          confirm: {
            title: '删除',
            primary: 'id',
            subFun(self, data) {
              return self.api?.deleteData(data)
            }
          }
        }
      ]
    }
  ]
}

export default data
