<script>
import auditGroupPicker from '@product/iam/components/picker/audit-group-picker.vue'
import pickValues from '@/common/services/pick-values'
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import components from './_import-components/xm-audit-cpm-checkself-setdept-handler-import'

export default {
  name: 'XmAuditCpmCheckselfSetdeptHandler',
  components,
  data() {
    return {
      button: {},
      initValues: [],
      childTable: undefined,
      selectedRows: [],
      eventScript: undefined,
      filterDeptId: undefined,
    }
  },
  methods: {
    run({ button, eventScript, context: { args, dataList } }) {
      this.button = button
      this.childTable = dataList
      this.selectedRows = dataList.getSelectedRows()
      // 根据需修改的字段，获取字段信息
      this.eventScript = eventScript
      /// 获取非隐藏 且 包含在事件配置修改字段中的字段
      // this.initValues =
      //   childTable?.fields.filter((f) => !f.attrFD?.hidden && modifyFields.includes(f.name)) || []

      this.fnGetProjectChildTable().then((res) => {
        // 记录过滤部门ID
        if (res) {
          this.filterDeptId = res
        }
        // 获取项目的被检查单位
        axios({
          method: 'get',
          url: `api/xcoa-mobile/v1/cpm-work-paper/getProject?id=${this.$route.query.projectId}`,
        }).then((res) => {
          const checkdeptnames = res.data[0].checkedDeptNames
          const checkdeptcodes = res.data[0].checkedDeptIds
          const names = checkdeptnames.split(',')
          const codes = checkdeptcodes.split(',')
          let orgfw = []
          names.forEach((n, index) => {
            orgfw.push({
              type: 'Group',
              name: n,
              code: codes[index],
            })
          })
          if (this.filterDeptId) {
            orgfw = orgfw.filter((item) => item.code === this.filterDeptId)
          }
          pickValues(auditGroupPicker, {
            single: orgfw.length === 1,
            selectchecked: true,
            selectcheckfn: () => {
              return new Promise((resolve) => {
                resolve(orgfw)
              })
            },
          }).then((res) => {
            if (!res | (res && res.length === 0)) return
            this.deptChange(res.map((item) => item.code))
          })
        })
      })
    },
    deptChange(deptList) {
      const projectId = this.$route.query.projectId
      const groupId = this.$route.query.groupId
      const deptIds = deptList.join(',')
      axios({
        url: `api/framework/v1/cpmcheckself/cpmCheckSelfEntitySave?projectId=${projectId}&subProjectId=${groupId}&ids=${deptIds}`,
        method: 'get',
      }).then((res) => {
        if (res.data.success) {
          message.success('分配完成')
        } else {
          message.error(res.data.message)
        }
        this.childTable.refresh()
      })
    },
    // 获取项目子表数据
    fnGetProjectChildTable() {
      const groupId = this.$route.query.groupId
      // 有gropuId说明是子项目，需要过滤部门
      if (groupId.toString() === '1') {
        return new Promise((resolve) => {
          resolve()
        })
      }
      return axios({
        method: 'post',
        url: `api/framework/v1/page/wp/onl:cd78040cdda84c6b90b2c688d9bfbaa1`,
        data: {
          id: this.$route.query.projectId,
        },
      }).then((res) => {
        const list = JSON.parse(
          res.data.pageFormData.pageFieldInfos.find((item) => {
            return item.name === 'CPM_SUB_PROJECT'
          }).value
        )
        return list.filter((item) => {
          return item.id.toString() === groupId.toString()
        })[0].CHECKED_DEPT_ID
      })
    },
  },
  render: () => {
    return ''
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
:global(.deptTable) {
  :global(.ant-table-tbody > tr > td) {
    border-right: none;
    border-left: none;
  }
  :global(.ant-table-thead > tr > th) {
    border-right: none;
    border-left: none;
  }
  :global(table) {
    border: none !important;
  }
  :global(.ant-table-placeholder) {
    border: none !important;
  }
}
</style>
