<template>
  <div class="role">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <!--项目编号/实验名称-->
        <el-form-item :label="$t('feedBack:search:trials')" v-if="level > 8">
          <el-input v-model="searchData.TrialKeyInfo" clearable style="width: 150px"></el-input>
        </el-form-item>
        <!--中心-->
        <el-form-item :label="$t('feedBack:search:site')" v-if="level > 7">
          <el-input v-model="searchData.TrialSiteCode" clearable style="width: 150px"></el-input>
        </el-form-item>
        <!--受试者访视-->
        <el-form-item :label="$t('feedBack:search:subjectVisit')" v-if="level > 7">
          <el-input v-model="searchData.SubejctAndVisitKeyInfo" clearable style="width: 150px"></el-input>
        </el-form-item>
        <!--角色-->
        <el-form-item :label="$t('feedBack:search:role')" v-if="level > 7">
          <el-select v-model="searchData.UserTypeEnum" clearable style="width: 150px"
            popper-class="feedBack-select-box">
            <el-option v-for="item of UserTypeOptins" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <!--反馈人-->
        <el-form-item :label="$t('feedBack:search:user')" v-if="level > 7">
          <el-input v-model="searchData.FeedBackUserKeyInfo" clearable style="width: 150px"></el-input>
        </el-form-item>
        <!--问题类型-->
        <el-form-item :label="$t('feedBack:search:questionType')">
          <el-select v-model="searchData.QuestionType" clearable style="width: 150px"
            popper-class="feedBack-select-box">
            <el-option v-for="item of QuestionTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <!--问题描述-->
        <el-form-item :label="$t('feedBack:search:description')">
          <el-input v-model="searchData.QuestionDescription" clearable style="width: 150px"></el-input>
        </el-form-item>
        <!--状态-->
        <el-form-item :label="$t('feedBack:search:status')" v-if="level > 7">
          <el-select v-model="searchData.State" clearable style="width: 150px" popper-class="feedBack-select-box">
            <el-option v-for="item of $d.FeedBackStatus" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <!--反馈日期-->
        <el-form-item :label="$t('feedBack:search:time')">
          <el-date-picker v-model="datetimerange" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
            :start-placeholder="$t('feedBack:search:beginTime')" :end-placeholder="$t('feedBack:search:endTime')"
            value-format="yyyy-MM-dd HH:mm:ss" @change="handleDatetimeChange" style="width: 250px"
            popper-class="feedBack-select-box" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
          <!-- <el-button
            type="primary"
            size="mini"
            @click="report"
            v-if="level > 7"
          >
            {{ $t("common:button:export") }}
          </el-button> -->
        </el-form-item>
        <el-button type="primary" @click="resolve" style="float: right" v-if="level > 7"
          :disabled="tableSelectData.length <= 0">
          {{ $t('feedBack:button:resolve') }}
        </el-button>
      </el-form>
      <el-table v-loading="loading" v-adaptive="{ bottomOffset: 45 }" height="100" :data="list" class="table"
        @selection-change="handleSelectChange" @sort-change="handleSortByColumn"
        :default-sort="{ prop: 'CreateTime', order: 'descending' }">
        <el-table-column type="selection" align="center" width="45" v-if="level > 7" :selectable="handleSelectTable" />
        <el-table-column type="index" width="50" />
        <el-table-column :label="$t('feedBack:form:trialCode')" prop="TrialCode" min-width="100" show-overflow-tooltip
          sortable="custom" v-if="level > 8" />
        <el-table-column :label="$t('feedBack:form:study')" prop="ExperimentName" min-width="140" show-overflow-tooltip
          sortable="custom" v-if="level > 8" />
        <el-table-column :label="$t('feedBack:form:siteCode')" prop="TrialSiteCode" min-width="100"
          show-overflow-tooltip sortable="custom" v-if="level > 7" />
        <el-table-column :label="$t('feedBack:form:SubjectCode')" prop="SubjectCode" min-width="120"
          show-overflow-tooltip sortable="custom" v-if="level > 7" />
        <el-table-column :label="$t('feedBack:form:subjectVisit')" prop="SubjectVisitName" min-width="120"
          show-overflow-tooltip sortable="custom" v-if="level > 7" />
        <el-table-column :label="$t('feedBack:form:role')" prop="FeedBackUserName" min-width="100" show-overflow-tooltip
          sortable="custom" v-if="level > 7" />
        <el-table-column :label="$t('feedBack:form:user')" prop="FeedBackFullName" min-width="140" show-overflow-tooltip
          sortable="custom" v-if="level > 7" />
        <el-table-column :label="$t('feedBack:form:questionType')" prop="QuestionType" min-width="180"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            <span>{{
              QuestionTypeOptions.filter(
                (item) => item.value === scope.row.QuestionType
              )[0].label
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('feedBack:form:description')" prop="QuestionDescription" min-width="200"
          show-overflow-tooltip sortable="custom" />
        <el-table-column :label="$t('feedBack:form:time')" prop="CreateTime" min-width="180" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('feedBack:form:status')" prop="State" min-width="120" show-overflow-tooltip
          sortable="custom" v-if="level > 7">
          <template slot-scope="scope">
            <el-tag :type="['danger', 'success', 'danger'][scope.row.State]">{{
              $fd('FeedBackStatus', scope.row.State)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('feedBack:form:uploadTime')" prop="UpdateTime" min-width="180" show-overflow-tooltip
          sortable="custom" v-if="level > 7" />
        <el-table-column :label="$t('common:action:action')" fixed="right" prop="UserTypeShortName" width="100"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="getDetail(scope.row)">
              {{ $t('common:button:view') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="text-align: right; margin-top: 5px">
        <pagination :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize"
          @pagination="getList" />
      </div>
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { batchUpdateFeedBackState, getUserFeedBackList } from '@/api/trials.js'
const searchDataDefault = () => {
  return {
    TrialKeyInfo: null,
    SubejctAndVisitKeyInfo: null,
    UserTypeEnum: null,
    FeedBackUserKeyInfo: null,
    QuestionDescription: null,
    QuestionType: null,
    State: null,
    TrialSiteCode: null,
    BeginCreatime: null,
    EndCreatime: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: 'CreateTime',
  }
}
export default {
  components: { Pagination },
  props: {
    trialId: {
      type: String,
      default: '',
    },
  },
  dicts: [
    'NoticeApplicableTrial',
    'NoteLevel',
    'NoteType',
    'NoticeState',
    'NoticeMode',
  ],
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      datetimerange: [],
      // 查询参数
      searchData: searchDataDefault(),
      tableSelectData: [],
      UserTypeOptins: [
        { label: 'CRC', value: 2 },
        { label: 'IR', value: 13 },
      ],
    }
  },
  computed: {
    level() {
      if (this.hasPermi(['role:dev', 'role:admin'])) {
        return 9
      }
      if (this.hasPermi(['role:pm', 'role:apm'])) {
        return 8
      }
      if (this.hasPermi(['role:ir', 'role:crc'])) {
        return 7
      }
      return 0
    },
    QuestionTypeOptions() {
      if (this.level > 7) {
        return [
          ...this.$d.FeedBackTypeToCRC.filter((item) => item.value > 0),
          ...this.$d.FeedBackTypeToIR,
        ]
      }
      if (this.hasPermi(['role:ir'])) {
        return this.$d.FeedBackTypeToIR
      }
      if (this.hasPermi(['role:crc'])) {
        return this.$d.FeedBackTypeToCRC
      }
      return []
    },
  },
  created() {
    this.getList()
  },
  methods: {
    // 通过提交状态决定该行的 CheckBox 是否可以勾选
    handleSelectTable(row) {
      return row.State < 1
    },
    async getList() {
      try {
        if (this.trialId) {
          this.searchData.TrialId = this.trialId
        }
        this.loading = true
        let res = await getUserFeedBackList(this.searchData)
        this.loading = false
        if (res.IsSuccess) {
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    handleDatetimeChange(val) {
      if (val) {
        this.searchData.BeginCreatime = val[0]
        this.searchData.EndCreatime = val[1]
      } else {
        this.searchData.BeginCreatime = ''
        this.searchData.EndCreatime = ''
      }
    },
    // 重置列表查询
    handleReset() {
      this.datetimerange = null
      this.handleDatetimeChange()
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 设为解决
    async resolve() {
      try {
        let confirm = await this.$confirm(this.$t('feedBack:resolve:tip'), {
          type: 'warning',
          distinguishCancelAndClose: true,
        })
        if (!confirm) return
        let data = {
          IdList: this.tableSelectData.map((item) => item.Id),
          state: 1,
        }
        let res = await batchUpdateFeedBackState(data)
        if (res.IsSuccess) {
          this.$message.success(this.$t('feedBack:resolve:success'))
          this.getList()
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 导出
    report() { },
    // 表格选择
    handleSelectChange(selection) {
      // console.log(selection, "handleSelectChange");
      this.tableSelectData = selection
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 获取详情
    getDetail(row) {
      this.$FB({
        type: 'detail',
        Id: row.Id,
        callBack: () => {
          this.getList()
          this.$emit('success')
        },
      })
    },
  },
}
</script>
<style lang="scss">
.role {
  height: 100%;
  box-sizing: border-box;
  display: flex;
  padding: 10px;
  border-radius: 5px;

  .left {
    display: flex;
    flex-direction: column;
    width: 0;
    flex-grow: 4;

    // border-right: 1px solid #ccc;
    .filter-container {
      display: flex;
      align-items: center;
      margin: 5px;
    }

    .data-table {
      flex: 1;
      padding: 5px 0px;
    }

    .pagination-container {
      text-align: right;
    }
  }

  .right {
    width: 0;
    flex-grow: 6;
    overflow-y: auto;
    border-right: 1px solid #ccc;
  }

  .selected-row {
    background-color: cadetblue;
  }
}
</style>
