<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      :class="showSearch ? '' : ' hide'"
      label-width="68px"
    >
      <el-form-item :label="resTypeName" prop="resourceName">
        <el-input
          v-model="queryParams.resourceName"
          :placeholder="`请输入${resTypeName}`"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入申请者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="auditBy">
        <el-input
          v-model="queryParams.auditBy"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker
          v-model="daterangeAuditTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="success"
          @click="refesh"
          :loading="submitting"
          v-if="queryParams.resourceType !== 'literature'"
          >刷新缓存</el-button
        >
      </el-form-item>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-form>

    <el-table v-loading="loading" :data="resourceList">
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column :label="resTypeName" prop="resourceName" />
      <el-table-column
        label="发布机构"
        prop="provideOrganization"
        v-if="queryParams.resourceType === 'data'"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_provide_organization"
            :value="scope.row.provideOrganization"
          />
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="createBy" />
      <el-table-column label="申请应用" prop="applicationName" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_audit_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditBy" />
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="'el-icon-edit'"
            @click="handleAudit(scope.row, true)"
            v-hasPermi="['system/audit:resource:audit']"
          >
            <span>审核</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核用户订阅资源对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
      :close-on-click-modal="false"
    >
      <!-- 数据资源 -->
      <el-form
        :model="form"
        label-width="100px"
        class="detail"
        v-if="form.resourceType === 'data' && resData"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="数据名称:">
              {{ resData.dataName }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="表名:">
              {{ resData.tableName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="信息标识符:">
              {{ resData.infoId }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布机构:">
              <dict-tag
                :options="dict.type.sys_provide_organization"
                :value="resData.provideOrganization"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标签:">
              <div v-if="resData.tag">
                <span v-for="(tag, i) in resData.tag" :key="i">{{ tag }}</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源描述:">
              {{ resData.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 服务资源 -->
      <el-form
        :model="form"
        label-width="100px"
        class="detail"
        v-if="form.resourceType === 'service' && resService"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="服务名称:">
              {{ resService.serviceName }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="需要鉴权:">
              {{ resService.needAuth === "Y" ? "需要" : "" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="鉴权方式:">
              <dict-tag
                :options="dict.type.sys_auth_way"
                :value="resService.authWay"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="服务地址:">
              {{ resService.serviceUrl }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="请求方法:">
              {{ resService.requestMethod }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="参数形式:">
              {{ resService.paramForm }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="返回格式:">
              <dict-tag
                :options="dict.type.sys_response_form"
                :value="resService.responseForm"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="信息标识符:">
              {{ resService.infoId }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布机构:">
              <dict-tag
                :options="dict.type.sys_provide_organization"
                :value="resService.provideOrganization"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标签:">
              <div v-if="resService.tag">
                <span v-for="(tag, i) in resService.tag" :key="i">{{
                  tag
                }}</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="日限额:">
              {{
                resService.dayLimit === 0
                  ? "不限制"
                  : "每日 " + resService.dayLimit + " 次"
              }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源描述:">
              {{ resService.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 文献 -->
      <el-form
        :model="form"
        label-width="100px"
        class="detail"
        v-if="form.resourceType === 'literature' && resLiterature"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="文献名称:">
              {{ resLiterature.literatureName }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布机构:">
              <dict-tag
                :options="dict.type.sys_provide_organization"
                :value="resLiterature.publishOrganization"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公开年份:">
              <dict-tag
                :options="dict.type.sys_public_year"
                :value="resLiterature.publicYear"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="主题:">
              <dict-tag
                :options="dict.type.sys_theme"
                :value="resLiterature.theme"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标签:">
              <div v-if="resLiterature.tag">
                <span v-for="(tag, i) in resLiterature.tag" :key="i">{{
                  tag
                }}</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider></el-divider>

      <!-- 申请人信息 -->
      <el-form :model="form" label-width="100px" class="detail">
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人:">
              <span style="margin-right: 10px">{{ form.applyName }}</span>
              <el-tag size="mini" type="primary" v-if="form.userType === '00'"
                >内部用户</el-tag
              >
              <el-tag size="mini" type="success" v-if="form.userType === '01'"
                >外部企业</el-tag
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间:">
              {{ parseTime(form.createTime, "{y}-{m}-{d} {h}:{i}") }}
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.resourceType !== 'literature'">
            <el-form-item label="申请应用:">
              {{ form.applicationName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请原因:">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider v-if="form.resourceType !== 'data'"></el-divider>
        <el-row v-if="form.resourceType !== 'data'">
          <el-col :span="24">
            <el-form-item label="数据过滤:" prop="statusRemark">
              <el-input
                v-model="form.statusRemark"
                placeholder="请输入请求接口search固定过滤JSON"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row v-if="!isAudit">
          <el-col :span="8">
            <el-form-item label="审核结果:" prop="status">
              <dict-tag
                :options="dict.type.sys_audit_status"
                :value="form.status"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="审核人:" prop="auditBy">
              {{ form.auditBy }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="原因说明:" prop="auditRemark">
              {{ form.auditRemark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        v-if="isAudit"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="审核结果:" prop="status">
              <el-radio v-model="form.status" label="1">通过</el-radio>
              <el-radio v-model="form.status" label="2">驳回</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="原因说明:" prop="auditRemark">
              <el-input
                v-model="form.auditRemark"
                placeholder="请输入原因说明"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 服务参数审核 -->
      <el-tabs
        type="card"
        v-model="activeName"
        :style="!isAudit ? 'margin-top: 20px;' : ''"
        v-if="form.resourceType === 'service'"
      >
        <el-tab-pane label="参数审核" name="1">
          <el-table
            v-loading="detailLoading"
            :data="resourceItemList"
            ref="multipleTable"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55"
              align="center"
              :selectable="canSelect"
              v-if="isAudit"
            >
            </el-table-column>
            <el-table-column
              label="编号"
              type="index"
              align="center"
            ></el-table-column>
            <el-table-column
              label="参数类型"
              prop="paramType"
              width="150"
              align="center"
            >
              <template>出参</template>
            </el-table-column>
            <el-table-column label="参数名" prop="paramName"></el-table-column>
            <el-table-column label="参数描述" prop="remark"></el-table-column>
            <el-table-column
              label="数据类型"
              prop="dataType"
              align="center"
              width="150"
            ></el-table-column>
            <el-table-column
              label="脱敏"
              prop="desensitizationRuleId"
              align="center"
              width="200"
            >
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.desensitizationRuleId"
                  placeholder="请选择规则"
                  clearable
                  size="mini"
                  :disabled="
                    scope.row.desensitizationReadOnly ||
                    !isAudit ||
                    scope.row.isVisable !== 'Y'
                  "
                  filterable
                >
                  <el-option
                    :label="rule.ruleName"
                    :value="rule.id"
                    v-for="(rule, i) in desensitizationRuleList"
                    :key="i"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <!-- 数据字段审核 -->
      <el-tabs
        type="card"
        v-model="activeName"
        :style="!isAudit ? 'margin-top: 20px;' : ''"
        v-if="form.resourceType === 'data'"
      >
        <el-tab-pane label="数据项审核" name="1">
          <el-table
            v-loading="detailLoading"
            :data="resourceItemList"
            ref="multipleTable"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55"
              align="center"
              :selectable="canSelect"
              v-if="isAudit"
            >
            </el-table-column>
            <el-table-column
              label="编号"
              type="index"
              align="center"
            ></el-table-column>
            <el-table-column
              label="参数类型"
              prop="paramType"
              width="100"
              align="center"
            >
              <template>出参</template>
            </el-table-column>
            <el-table-column
              label="英文名称"
              prop="columnName"
            ></el-table-column>
            <el-table-column
              label="描述"
              prop="columnComment"
            ></el-table-column>
            <el-table-column
              label="数据类型"
              prop="columnType"
              align="center"
              width="150"
            ></el-table-column>
            <el-table-column
              label="是否可搜索"
              prop="isSearchable"
              width="100"
              align="center"
            >
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.isSearchable"
                  active-value="Y"
                  inactive-value="N"
                  size="mini"
                  :disabled="scope.row.isSearchableReadOnly"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column
              label="脱敏"
              prop="desensitizationRuleId"
              align="center"
              width="200"
            >
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.desensitizationRuleId"
                  placeholder="请选择规则"
                  clearable
                  size="mini"
                  :disabled="
                    scope.row.desensitizationReadOnly ||
                    !isAudit ||
                    scope.row.isVisable !== 'Y'
                  "
                  filterable
                >
                  <el-option
                    :label="rule.ruleName"
                    :value="rule.id"
                    v-for="(rule, i) in desensitizationRuleList"
                    :key="i"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column
              label="过滤条件"
              prop="filterOperator"
              align="center"
              width="150"
            >
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.filterOperator"
                  placeholder="请选择"
                  clearable
                  size="mini"
                  :disabled="!isAudit || scope.row.isVisable !== 'Y'"
                >
                  <el-option label="等于" :value="0"></el-option>
                  <el-option label="大于" :value="1"></el-option>
                  <el-option label="小于" :value="2"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column
              label="过滤值"
              prop="filterOperator"
              align="center"
              width="150"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.filterValue"
                  placeholder="请输入"
                  :disabled="!isAudit || scope.row.isVisable !== 'Y'"
                ></el-input>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="isAudit"
          :loading="submitting"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listResource,
  getUserItem,
  delResource,
  auditResource,
  refeshResource,
} from "@/api/system/customer/userResource";
import { getDataItem, getData } from "@/api/system/share/data";
import { getServiceItem, getService } from "@/api/system/share/service";
import { getLiterature } from "@/api/system/share/literature";
import { listDesensitization } from "@/api/system/quality/desensitization";
import { parseQueryParam } from "@/utils/dxp";
import { getUser } from "@/api/system/user";
export default {
  name: "Resource",
  dicts: [
    "sys_audit_status",
    "sys_provide_organization",
    "sys_auth_way",
    "sys_response_form",
    "sys_theme",
    "sys_public_year",
  ],
  data() {
        const validateJSON = (rule, value, callback) => {
      if (value) {
        try {
          JSON.parse(value);
          callback();
        } catch(e) {
          callback("请输入正确的JSON");
        }
      }
      callback();
    };
    return {
      resName: null,
      // 遮罩层
      loading: true,
      submitting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户订阅资源表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核意见时间范围
      daterangeCreateTime: [],
      // 审核意见时间范围
      daterangeAuditTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        resourceType: null,
        resourceName: null,
        status: "0",
        createBy: null,
        createTime: null,
        auditBy: null,
        auditTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
        auditRemark: [
          { required: true, message: "请填写原因说明", trigger: "change" },
        ],
        statusRemark: [
          { validator: validateJSON, trigger: "change" }
        ]
      },
      isAudit: false,
      resourceItemList: [], // 资源明细列表
      resData: null, // 订阅数据
      resService: null, // 订阅服务
      resLiterature: null, // 订阅的文献
      detailLoading: false,
      desensitizationRuleList: [],
      activeName: "1",
      selectedItems: [], // 选择的参数/字段
    };
  },
  created() {
    this.getDesensitizationRuleList();
    this.initResType();
    this.getList();
  },
  methods: {
    refesh() {
      this.submitting = true;
      refeshResource()
        .then((res) => {
          this.submitting = false;
          this.$modal.msgSuccess("刷新成功");
        })
        .catch(() => {
          this.submitting = false;
        });
    },
    canSelect(row) {
      return row.isVisable === "Y";
    },
    getDesensitizationRuleList() {
      // 获取脱敏规则列表
      listDesensitization({ pageNum: 1, pageSize: 10000 }).then((response) => {
        this.desensitizationRuleList = response.rows;
      });
    },
    initResType() {
      const params = parseQueryParam(window.location.search);
      if (params.resType) {
        this.queryParams.resourceType = params.resType;
      } else {
        this.queryParams.resourceType = "data";
      }

      if (params.resType === "service") {
        this.resTypeName = "服务名称";
      } else if (params.resType === "data") {
        this.resTypeName = "数据名称";
      } else {
        this.resTypeName = "文献名称";
      }
    },
    /** 查询用户订阅资源列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0] + " 00:00:00";
        this.queryParams.params["endCreateTime"] =
          this.daterangeCreateTime[1] + " 23:59:59";
      }
      if (null != this.daterangeAuditTime && "" != this.daterangeAuditTime) {
        this.queryParams.params["beginAuditTime"] =
          this.daterangeAuditTime[0] + " 00:00:00";
        this.queryParams.params["endAuditTime"] =
          this.daterangeAuditTime[1] + " 23:59:59";
      }
      listResource(this.queryParams).then((response) => {
        this.resourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        auditRemark: null,
      };
      this.resourceItemList = [];
      this.resData = null;
      this.resService = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeAuditTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getUserItem2(row) {
      getUserItem(row.id).then((resUserItem) => {
        if (resUserItem.data && resUserItem.data.resourceItem) {
          const selectedItems = JSON.parse(resUserItem.data.resourceItem);
          console.log(selectedItems);
          console.log(this.resourceItemList);

          // 设置选中
          if (
            this.resourceItemList &&
            this.resourceItemList.length > 0 &&
            selectedItems &&
            selectedItems.length > 0
          ) {
            this.resourceItemList.forEach((p) => {
              // console.log(p);
              const selected = selectedItems.find((k) => k.id === p.id);
              if (selected) {
                this.$refs.multipleTable.toggleRowSelection(p);
                p.desensitizationRuleId = selected.desensitizationRuleId;
                p.filterOperator = selected.filterOperator;
                p.filterValue = selected.filterValue;
              }
            });
          }
          // this.$refs.multipleTable.toggleRowSelection(row);
        }
      });
    },
    getResourceItem(row) {
      if (row.resourceType === "data") {
        getDataItem(row.resourceId, 0).then((response) => {
          if (response.data && response.data.length > 0) {
            response.data.forEach((p) => {
              if (p.desensitizationRuleId > 0) {
                p.desensitizationReadOnly = true;

                if (p.isSearchable !== "Y") {
                  p.isSearchableReadOnly = true;
                }
              }
            });
          }
          this.resourceItemList = response.data;
          this.getUserItem2(row);
          this.open = true;
          this.detailLoading = false;
          // console.log(this.resData);
          // console.log(this.resourceItemList);
        });
      } else if (row.resourceType === "service") {
        getServiceItem(row.resourceId, 0).then((response) => {
          this.detailLoading = false;
          if (response.data && response.data.length > 0) {
            response.data.forEach((p) => {
              if (p.desensitizationRuleId > 0) {
                p.desensitizationReadOnly = true;
              }
            });
          }
          this.resourceItemList = response.data.filter(
            (p) => p.paramType === "out"
          ); // 只配置出参
          this.getUserItem2(row);
          this.open = true;
          this.detailLoading = false;
          // console.log(this.resService);
          // console.log(this.resourceItemList);
        });
      }
    },
    getResourceDetail(row) {
      console.log(row);
      // 获取共享明细
      if (row.resourceType === "data") {
        getData(row.resourceId).then((resData) => {
          this.resData = resData.data;
        });
      } else if (row.resourceType === "service") {
        getService(row.resourceId).then((resService) => {
          this.resService = resService.data;
          if (this.resService) {
            this.resService.dayLimit = row.dayLimit;
          }
        });
      } else if (row.resourceType === "literature") {
        getLiterature(row.resourceId).then((resService) => {
          this.resLiterature = resService.data;
        });
      }
    },
    handleSelectionChange(val) {
      // console.log(val);
      this.selectedItems = val;
    },
    handleAudit(row, isAudit) {
      // console.log(row);
      getUser(row.userId).then((res) => {
        this.detailLoading = true;
        this.reset();
        this.form = JSON.parse(JSON.stringify(row));
        this.form.userType = res.data.userType;
        this.form.applyName = res.data.nickName;
        this.isAudit = isAudit;
        if (this.isAudit) {
          this.form.auditRemark = "";
        }
        this.title = "审核用户订阅资源";

        if (this.form.resourceType === "literature") {
          this.getResourceDetail(row);
          this.open = true;
        } else {
          this.getResourceDetail(row);
          this.getResourceItem(row);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(this.form));
          if (this.form.resourceType !== "literature") {
            if (
              (this.form.responseForm =
                "standard" &&
                this.form.status === "1" &&
                (!this.selectedItems || this.selectedItems.length === 0))
            ) {
              this.$modal.msgError("至少勾选一个参数");
              return;
            }

            this.selectedItems.sort(function (a, b) {
              return a.sort - b.sort;
            });

            form.resourceItem = JSON.stringify(this.selectedItems);
          }

          this.submitting = true;
          auditResource(form)
            .then((response) => {
              this.submitting = false;
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            })
            .catch(() => {
              this.submitting = false;
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户订阅资源编号为"' + ids + '"的数据项？')
        .then(function () {
          return delResource(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
