<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId" filterable  placeholder="请选择项目" clearable @change="doQuerySub">
          <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.projectName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="总包单位" prop="deptId">        
        <el-select v-model="queryParams.deptId" filterable placeholder="请输入总包单位名称" clearable>
          <el-option v-for="(item, index) in depts" :key="index" :label="item.deptName" :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="保险公司" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入保险公司" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="保险单号" prop="insuranceNumber">
        <el-input v-model="queryParams.insuranceNumber" placeholder="请输入保险单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="保险类型" prop="insuranceType">
        <el-select v-model="queryParams.insuranceType" placeholder="请选择保险类型" clearable>
          <el-option v-for="dict in dict.type.sur_project_insurance_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="保险状态" prop="insuranceState">
        <el-select v-model="queryParams.insuranceState" placeholder="请选择保险类型" clearable>
          <el-option v-for="dict in dict.type.sur_project_insurance_state" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </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>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <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>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:surProjectInsurance:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['project:surProjectInsurance:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['project:surProjectInsurance:export']">导出</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" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="center" prop="projectName"  width="200" show-overflow-tooltip />
      <el-table-column label="总包单位" align="center" prop="deptName"  width="180" show-overflow-tooltip />
      <el-table-column label="保险类型" align="center" prop="insuranceTypeName" />
      <el-table-column label="保险状态" align="center" prop="insuranceState">
        <template slot-scope="scope">
          <svg v-if="scope.row.insuranceState == '1'" style="
              width: 1em;
              height: 1em;
              position: relative;
              top: -2px;
              vertical-align: middle;
              fill: currentColor;
              overflow: hidden;
            " viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3100">
            <path
              d="M802.922882 383.309012 428.076612 758.155283 220.943065 551.154765c-22.317285-22.317285-22.317285-55.993269 0-78.310553 22.450315-22.450315 55.993269-22.450315 78.443583 0l128.689964 128.689964L724.613352 304.999482c22.450315-22.450315 55.993269-22.450315 78.30953 0C825.373197 327.316767 825.373197 360.858698 802.922882 383.309012zM512 64.322981c-246.155283 0-447.677019 201.521736-447.677019 447.677019s201.521736 447.677019 447.677019 447.677019 447.677019-201.521736 447.677019-447.677019S758.155283 64.322981 512 64.322981z"
              fill="#22C134" p-id="3101"></path>
          </svg>
          <svg v-else style="
              width: 1em;
              height: 1em;
              position: relative;
              top: -2px;
              vertical-align: middle;
              fill: currentColor;
              overflow: hidden;
            " viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3472">
            <path
              d="M512 981.333333C252.8 981.333333 42.666667 771.2 42.666667 512S252.8 42.666667 512 42.666667s469.333333 210.133333 469.333333 469.333333-210.133333 469.333333-469.333333 469.333333z m44.245333-469.333333l159.914667-159.914667a31.274667 31.274667 0 1 0-44.245333-44.245333L512 467.754667 352.085333 307.84a31.274667 31.274667 0 1 0-44.245333 44.245333L467.754667 512l-159.914667 159.914667a31.274667 31.274667 0 1 0 44.245333 44.245333L512 556.245333l159.914667 159.914667a31.274667 31.274667 0 1 0 44.245333-44.245333L556.245333 512z"
              fill="#F5222D" p-id="3473"></path>
          </svg>
          <span v-if="scope.row.insuranceState == '1'" style="font-weight: 700; color: rgb(34, 193, 52)">
            已办理
          </span>
          <span v-if="scope.row.insuranceState == '0'" style="font-weight: 700; color: rgb(245, 34, 45)">
            未办理
          </span>
        </template>
      </el-table-column>
      <el-table-column label="保险公司" align="center" prop="companyName"  width="180" show-overflow-tooltip />
      <el-table-column label="保险单号" align="center" prop="insuranceNumber"  width="180" show-overflow-tooltip />
      <el-table-column label="数据状态" align="center" prop="isDel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_isdel" :value="scope.row.isDel" />
        </template>
      </el-table-column>
      <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 :close-on-click-modal="false"
      :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="保险类型" prop="insuranceType">
          <el-select v-model="form.insuranceType" placeholder="请选择保险类型" :disabled="form.id != null" 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" :fileType="['pdf']" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input 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>
  </div>
</template>

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

export default {
  name: "SurProjectInsurance",
  dicts: [
    "sur_project_insurance_type",
    "sur_project_insurance_state",
    "sys_common_isdel",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目保险表格数据
      surProjectInsuranceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        projectName: null,
        deptId: null,
        deptName: null,
        insuranceType: null,
        insuranceNumber: null,
        insuranceFile: null,
        insuranceState: null,
        companyName: null,
        isDel: null,
      },
      // 表单参数
      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" }],
      },
      projectOptions:[],
      depts:[],
    };
  },
  created() {
    this.getList();
    this.$api.publics.getMyProjectList({}).then((response) => {
        this.projectOptions = response.rows;
    }); 
  },
  methods: {
    doQuerySub() {
      let prjId=this.queryParams.projectId;      
      let tmps = this.prjDept2 && this.prjDept2[prjId] ? this.prjDept2[prjId] || [] : [];
      if (tmps.length > 0 || !prjId) {
        this.depts = tmps;
        if (tmps.length == 1) {
          this.queryParams.deptId = tmps[0].deptId;
        } else {
          this.queryParams.deptId = '';
        }
        return;
      }
      this.$api.publics.queryUnitList({
        projectId: prjId,
        unitTypes: "2".split(","),
      }).then((d) => {
        let objs = d.rows || [];
        if (!this.prjDept2) {
          this.prjDept2 = {};
        }
        this.prjDept2[prjId] = objs;
        this.depts = objs;
        if (objs.length == 1) {
          this.queryParams.deptId = objs[0].deptId;
        } else {
          this.queryParams.deptId = '';
        }
      });
    },
    /** 查询项目保险列表 */
    getList() {
      this.loading = true;
      listSurProjectInsurance(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() {
      this.reset();
      this.open = true;
      this.title = "添加项目保险";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSurProjectInsurance(id).then((response) => {
        this.form = response.data;
        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) {
            updateSurProjectInsurance(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSurProjectInsurance(this.form).then((response) => {
              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(process.env.VUE_APP_BASE_API + row.insuranceFile);
    },
  },
};
</script>
