<template>
  <div :class="$style.userDiv">
    <sd-user-picker ref="userPicker" :single="true" @change="change"></sd-user-picker>
  </div>
</template>

<script>
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import components from './_import-components/xm-audit-cpm-checkself-setuser-handler-import'

export default {
  name: 'XmAuditCpmCheckselfSetuserHandler',
  components,
  data() {
    return {
      visible: false,
      button: {},
      initValues: [],
      childTable: undefined,
      selectedRows: [],
      eventScript: undefined,
    }
  },
  methods: {
    run({ button, eventScript, context: { args, dataList } }) {
      this.button = button
      this.childTable = dataList
      this.selectedRows = dataList.getSelectedRows()
      // 根据需修改的字段，获取字段信息
      this.eventScript = eventScript

      if (this.selectedRows.length === 0) {
        message.info('请选择需要分配的文件')
        return
      }
      this.$refs.userPicker.openPicker()
    },
    change(userList) {
      const userAccount = userList[0].code
      const flowOverData = this.selectedRows.filter((item) => item.CURRENT_STATE === '已反馈')
      if (flowOverData.length > 0) {
        message.warning('已反馈文件无法设置联系人，请重新选择!')
        return
      }
      const ids = this.selectedRows
        .map((item) => {
          return item.id
        })
        .join(',')
      axios({
        url: `api/framework/v1/cpmcheckself/cpmCheckSelfEntityIssued?userAccount=${userAccount}&ids=${ids}`,
        method: 'get',
      }).then((res) => {
        if (res.data.success) {
          message.success('分配完成')
          this.childTable.refresh()
        } else {
          message.warning(res.data.message, 1)
        }
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.user-div {
  display: none;
}
</style>
