<template>
  <a-card>
    <!-- 高级搜索区域 -->
    <audit-advanced-query
      :expand="expand"
      :search-data="advSearchForm"
      :ref-name="searchform"
      :search-style="{ height: '170px', left: '20px', top: '60px' }"
      :search-fun="advSearch"
      @searchedClick="searchedClick"
      @resetForm="resetForm"
    >
      <template>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目名称" prop="projectName">
              <a-input v-model="advSearchForm.projectName" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目编号" prop="projectCode">
              <a-input v-model="advSearchForm.projectCode" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="整改责任主体" prop="auditedUnit">
              <sd-group-picker
                ref="unitSelect"
                v-model="advSearchForm.auditedUnit"
                :single="true"
                :read-only="false"
                @change="changeunit"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="'状\u2003\u2003态'" prop="planStatus">
              <sd-select
                v-model="advSearchForm.planStatus"
                :allow-clear="true"
                :options="itemStatusOptions"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </template>
    </audit-advanced-query>
    <div :class="[$style.btns]">
      <audit-advanced-export
        ref="exportExcel"
        :v-if="true"
        :class-style="[$style.buttonSpacing]"
        :config-id="191"
        :exclebxh="false"
      ></audit-advanced-export>
    </div>
    <div :class="$style.rectPlanProjectlist">
      <!-- data-url="api/xcoa-mobile/v1/iam-work-rect-feedback/all-list" -->
      <sd-data-table-ex
        ref="dataTable"
        :projectlist="true"
        :class="$style.rectfeedbacklist"
        :columns="columns"
        :actions="actions"
        show-selection
        form-id="iamWorkRectFeedback"
        data-url="api/xcoa-mobile/v1/rect-feed-back/list"
        :filter-expressions="tableExpressions"
        :show-advance-query="true"
        :search-fields="['projectName']"
        @searchbtnClick="searchbtnClick"
      >
        <div slot="isLink" slot-scope="text, record">
          <a :title="text" @click="linkProject(record)">{{ text }}</a>
        </div>
        <div slot="isnum" slot-scope="text, record">
          <span style="padding-left:43%">{{ text }}</span>
        </div>
        <div slot="opt" slot-scope="text, record" :class="$style.operatecol">
          <!-- 管理员 项目组长 项目副组长 则可以变更 -->
          <span v-for="(bt, index) in buttonsfortable" :key="index">
            <a-button
              v-if="record.changeButtons === 'true'"
              :key="index"
              type="link"
              size="small"
              @click="optbuttonClick(bt.id, record)"
              >{{ bt.label }}
              <!-- <sd-icon :type="bt.icontype" :theme="bt.theme"/> -->
            </a-button>
          </span>
        </div>
      </sd-data-table-ex>
    </div>

    <div v-show="false">
      <sd-user-picker
        ref="assignTeamLeader"
        v-model="mainSendUnit"
        :single="true"
        :read-only="false"
        @change="change"
      />
    </div>
  </a-card>
</template>

<script>
import axios from '@/common/services/axios-instance'
import TableActionTypes from '@/common/services/table-action-types'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import auditAdvancedExport from '../../components/audit-advanced-export'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
// import AuditRectplanprojectService from './audit-rectplanproject-service'
import components from './_import-components/audit-rectfeedback-list-import'

export default {
  name: 'AuditRectfeedbackList',
  metaInfo: {
    title: '整改反馈列表',
  },
  components: { ...components, auditAdvancedQuery, auditAdvancedExport },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      searchYear: '',
      yearArry: [],
      searchValue: '',
      mainSendUnit: [],
      checkboxValue: false,
      pageId: 'audit/rectFeedback/iamWorkRectFeedback',
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          width: '450px',
          scopedSlots: { customRender: 'isLink' },
        },
        {
          title: '编号',
          dataIndex: 'rectPlanId',
          defaultSortOrder: 'descend',
          sdHidden: true,
        },
        {
          title: '项目编号',
          dataIndex: 'projectCode',
          sorter: true,
          width: '200px',
          sortDirections: ['ascend', 'descend'],
        },
        {
          title: '项目Id',
          dataIndex: 'projectId',
          sdHidden: true,
        },
        {
          title: '跟踪人账号',
          dataIndex: 'resppersonsAccount',
          sdHidden: true,
          width: '100px',
        },
        {
          title: '项目来源',
          dataIndex: 'projectSource',
          sdHidden: true,
        },
        {
          title: '跟踪人姓名',
          dataIndex: 'resppersonsName',
          sdHidden: true,
        },
        {
          title: '整改责任主体',
          dataIndex: 'auditedUnitName',
          sorter: true,
          width: '150px',
          sortDirections: ['ascend', 'descend'],
        },
        {
          title: '整改责任主体联系人',
          width: '180px',
          dataIndex: 'auditedContactsName',
        },
        {
          title: '需整改问题数量',
          dataIndex: 'rectProblemNum',
          scopedSlots: { customRender: 'isnum' },
          width: '150px',
        },
        {
          title: '整改中数量',
          dataIndex: 'rectDoProblemNum',
          scopedSlots: { customRender: 'isnum' },
          width: '150px',
        },
        {
          title: '整改完成数量',
          dataIndex: 'rectFinishNum',
          scopedSlots: { customRender: 'isnum' },
          width: '150px',
        },
        {
          title: '风险接受数量',
          dataIndex: 'riskAcceptNum',
          scopedSlots: { customRender: 'isnum' },
          width: '150px',
        },
        {
          title: '当前状态',
          sorter: true,
          dataIndex: 'rectFeedStatus',
          width: '150px',
        },
        {
          title: '当前处理人',
          dataIndex: 'currentUser',
          width: '150px',
        },
        {
          title: '操作',
          dataIndex: 'id',
          scopedSlots: { customRender: 'opt' },
          width: '100px',
        },
        {
          title: '流程ID',
          dataIndex: 'instId',
          sdHidden: true,
        },
      ],
      actions: [
        // {
        //   label: '新建',
        //   id: 'new',
        //   type: TableActionTypes.primary, // 新建按钮，不需要回调，自动处理
        //   permission: null, // 纯前端操作，不需要权限控制
        //   callback: this.createrectfeedback,
        // },
        {
          label: '删除',
          id: 'delete',
          type: TableActionTypes.ex.delete, // 删除按钮，不需要回调，会自动处理（对sd-data-table无效）
        },
        {
          label: '导出',
          permission: null, // 纯前端操作，不需要权限控制
          callback: () => {
            this.$refs.exportExcel.exportdata()
          },
        },
      ],
      tableExpressions: [],
      buttonsfortable: [
        {
          label: '变更',
          id: 'changeleader',
          showCondition: '',
        },
      ],
      searchform: 'searchform',
      expand: false,
      advSearchForm: {
        projectName: '',
        projectCode: '',
        auditedUnit: '',
        planStatus: '',
      },
      itemStatusOptions: [],
      auditTypeOptions: [],
      auditModeOptions: [],
      projectId: '',
      setType: '',
      unitName: '',
    }
  },
  mounted() {
    // 初始化数据字典信息
    this.initDictionaryInfo()
  },
  methods: {
    changeunit(values) {
      this.unitName = ''
      values.forEach((element) => {
        this.unitName = element.name
      })
    },
    createrectfeedback() {
      const url = `/sd-flow-guide?code=PRODUCT_IAM_ZGFK` // 新页面要打开的路由地址
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.$refs.dataTable.refresh()
        }
      })
    },
    initDictionaryInfo() {
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=RECT_FEED_STATUS',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.itemStatusOptions = res.data
        }
      })
    },
    onSearch(value) {
      this.tableExpressions = []
      if (value) {
        const expressions = []
        // 项目名称
        expressions.push({
          dataType: 'str',
          name: 'projectName',
          op: 'like',
          stringValue: `%${value}%`,
        })
        // 项目编号
        expressions.push({
          dataType: 'str',
          name: 'projectCode',
          op: 'like',
          stringValue: `%${value}%`,
        })
        this.tableExpressions.push({
          dataType: 'exps',
          op: 'or',
          expressionsValue: expressions,
        })
      }
    },
    advSearch() {
      this.tableExpressions = []
      // 项目名称
      if (this.advSearchForm.projectName) {
        this.tableExpressions.push({
          dataType: 'str',
          name: 'projectName',
          op: 'like',
          stringValue: `%${this.advSearchForm.projectName}%`,
        })
      }
      // 项目编号
      if (this.advSearchForm.projectCode) {
        this.tableExpressions.push({
          dataType: 'str',
          name: 'projectCode',
          op: 'like',
          stringValue: `%${this.advSearchForm.projectCode}%`,
        })
      }
      // 项目组长
      if (this.advSearchForm.auditedUnit) {
        this.tableExpressions.push({
          dataType: 'str',
          name: 'auditedUnitName',
          op: 'like',
          stringValue: `%${this.unitName}%`,
        })
      }
      if (this.advSearchForm.planStatus[0]) {
        this.tableExpressions.push({
          dataType: 'str',
          name: 'rectFeedStatus',
          op: 'eq',
          stringValue: `${this.advSearchForm.planStatus[0].id}`,
        })
      }
    },
    searchedClick() {
      this.expand = !this.expand
    },
    // 重置年份
    resetForm() {
      this.advSearchForm.auditedUnit = ''
      this.unitName = ''
    },
    linkProject(record) {
      // if (record.flowState === '起草') {
      //   const url = '/sd-webflow/pages/draft/' + record.instId // 编辑面要打开的路由地址
      //   crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
      //     if (refreshFlag) {
      //       // 这里写或者调刷新的方法
      //       this.$refs.dataTable.refresh()
      //     }
      //   })
      // } else {
      window.open('#/sd-webflow/done-pages/' + record.instId)
      // }
    },
    // // 项目组长配置保存
    change(val) {
      let codes = ''
      let names = ''
      if (val.length > 0) {
        val.forEach((e) => {
          codes += e.code + ','
          names += e.name + ','
        })
        codes = codes.substr(0, codes.length - 1)
        names = names.substr(0, names.length - 1)
      }
      return new Promise((resolve) => {
        // AuditRectplanprojectService.changeTeamEditor(this.projectId, codes, names).then((res) => {
        //   if (res.data) {
        //     this.$refs.dataTable.clearSelection()
        //     this.$refs.dataTable.refresh()
        //   }
        // })
      })
    },
    AdvSearchClick() {
      this.expand = !this.expand
    },
    optbuttonClick(val, record) {
      // 变更编辑人
      if (val === 'changeleader') {
        const jsonArry = []
        if (record.resppersonsAccount !== '' && record.resppersonsAccount !== null) {
          if (record.resppersonsAccount.indexOf(',') > -1) {
            const codes = record.resppersonsAccount.split(',')
            const names = record.resppersonsName.split(',')
            for (let i = 0; i < codes.length; i++) {
              jsonArry.push({ type: 'User', code: codes[i], name: names[i], attrs: [] })
            }
          } else {
            jsonArry.push({
              type: 'User',
              code: record.resppersonsAccount,
              name: record.resppersonsName,
              attrs: [],
            })
          }
        }
        this.mainSendUnit = jsonArry
        this.projectId = record.id
        setTimeout(() => {
          this.$refs.assignTeamLeader.openPicker()
        }, 0)
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;

.btns {
  position: absolute;
  top: 24px;
  right: 20.5px;
  z-index: 100;
  display: none;
}

.operatecol {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;

  :global(.ant-btn-sm) {
    padding: 0 3px;
  }
}

.advSearchBtn {
  width: 35px;
  padding: 0px;
}

:global(.ant-card .searchcard_audit-advanced-query_product) {
  right: 17px !important;
}

.buttonSpacing {
  margin-left: 5px;
}

.search-icon {
  font-size: 12px;
  margin-left: 1px !important;
}
// .rectfeedbacklist {
//   :global(.ant-table-column-title) {
//     white-space: normal;
//   }
// }
</style>
