<template>
  <div id="">
    <a-button
      @click="addProblem"
      type="primary"
      style="margin-bottom: 10px;position: relative;z-index: 1;"
      v-if="openType">添加问题
    </a-button>
    <a-button
      @click="batchSubmit"
      type="primary"
      style="margin-bottom: 10px;margin-left: 10px;margin-right: 10px;position: relative;z-index: 1;"
      v-if="openType && rowData.chkType=='1'">批量提交</a-button>
    <a-button
      @click="batchSubmitNo"
      type="primary"
      style="margin-bottom: 10px;position: relative;z-index: 1;"
      v-if="openType && rowData.chkType=='1'">暂无问题</a-button>
    <a-table
      :loading="loading"
      :columns="columns"
      bordered
      ref="multipleTable"
      :data-source="problemList"
      row-key="pblmId"
      :pagination="false"
      :style="{ height: tableHeight + 'px' }"
      :scroll="{ y: tableHeight - 60 }"
      style="width: 100%"
      :row-selection="{
        columnWidth: 50,
        selectedRowKeys: selectedRowKeys,
        onChange: handleSelection,
        getCheckboxProps: record => ({
          props: {
            disabled: record.workflowStat && record.workflowStat !== '10',
            name: record.workflowStat
          }
        })
      }">
      <span slot="index" slot-scope="text, recode, index">
        {{ (currentPage - 1) * pageSize + (index + 1) }}
      </span>
      <span slot="collTime" slot-scope="text">
        {{ timestampToTime(text) }}
      </span>
      <span slot="action" slot-scope="text">
        <!-- 通用督查页面需要 立行立改 的 改 -->
        <template v-if="isTongYong == '1'">
          <a-button v-if="text.offlineCancellation == '1'" @click="makeChange1(text)" type="link" title="取消立行立改" style="font-weight: bold;color: red;padding: 0 8px;">改</a-button>
          <a-button v-else @click="makeChange(text)" type="link" title="设置立行立改" style="font-weight: bold;padding: 0 8px;">改</a-button>
        </template>
        <a-button icon="eye" title="查看详情" type="link" @click="showDetails(text.pblmId)" />
        <a-button icon="edit" type="link" title="修改问题" @click="changeSup(text.pblmId)" v-if="openType && !(text.workflowStat && text.workflowStat != '10')" />
        <a-button icon="delete" type="link" v-if="openType && !(text.workflowStat && text.workflowStat!='10')" title="删除问题" @click="removeSuperviseOne(text.pblmId)" />
      </span>
    </a-table>
    <a-pagination
      show-size-changer
      :pageSize.sync="pageSize"
      :total="total"
      :page-size-options="['10', '20', '50', '100']"
      :show-total="total => `共${total}条`"
      @showSizeChange="handleSizeChange"
      @change="search"
      style="margin-top: 10px;" />
    <!-- 详情组件-->
    <a-modal
      :open="supervisionDetailsVisible"
      @cancel="supervisionDetailsVisible = false"
      class="custom_dialog"
      title="问题详情"
      width="60%">
      <supervision-details ref="xianqing" :problemId="currentProblemId" :objType="pType"></supervision-details>
      <span slot="footer">
        <a-button style="margin-top:5px;" @click="supervisionDetailsVisible = false">返回</a-button>
      </span>
    </a-modal>
    <!-- 删除组件 -->
    <a-modal
      :open="problemDeleteVisible"
      @cancel="problemDeleteVisible = false"
      class="deleteDialog"
      title="问题删除描述"
      width="60%">
      <Problem-delete ref="deleteMiaoShu" :deleteProblemId="deleteId" @deleteSuccess="deleteSuccess"></Problem-delete>
      <span slot="footer">
        <a-button @click="problemDeleteVisible = false">返回</a-button>
      </span>
    </a-modal>
    <!-- 修改组件 -->
    <problem-edit :editId="editId" @closeEditDialog="closeEditDialog" v-if="changeSupervisionVisible" :pType="rowData.type">
    </problem-edit>
    <!-- 添加问题组件 -->
    <problem-dialog
      v-if="showAddProblemDialog"
      :showAddProblemDialog="showAddProblemDialog"
      :objType="111"
      :rowData="rowData"
      :villType="111"
      :objId="currentObjectId"
      :regId="currentObjectRgstrId"
      :objName="currentObjectName"
      :problemMiddleType="'通用督查'"
      @closeDialog="closePlDialog"
      :newModelData="newModelData"
      :currentObjectRgstrId="currentObjectRgstrId"
      :flag="flag">
    </problem-dialog>
    <a-modal
      :open="pblmEascaderVisible"
      class="pblmEascader"
      title="新增问题"
      width="60%"
      @cancel="pblmEascaderVisible = false">
      <pblmEascader :objType="rowData.type" @modelData="modelData" v-if="pblmEascaderVisible"></pblmEascader>
      <span slot="footer">
        <a-button @click="pblmEascaderVisible = false">返 回</a-button>
        <a-button type="primary" @click="submitEascader">确 定</a-button>
      </span>
    </a-modal>
    <a-modal :open="makeChangeVisible" @cancel="makeChangeVisible=false" title="立行立改描述" width="40%">
      <a-form :label-col="{span: 6}" :wrapper-col="{span: 18}" class="demo-form-inline" style="padding-left: 10px;">
        <a-form-item label="整改描述" required>
          <a-input v-model:value="auditidea" placeholder="整改描述" />
        </a-form-item>
        <a-form-item label="整改附件" required>
          <div class="upload_div">
            <a-upload-dragger
              multiple
              :file-list="fileList"
              :remove="handleRemove"
              :before-upload="beforeUpload"
              ref="upload"
              class="upload-demo">
              <p class="ant-upload-drag-icon">
                <a-icon type="inbox" />
              </p>
              <p class="ant-upload-text">
                将文件拖到此处，或<em>点击上传</em>
              </p>
            </a-upload-dragger>
          </div>
        </a-form-item>
      </a-form>
      <span slot="footer">
        <a-button @click="hasArgee" style="margin-top:5px;" type="primary">确定</a-button>
        <a-button style="margin-top:5px;" @click="cancelArgee">取消</a-button>
      </span>
    </a-modal>
  </div>
</template>

<script>
import request from '../../utils/gw_ajax_request.js';
import { formatDateTimeD } from '../../utils/gw_date';
import problemDialog from '../duChaWenTi/addProblem/xiaoshuiku_problem_dialog.vue';
import problemEdit from '../duChaWenTi/commonProblemModify.vue';
import ProblemDelete from '../duChaWenTi/problemDelete.vue';
import supervisionDetails from '../duChaWenTi/supervisionDetails_common.vue';
import pblmEascader from './project/pblmEascader.vue';
export default {
  props: [
    'currentObjectId',
    'currentObjectName',
    'currentObjectRgstrId',
    'activeLabel',
    'pType',
    'openType',
    'rowData',
    'chkType',
    'flag',
    'isTongYong',
  ],
  data() {
    return {
      tableHeight: 560,
      problemList: [],
      pblmList: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      pageNum: 1,
      total: 0,
      supervisionDetailsVisible: false,
      currentProblemId: '',
      problemDeleteVisible: false,
      deleteId: '',
      editId: '',
      changeSupervisionVisible: false,
      showAddProblemDialog: false,
      rsvrRules: null,
      probId: '',
      pblmEascaderVisible: false,
      newModelData: null,
      selectedRowKeys: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 40,
          ellipsis: true,
        },
        {
          title: '督查问题类别',
          dataIndex: 'inspPblmName',
          align: 'center',
          width: 140,
          ellipsis: true,
        },
        {
          title: '检查项目',
          dataIndex: 'checkPoint',
          align: 'center',
          width: 100,
          ellipsis: true,
        },
        {
          title: '详细描述',
          dataIndex: 'inspPblmDesc',
          align: 'center',
          width: 100,
          ellipsis: true,
        },
        {
          title: '上报时间',
          dataIndex: 'collTime',
          align: 'center',
          width: 120,
          ellipsis: true,
          sorter: (a, b) => b.collTime - a.collTime,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 100,
        },
      ],
      makeChangeVisible: false,
      auditidea: '',
      fileList: '',
      bizId: '',
      actionUrl: '',
      header: {
        persId: localStorage.getItem('persId'),
        accessToken: localStorage.getItem('accessToken'),
        orgId: localStorage.getItem('orgId'),
      },
    };
  },
  components: {
    // 详情页
    supervisionDetails,
    // 删除描述页
    ProblemDelete,
    // 修改问题
    problemEdit,
    problemDialog,
    pblmEascader,
  },
  computed: {
    persId() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '1098101939614724096';
    },
  },
  created() {
    // this.findItemByType()
    console.log(this.isTongYong, 'isTongYong');
  },
  mounted() {
    this.getProblemList();
  },
  methods: {
    getProblemList() {
      const problemForm = {};
      problemForm.objId = this.currentObjectId;
      problemForm.objType = '111';
      problemForm.pageNum = this.currentPage;
      problemForm.pageSize = this.pageSize;
      problemForm.presId = this.persId;
      this.loading = true;
      request
        .post(`/dc/insp/pblm/list/${this.persId}`, problemForm)
        .then((res) => {
          if (res.success) {
            this.problemList = res.data.list;
            this.total = res.data.total;
            this.loading = false;
          }
        })
        .catch((err) => {
          this.$message.error(err);
          this.loading = false;
        });
    },
    showDetails(pblmId) {
      this.currentProblemId = pblmId;
      this.supervisionDetailsVisible = true;
      this.$nextTick(() => {
        this.$refs.xianqing.getFirstData();
      });
    },
    changeSup(pblmId) {
      this.editId = pblmId;
      this.changeSupervisionVisible = true;
    },
    removeSuperviseOne(pblmId) {
      this.deleteId = pblmId;
      this.problemDeleteVisible = true;
    },
    deleteSuccess() {
      this.problemDeleteVisible = false;
      this.getProblemList();
    },
    search(val) {
      this.currentPage = val;
      this.getProblemList();
    },
    handleSizeChange() {},
    timestampToTime(row, column) {
      var date = new Date(row.collTime);
      var Y = date.getFullYear() + '-';
      var M =
        (date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1) + '-';
      var D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
      var h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
      var m =
        date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
      return Y + M + D + h + m;
    },
    closeEditDialog() {
      this.changeSupervisionVisible = false;
      this.getProblemList();
    },
    handleSelection(val, row) {
      this.selectedRowKeys = val;
      this.pblmList = row;
    },
    clearSelection() {
      this.selectedRowKeys = [];
    },
    batchSubmitNo() {
      var obj = {
        id: this.currentObjectRgstrId,
        objId: this.currentObjectId,
        state: '2',
      };
      this.$confirm({
        title: '提示',
        content: '确定要修改为暂无问题吗, 是否继续?',
        okType: 'warning',
        onOk: () => {
          request.post(`/bis/insp/gnrl/`, obj).then((res) => {
            if (res.success) {
              this.$message.success('修改成功');
              this.$emit('cancelHandler');
            }
          });
        },
      });
    },
    batchSubmit() {
      if (this.pblmList.length > 0) {
        this.$confirm({
          title: '提示',
          content: '确定要提交选中的问题吗, 是否继续?',
          okType: 'warning',
          onOk: () => {
            request
              .post(`/dc/insp/pblm/submit/updateState`, this.pblmList)
              .then((res) => {
                if (res.success) {
                  this.$message.success('提交成功!');
                  this.getProblemList();
                  this.clearSelection();
                } else {
                  this.$message.error('提交失败,请联系管理员!');
                }
              });
          },
        });
      } else {
        this.$message.warning('请选择数据');
      }
    },
    addProblem(item) {
      this.pblmEascaderVisible = true;
    },
    closePlDialog(val) {
      this.showAddProblemDialog = false;
      this.getProblemList();
      this.newModelData = null;
    },
    submitEascader() {
      if (!this.newModelData || !this.newModelData.model.length) {
        this.$message.warning('问题不可以为空');
        return;
      }
      this.pblmEascaderVisible = false;
      this.showAddProblemDialog = true;
    },
    modelData(val) {
      this.newModelData = val;
    },
    // 设为立行立改
    makeChange(text) {
      this.auditidea = '';
      this.fileList = [];
      this.bizId = text.pblmId;
      if (process.env.NODE_ENV === 'development') {
        this.actionUrl = `/pdcApi/file/insert?bizId=${this.bizId}`;
      } else {
        this.actionUrl = `/modular-source/6ace62b8e2bb467d98286d260aebcef2/pdcApi/file/insert?bizId=${this.bizId}`;
      }
      this.makeChangeVisible = true;
    },
    hasArgee() {
      if (!this.auditidea) {
        return this.$message.error('请输入整改描述!');
      }
      if (this.fileList.length < 1) {
        return this.$message.error('请上传附件!');
      }
      console.log(this.fileList);
      const { fileList } = this;
      fileList.forEach((file) => {
        const formData = new FormData();
        formData.append('file', file);
        request
          .post(`/file/insert?bizId=${this.bizId}&bizType=pblm_ylxlg`, formData)
          .then((res) => {
            if (res.success) {
              console.log(res, 'xxxxxx');
            } else {
              this.$message.error(res.message);
            }
          })
          .catch(() => {
            this.$message.error('上传文件失败!');
          });
      });
      const params = {
        offlineCancellation: 1,
        pblmId: this.bizId,
        lxlgxx: this.auditidea,
        // gwComFiles2: this.fileList
      };
      request.post('/dc/insp/pblm/page/update', params).then((res) => {
        this.makeChangeVisible = false;
        this.getProblemList();
      });
    },
    cancelArgee() {
      request
        .get(`/file/delByBiz?bizId=${this.bizId}&bizType=pblm_ylxlg`)
        .then((res) => {
          this.makeChangeVisible = false;
        });
    },
    // 取消立行立改
    makeChange1(row) {
      this.$confirm({
        title: '提示',
        content: '确认取消立行立改？',
        okType: 'warning',
        onOk: () => {
          request
            .get(`/file/delByBiz?bizId=${row.pblmId}&bizType=pblm_ylxlg`)
            .then((res) => {
              const params = {
                offlineCancellation: 0,
                pblmId: row.pblmId,
              };
              request.post('/dc/insp/pblm/page/update', params).then((res) => {
                this.getProblemList();
              });
            });
        },
      });
    },
    handleRemove(file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file];
      return false;
    },
  },
  watch: {
    activeLabel(val) {
      if (val == '问题清单') {
        this.getProblemList();
      }
    },
  },
};
</script>

<style lang='less' scoped>
@import url('../../assets/style/tableScroll.less');
.pblmEascader .el-dialog__body {
  height: 15vh;
  overflow: auto;
}
</style>
