<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    :default-sorter="[
      {direction: 'DESC', property: 'project_code_'},
      {direction: 'DESC', property: 'project_name_'},
    ]"
    :dialogTitle="dialogTitle"
    :dialogVisible="dialogVisible"
    :elAuth="'eia_other_edit'"
    @select-all="handleSelect"
    @select="handleSelect"
    @handleDialogCancel="handleDialogCancel"
    @handleDialogClose="handleDialogClose"
    @handleDialogSubmit="handleDialogSubmit"
    @row-dblclick="handleRowDBClick"
  >
    <template v-slot:searchInput>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        class="search-top"
        @submit.native.prevent
      >
        <!-- <project-info-selector
          v-if="isShow"
          v-model="searchForm.projectId"
          @keyup.enter.native="handleSearch"
        ></project-info-selector> -->
        <el-input
          v-model="searchForm.securityInspectCode"
          placeholder="请输入安检编号"
          @keyup.enter.native="handleSearch"
          clearable
        />
        <!-- <el-date-picker
          v-model="searchForm.establishmentDate"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @keyup.enter.native="handleSearch"
        >
        </el-date-picker> -->
        <el-date-picker
          v-model="searchForm.inspectDate"
          type="date"
          placeholder="选择检查日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          @keyup.enter.native="handleSearch"
        >
        </el-date-picker>
        <el-button
          size="small"
          @click="handleSearch"
          type="primary"
          class="el-icon-search search-btn"
          >查询</el-button
        >
        <el-button
          size="small"
          @click="handleReset"
          type="primary"
          plain
          class="el-icon-refresh"
          >重置</el-button
        >
      </el-form>
    </template>
    <template v-slot:operationBtn>
      <el-button
        size="small"
        type="primary"
        @click="handleAdd"
        icon="el-icon-plus"
        v-methodauth="'eia_other_add'"
        >添加</el-button
      >

      <ht-delete-button
        :url="deleteUrl"
        :htTable="$refs.AtTable"
        :requestMethod="'POST'"
        v-methodauth="'eia_other_del'"
        >删除</ht-delete-button
      >
      <import-file :importData="importData" @load="load" v-methodauth="'eia_other_import'"></import-file>
      <el-button
        size="small"
        type="primary"
        @click="handleExport"
        icon="el-icon-download"
        plain
        style="margin-left: 10px"
        v-methodauth="'eia_other_export'"
        >导出</el-button
      >
    </template>
    <template>
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" width="55" align="center">
      </el-table-column>
      <!-- <el-table-column
        prop="projectInfo.projectName"
        label="项目名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        prop="securityInspectTheme"
        label="主题"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="securityInspectCode"
        label="安检编号"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column prop="inspectDate" label="检查日期" align="center">
      </el-table-column>
      <el-table-column prop="inspectUserName" label="检测人" align="center">
      </el-table-column>
      <!-- <el-table-column
        prop="baseSituation"
        label="基本情况"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="checkSituation"
        label="现场检查情况"
        align="center"
        width="150"
      >
      </el-table-column>
      <el-table-column
        prop="changeContractName"
        label="处理要求及措施"
        align="center"
        width="150"
      >
      </el-table-column> 
      <el-table-column
        prop="imageProgress"
        label="形象进度"
        align="center"
        width="120"
      >
      </el-table-column>-->
      <el-table-column prop="reexaminationUser" label="复查人" align="center">
      </el-table-column>
      <!-- <el-table-column
        prop="reexaminationSituation"
        label="复查情况"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="projectScale"
        label="工程规模"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->

      <el-table-column
        prop="remarks"
        label="备注"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
    </template>
    <!-- 弹框 -->
    <template v-slot:dialogContent>
      <el-form
        ref="ruleForm"
        data-vv-scope="form"
        :model="form"
        :rules="rules"
        label-width="130px"
        v-if="dialogVisible"
        :label-position="labelPosition"
        v-form
      >
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="项目名称" prop="projectInfo.projectName">
              <el-input
                v-model="form.projectInfo.projectName"
                placeholder="请选择项目名称"
                @focus="handleFocusProject"
                :disabled="!isShow"
                clearable
              >
                <el-button
                  type="primary"
                  @click="handleFocusProject"
                  slot="append"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="安检编号" prop="securityInspectCode">
              <el-input
                v-model="form.securityInspectCode"
                placeholder="系统自动生成,无需手动输入"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主题" prop="securityInspectTheme">
              <el-input
                v-model="form.securityInspectTheme"
                placeholder="请输入主题"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="检查日期" prop="inspectDate">
              <el-date-picker
                v-model="form.inspectDate"
                type="date"
                placeholder="请输入检查日期"
                class="basisType"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检查人" prop="inspectUserName">
              <!-- <eip-user-selector
                :appendToBody="true"
                :placeholder="'请选择用户'"
                ref="eipSelector"
                v-model="form.inspectUserName"
                single
                permission="b"
                :config="{
                  id: 'form.inspectUserId',
                  name: 'form.inspectUserName',
                }"
              /> -->

               <!-- 2023年2月9日修改为输入框 -->
               <el-input
                v-model="form.inspectUserName"
                placeholder="请输入检查人姓名"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="形象进度" prop="imageProgress">
              <el-input
                type="number"
                v-model="form.imageProgress"
                placeholder="请输入形象进度"
                clearable
              >
                <i slot="suffix">%</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="复查人" prop="reexaminationUser">
              <el-input
                v-model="form.reexaminationUser"
                placeholder="请输入复查人"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="复查情况" prop="reexaminationSituation">
          <el-input
            type="textarea"
            v-model="form.reexaminationSituation"
            placeholder="请输入复查情况"
          ></el-input>
        </el-form-item>
        <el-form-item label="基本情况" prop="baseSituation">
          <el-input
            type="textarea"
            v-model="form.baseSituation"
            placeholder="请输入基本情况"
          ></el-input>
        </el-form-item>

        <el-form-item label="处理要求及措施" prop="changeContractName">
          <el-input
            type="textarea"
            v-model="form.changeContractName"
            placeholder="请输入处理要求及措施"
          ></el-input>
        </el-form-item>
        <el-form-item label="现场处理情况" prop="checkSituation">
          <el-input
            type="textarea"
            v-model="form.checkSituation"
            placeholder="请输入现场处理情况"
          ></el-input>
        </el-form-item>

        <el-form-item label="工程规模" prop="projectScale">
          <el-input
            type="textarea"
            v-model="form.projectScale"
            placeholder="请输入工程规模"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            type="textarea"
            v-model="form.remarks"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
        <file-table
          @handleFile="accessoryInfo"
          :file="form.accessoryInfo"
          :info="'上传安全巡查记录等文件'"
        />
      </el-form>
    </template>
    <project-info-dialog
      ref="project"
      :single="true"
      @select="handleSelectProject"
    ></project-info-dialog>
  </at-table>
</template>
<script>
const eipUserSelector = () =>
  import('@/components/selector/EipUserSelector.vue')
import ProjectInfoDialog from '@/components/dialog/ProjectInfoDialog.vue'
import req from '@/request.js'
export default {
  name: 'ProjectOutside',
  components: {
    ProjectInfoDialog,
    eipUserSelector,
  },
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      importData: {
        title: '安全管理导入',
        templateName: '安全管理模板.xls',
        downloadUrl: '/../fvue/static/excel/SecurityManagement.xlsx',
        importUrl:
          window.context.cqxy +
          '/biz/processManagement/securityManage/v1/importExcelData',
        showProject: true,
        // projectId: '',
        // params: {
        //   inspectUserId: '', //巡检员ID
        //   inspectUserName: '', //巡检员姓名
        // },
      },
      projectObj: [], //获取的所有项目数组
      labelPosition: 'right', //表单对齐方式
      searchForm: {
        // projectId: '',
        securityInspectCode: '',
        inspectDate: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {
        accessoryInfo: [],
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
      },
      dialogVisible: false,
      dialogTitle: '添加',
      //表单验证
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '项目名称不能为空',
          trigger: 'blur',
        },
        'projectInfo.projectCode': {
          required: true,
          message: '项目代码不能为空',
          trigger: 'blur',
        },
        inspectDate: {
          required: true,
          message: '检查日期不能为空',
          trigger: 'blur',
        },
        inspectUserName: {
          required: true,
          message: '检查人不能为空',
          trigger: 'blur',
        },
      },
    }
  },
  computed: {
    deleteUrl: function () {
      return (
        window.context.cqxy + '/biz/processManagement/securityManage/v1/delete'
      )
    },
  },
  mounted() {
    this.initProjectAll()
  },
  methods: {
    //重置
    handleReset() {
      // this.searchForm.projectId = ''
      this.searchForm.securityInspectCode = ''
      this.searchForm.inspectDate = ''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0,
      }
      this.$refs['searchForm'].resetFields()
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    handleSelectProject(data) {
      this.form.projectInfo.projectName = data.projectName
      this.form.projectInfo.projectCode = data.projectCode
      // this.form.projectId = data.id
      this.$refs.ruleForm.clearValidate()
    },
    //打开选择项目弹框
    handleFocusProject() {
      this.$refs.project.open()
    },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      let query = []
      let search = {
        // 'pm.ID_': this.searchForm.projectId,
        security_inspect_code_: this.searchForm.securityInspectCode,
        inspect_date_: this.searchForm.inspectDate,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      let param = {
        pageBean: {
          page: 1,
          pageSize: -1,
          showTotal: true,
        },

        querys: query,
        sorter: [],
      }
      req
        .downloadPostCopy(
          '${cqxy}/biz/processManagement/securityManage/v1/exportDataToExcel',
          param
        )
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
          } else {
            this.$message.error('导出失败')
          }
        })
    },
    //获取附件信息
    accessoryInfo(info) {
      this.form.accessoryInfo = info
    },
    //初始获取所有项目数据
    initProjectAll() {
      this.$http.post('${cqxy}/biz/projectManagement/pm/v1/All').then(
        (res) => {
          this.projectObj = res.data.value
        },
        (error) => {
          reject(error)
        }
      )
    },
    //查询
    handleSearch() {
      this.$refs.AtTable.load()
    },
    afterSaveData() {
      setTimeout(() => {
        this.form = {
          accessoryInfo: [],
          projectInfo: {
            projectName: '',
            projectCode: '',
          },
        }
        this.beforeCloseDialog()
        this.$refs.AtTable.load()
      }, 500)
    },
    beforeCloseDialog() {
      this.dialogVisible = false
    },
    handleAdd() {
      this.dialogTitle = '添加'
      this.dialogVisible = true
      if (this.isShow) {
        let selectedProject = this.projectObj.find((item) => {
          if (item.currentProject && item.currentProject == 1) {
            return item.currentProject == 1
          }
        })

        this.form.projectInfo.projectName = selectedProject.projectName
        this.form.projectInfo.projectCode = selectedProject.projectCode
        // this.form.projectId = selectedProject.id
      } else {
        //根据项目id查询
        this.form.projectInfo.projectName = this.projectObj[0].projectName
        this.form.projectInfo.projectCode = this.projectObj[0].projectCode
        // this.form.projectId = this.projectObj[0].id
      }
    },
    handleRowDBClick(data) {
      this.dialogTitle = '修改'
      this.$http
        .post(
          '${cqxy}/biz/processManagement/securityManage/v1/detail/' + data.id
        )
        .then((res) => {
          this.dialogVisible = true
          let response = res.data.value
          this.form = response
        })
    },

    handleSelect(selection) {},
    handleDialogCancel() {
      this.dialogVisible = false
      this.form = {
        accessoryInfo: [],
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
      }
    },
    handleDialogClose() {
      this.dialogVisible = false
      this.form = {
        accessoryInfo: [],
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
      }
    },
    //提交保存
    handleDialogSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid && this.form.accessoryInfo.length !== 0) {
          this.form.accessoryInfo.forEach((item) => {
            if (item.id) {
              delete item.id
            }
          })
          this.$http
            .post(
              '${cqxy}/biz/processManagement/securityManage/v1/save',
              this.form
            )
            .then((res) => {
              if (res.status == 200) {
                setTimeout(() => {
                  this.$message.success(res.data.message)
                  this.afterSaveData()
                }, 500)
              } else {
                this.$message.error(res.data.message)
              }
            })
        } else if (this.form.accessoryInfo.length === 0) {
          this.$message.error('至少上传一个附件')
        } else {
          this.$message({
            message: '*号必填项,不能为空!',
            type: 'warning',
          })
          return false
        }
      })
    },
    loadData(param, cb) {
      let query = [
        {
          group: 'advance',
          operation: 'IS_NULL',
          property: 'pm.ID_',
          relation: 'AND',
          value: '',
        },
        {
          group: 'advance',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'PROJECT_ID_',
          relation: 'OR',
          value: '',
        },
      ]
      let search = {
        // 'pm.ID_': this.searchForm.projectId,
        security_inspect_code_: this.searchForm.securityInspectCode,
        inspect_date_: this.searchForm.inspectDate,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
        // this.importData.projectId = this.pid
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      if (param.querys && param.querys.length) {
        param.querys.push(...query)
      } else {
        param.querys = query
      }
      this.$http
        .post('${cqxy}/biz/processManagement/securityManage/v1/page', param)
        .then(
          (resp) => {
            let response = resp.data.value
            this.data = response.rows
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => cb())
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>
