<template>
  <div class="projectect-attendance-drawer">
    <el-drawer
      v-if="isOpen"
      :visible.sync="isOpen"
      direction="rtl"
      size="50%"
      style="padding-left: 20px"
    >
      <template slot="title">
        <div>{{ title + " 【项目保险管理】" }}</div>
      </template>
      <el-tabs v-model="activeName" style="padding-left: 20px" @tab-click="getList">
        <el-tab-pane
          v-for="(it, idx) in nodes"
          :label="it.unitName + ''"
          :name="it.unitId + ''"
          :key="idx"
        ></el-tab-pane>
      </el-tabs>
      <el-row
        :gutter="10"
        class="mb8"
        style="margin-left: 25px; margin-top: 10px; margin-right: 0px"
      >
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['project:surProjectInsurance:add']"
            >办理保险</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table
        v-loading="loading"
        :data="surProjectInsuranceList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="保险类型" align="center" prop="insuranceTypeName" />
        <el-table-column label="保险状态" align="center" prop="insuranceState">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sur_project_insurance_state"
              :value="scope.row.insuranceState"
            />
          </template>
        </el-table-column>
        <el-table-column label="保险公司" align="center" prop="companyName" />
        <el-table-column label="保险单号" align="center" prop="insuranceNumber" />
        <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"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.insuranceState == 1"
              size="mini"
              type="text"
              icon="el-icon-link"
              @click="handleDownload(scope.row)"
              v-hasPermi="['project:surProjectInsurance:edit']"
              >保险合同</el-button
            >
            <el-button
              v-if="scope.row.insuranceState == 1"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['project:surProjectInsurance:edit']"
              >修改</el-button
            >
            <el-button
              v-if="scope.row.insuranceState == 1"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['project:surProjectInsurance:remove']"
              >删除</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="680px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="项目名称" prop="projectName">
            <el-input
              v-model="form.projectName"
              placeholder="请输入项目名称"
              :disabled="true"
            />
          </el-form-item>
          <el-form-item label="部门名称" prop="deptName">
            <el-input
              v-model="form.deptName"
              placeholder="请输入部门名称"
              :disabled="true"
            />
          </el-form-item>
          <el-form-item label="保险类型" prop="insuranceType">
            <el-select
              :disabled="form.id != null"
              v-model="form.insuranceType"
              placeholder="请选择保险类型"
              style="width: 100%"
            >
              <el-option
                v-for="dict in dict.type.sur_project_insurance_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="保险公司" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入保险公司" />
          </el-form-item>
          <el-form-item label="保险单号" prop="insuranceNumber">
            <el-input v-model="form.insuranceNumber" placeholder="请输入保险单号" />
          </el-form-item>
          <el-form-item label="保险合同" prop="insuranceFile">
            <file-upload
              v-model="form.insuranceFile"
              :limit="1"
              :fileType="['pdf', 'png', 'jpg', 'jpeg']"
            />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-drawer>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  listSurProjectInsurance,
  listSurProjectInsurancev2,
  getSurProjectInsurance,
  delSurProjectInsurance,
  addSurProjectInsurance,
  updateSurProjectInsurance,
} from "@/api/project/surProjectInsurance";

export default {
  name: "RuoyiUiProjectDeptWroksDrawer",
  dicts: ["sur_project_insurance_type", "sur_project_insurance_state"],
  data() {
    return {
      open: false,
      isOpen: false,
      project: null,
      title: "",
      isUnit: true,
      nodes: [],
      form: {},
      rules: {
        insuranceType: [{ required: true, message: "请选择保险类型", trigger: "blur" }],
        companyName: [
          { required: true, message: "请输入保险公司名称", trigger: "blur" },
          { max: 20, message: "请输入", trigger: "blur" },
        ],
        insuranceNumber: [
          { required: false, message: "请输入保险单编号", trigger: "blur" },
        ],
        insuranceFile: [{ required: true, message: "请上传保险合同", trigger: "blur" }],
      },
      surProjectInsuranceList: [],
      activeName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        deptId: null,
      },
    };
  },
  computed: {
    ...mapGetters(["dept"]),
  },
  mounted() {},

  methods: {
    doOk() {
      updateProjectDeptWroks(this.form).then((response) => {
        this.$modal.msgSuccess("保存成功");
        this.isOpen = false;
      });
    },
    doCanel() {
      this.isOpen = false;
    },
    show(project) {
      this.project = project;
      this.title = project.projectName;
      this.isOpen = true;
      this.queryParams.projectId = project.id;
      this.$api.publics
        .queryUnitList({
          projectId: project.id,
          unitType: 2,
        })
        .then((d) => {
          this.nodes = d.rows;
          if (d.rows.length > 0) {
            this.activeName = this.nodes[0].unitId + "";
            this.isUnit = true;
            this.getList();
          } else {
            this.surProjectInsuranceList = [];
            this.$message.error("当前项目未分配总包单位，不能办理保险！");
            this.isUnit = false;
          }
        });
    },
    // 页签点击
    getList() {
      this.queryParams.deptId = this.activeName;
      this.loading = true;
      listSurProjectInsurancev2(this.queryParams).then((response) => {
        this.surProjectInsuranceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        deptId: null,
        insuranceType: null,
        insuranceNumber: null,
        insuranceFile: null,
        insuranceState: null,
        beginDate: null,
        endDate: null,
        companyName: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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() {
      if (this.nodes.length > 0) {
        this.reset();
        this.form.projectId = this.project.id;
        this.form.projectName = this.project.projectName;
        this.form.deptId = this.activeName;
        this.nodes.forEach((item) => {
          if (item.unitId == this.activeName) {
            this.form.deptName = item.unitName;
          }
        });
        this.open = true;
        this.title = "添加项目保险";
      } else {
        this.$message.error("当前项目未分配总包单位，不能办理保险！");
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSurProjectInsurance(id).then((response) => {
        this.form = response.data;
        this.form.projectName = this.project.projectName;
        this.nodes.forEach((item) => {
          if (item.unitId == this.activeName) {
            this.form.deptName = item.unitName;
          }
        });
        this.open = true;
        this.title = "修改项目保险";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (
            this.form.insuranceFile != null &&
            this.form.insuranceFile != "" &&
            this.form.insuranceFile.length > 0
          ) {
            this.form.insuranceFile = this.form.insuranceFile[0].url;
          } else {
            this.$modal.msgWarning("请上传保险合同");
            return false;
          }
          if (this.form.id != null) {
            updateSurProjectInsurance(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSurProjectInsurance(this.form).then((response) => {
              if (
                response.code == 200 &&
                response.data &&
                response.data.resultCode == "-1"
              ) {
                this.$message.error("当前项目单位已办理该保险，请勿重复办理！！");
              } else {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除项目保险编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSurProjectInsurance(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "project/surProjectInsurance/export",
        {
          ...this.queryParams,
        },
        `surProjectInsurance_${new Date().getTime()}.xlsx`
      );
    },
    /** 预览 */
    handledownload(row) {
      window.open("/jhapi" + row.insuranceFile);
    },
  },
};
</script>
<style lang="scss" scoped></style>
