<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="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          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>
      <br />
      <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-form-item>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-form>
    <!-- 
    <el-row :gutter="10" class="mb8"> </el-row> -->

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column label="服务名称" prop="serviceName" />
      <el-table-column label="发布机构" prop="provideOrganization">
        <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="publishType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_publish_type"
            :value="scope.row.publishType"
          />
        </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="createBy" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </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} {h}:{i}:{s}")
          }}</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/share:data:edit']"
            v-if="scope.row.status === '0'"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleAudit(scope.row, false)"
            v-hasPermi="['system/share:data:edit']"
            v-if="scope.row.status !== '0'"
            >查看</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="1000px"
      append-to-body
    >
      <el-form :model="form" label-width="100px" class="detail">
        <el-row>
          <el-col :span="12">
            <el-form-item label="服务名称:" prop="serviceName">
              {{ form.serviceName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开放类型:" prop="publishType">
              <dict-tag
                :options="dict.type.sys_publish_type"
                :value="form.publishType"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="服务地址:" prop="serviceUrl">
              {{ form.serviceUrl }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否鉴权:" prop="needAuth">
              {{ form.needAuth === "Y" ? "需要" : "" }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鉴权方式:" prop="authWay">
              <dict-tag
                :options="dict.type.sys_auth_way"
                :value="form.authWay"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请求方法:" prop="requestMethod">
              {{ form.requestMethod }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参数形式:" prop="paramForm">
              {{ form.paramForm }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="信息标识符:" prop="infoId">
              {{ form.infoId }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布机构:" prop="provideOrganization">
              <dict-tag
                :options="dict.type.sys_provide_organization"
                :value="form.provideOrganization"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标签:" prop="tag">
              {{ form.tag }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="描述:" prop="remark">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人:" prop="tag">
              {{ form.createBy }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间:" prop="remark">
              {{ parseTime(form.createTime, "{y}-{m}-{d}") }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row v-if="!isAudit">
          <el-col :span="24">
            <el-form-item label="申请结果:" prop="status">
              <dict-tag
                :options="dict.type.sys_audit_status"
                :value="form.status"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!isAudit">
          <el-col :span="24">
            <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="24">
            <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-row>
        <el-row>
          <el-col :span="24">
            <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>
      <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 {
  listService,
  getService,
  delService,
  auditService,
} from "@/api/system/share/service";

export default {
  name: "Data",
  dicts: [
    "sys_publish_type",
    "sys_audit_status",
    "sys_provide_organization",
    "sys_auth_way",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      submitting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 共享数据表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 开放类型：关联数据字典时间范围
      daterangeCreateTime: [],
      // 开放类型：关联数据字典时间范围
      daterangeAuditTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: null,
        dataName: null,
        status: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        auditBy: null,
        auditTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
        auditRemark: [
          { required: true, message: "请填写原因说明", trigger: "change" },
        ],
      },
      isAudit: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询共享数据列表 */
    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';
      }
      listService(this.queryParams).then((response) => {
        this.dataList = 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.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeAuditTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 审核按钮操作 */
    handleAudit(row, isAudit) {
      this.reset();
      const id = row.id || this.ids;
      this.isAudit = isAudit;
      getService(id).then((response) => {
        if (response.status === "0") {
          response.data.status = null;
          response.data.auditRemark = null;
        }

        this.form = response.data;
        if (this.isAudit) {
          this.form.auditRemark = "";
        }
        this.open = true;
        this.title = "审核共享服务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // console.log(this.form);
          this.submitting = true;
          auditService(this.form).then((response) => {
            this.submitting = false;
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除共享服务编号为"' + ids + '"的数据项？')
        .then(function () {
          return delService(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
