<template>
  <el-dialog :title="$t('申请记录')" :visible.sync="visible" width="1200px" append-to-body>
    <el-table v-loading="loading" :data="approveList" @selection-change="handleSelectionChange" :height="clientHeight" border>
      <el-table-column :label="$t('申请人')" align="center" prop="userName" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('设备类型')+':'" align="center" prop="devType" :formatter="devTypeFormat" />
      <el-table-column :label="$t('审批类型')" align="center" prop="approveType" :formatter="approveTypeFormat" />
      <el-table-column :label="$t('内容')" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('申请状态')" align="center" prop="status"  :formatter="statusFormat"/>
      <el-table-column :label="$t('开始时间')" align="center" prop="creattime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creattime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('结束时间')" align="center" prop="endtime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center"  width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="detailHandle(scope.row)"
          >{{ $t('查看') }}</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"-->
<!--    />-->
    <add-or-update v-if="specDealVisible" ref="specDeal" @refreshDataList="getList"></add-or-update>
  </el-dialog>
</template>

<script>
import { listApprove, getApprove,listHisApprove, delApprove, addApprove, updateApprove, exportApprove, importTemplate} from "@/api/intact/approve_dev";
import { treeselect } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import AddOrUpdate from '../../approve/approveDetail/intact-detail';
import Editor from '@/components/Editor';

export default {
  name: "Approve",
  components: { Treeselect,AddOrUpdate },
  // components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      visible: false,
      specDealVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 特种设备申请表格数据
      approveList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      clientHeight:300,
      // 是否显示弹出层
      open: false,
      // 设备类型：字典
      devTypeOptions: [],
      // 审批类型字典
      approveTypeOptions: [],
      statusOptions: [],
        // 用户导入参数
        upload: {
            // 是否显示弹出层（用户导入）
            open: false,
            // 弹出层标题（用户导入）
            title: "",
            // 是否禁用上传
            isUploading: false,
            // 是否更新已经存在的用户数据
            updateSupport: 0,
            // 设置上传的请求头部
            headers: { Authorization: "Bearer " + getToken() },
            // 上传的地址
            url: process.env.VUE_APP_BASE_API + "/sems/approve/importData"
        },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        userId: null,
        devId: null,
        devType: null,
        approveType: null,
        content: null,
        fileUrls: null,
        reUrls: null,
        status: null,
        creattime: null,
        endtime: null,
        processId: null,
        govDate: null,
        delayDate: null,
        delayReason: null,
        delayMeasure: null,
        delayNotice: null,
        apNo: null,
        checkDate: null,
        reportId: null,
        monthId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: this.$t('申请人')+this.$t('空格') +'id' + this.$t('不能为空'), trigger: "blur" }
        ],
      }
    };
  },
  watch: {
        // 根据名称筛选部门树
        deptName(val) {
            this.$refs.tree.filter(val);
        }
   },
  created() {
      //设置表格高度对应屏幕高度
      this.$nextTick(() => {
          this.clientHeight = document.body.clientHeight -250
      })
    this.getList();
    this.getTreeselect();
    this.getDicts("INTACT_DEVTYPE").then(response => {
      this.devTypeOptions = response.data;
    });
    this.getDicts("INTACT_APPROVE_TYPE").then(response => {
      this.approveTypeOptions = response.data;
    });
    this.getDicts("spec_approve_res").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    init (row, type) {
      this.visible = true
      this.loading = true;
    //  console.log(row.devType)
      this.queryParams.devId = row.id
      this.queryParams.devType = type
      listHisApprove(this.queryParams).then(response => {
        this.approveList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询特种设备申请列表 */
    getList() {

    },
     /** 查询部门下拉树结构 */
     getTreeselect() {
          treeselect().then(response => {
              this.deptOptions = response.data;
          });
     },
    // 设备类型：字典翻译
    devTypeFormat(row, column) {
      return this.selectDictLabel(this.devTypeOptions, row.devType);
    },
    // 审批类型字典翻译
    approveTypeFormat(row, column) {
      return this.selectDictLabel(this.approveTypeOptions, row.approveType);
    },
    // 审批类型字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        devId: null,
        devType: null,
        approveType: null,
        content: null,
        fileUrls: null,
        reUrls: null,
        status: 0,
        creattime: null,
        endtime: null,
        processId: null,
        govDate: null,
        delayDate: null,
        delayReason: null,
        delayMeasure: null,
        delayNotice: null,
        apNo: null,
        checkDate: null,
        reportId: null,
        monthId: null,
        delFlag: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加特种设备申请');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApprove(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title =this.$t('修改特种设备申请');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApprove(this.form).then(response => {
              this.msgSuccess(this.$t('修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addApprove(this.form).then(response => {
              this.msgSuccess(this.$t('新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
          type: "warning"
        }).then(function() {
          return delApprove(ids);
        }).then(() => {
          this.getList();
        this.msgSuccess(this.$t('删除成功'));
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm(this.$t('是否确认导出所有特种设备申请数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
          type: "warning"
        }).then(function() {
          return exportApprove(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
      /** 导入按钮操作 */
      handleImport() {
          this.upload.title = this.$t('用户导入');
          this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
          importTemplate().then(response => {
              this.download(response.msg);
          });
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
          this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
          this.upload.open = false;
          this.upload.isUploading = false;
          this.$refs.upload.clearFiles();
          this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true });
          this.getList();
      },
      // 提交上传文件
      submitFileForm() {
          this.$refs.upload.submit();
      },
    //操作审批流程
    detailHandle (row) {
        this.specDealVisible = true

        this.$nextTick(() => {
          console.log(row.taskName)

          this.$refs.specDeal.init(row.id, row.taskId, row.processId,row.taskName)

        })

    },
  }
};
</script>
