<template>
  <!--  项目基本信息-->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.data.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="cyan"
          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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['build:info:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['build:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['build:info:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center"
        fixed
      />

      <el-table-column
        label="项目名称"
        align="center"
        prop="projectName"
        :formatter="projectNameFormat"
        min-width="120"
      />
      <el-table-column
        label="项目法人"
        align="center"
        prop="unitLeader"
        min-width="120"
      />
      <el-table-column
        label="项目法人"
        align="center"
        prop="projectLegalPerson"
        min-width="120"
      />
      <el-table-column
        label="项目法人单位"
        align="center"
        prop="projectLegalUnit"
        min-width="120"
      />
      <el-table-column
        label="单位负责人电话"
        align="center"
        prop="projectLegalPhone"
        min-width="120"
      />
      <el-table-column
        label="工程等别"
        align="center"
        prop="engineeringGrade"
        min-width="120"
      />
      <el-table-column
        label="工程用途"
        align="center"
        prop="engineeringPurposes"
        min-width="120"
      />
      <el-table-column
        label="项目建设地址"
        align="center"
        prop="constructionAddress"
        min-width="120"
      />
      <el-table-column
        label="建设模式"
        align="center"
        prop="constructionMode"
        min-width="120"
      />
      <el-table-column
        label="建设性质"
        align="center"
        prop="constructionNature"
        min-width="120"
      />
      <el-table-column
        label="建设内容"
        align="center"
        prop="constructionContent"
        min-width="120"
      />
      <el-table-column
        label="行政区划"
        align="center"
        prop="adcd"
        min-width="120"
      />
      <el-table-column
        label="所属流域"
        align="center"
        prop="basin"
        min-width="120"
      />
      <el-table-column
        label="项目类型"
        align="center"
        prop="projectType"
        min-width="120"
      />
      <!-- <el-table-column label="资金来源" align="center" prop="fundingSource" 
        min-width="120"/> -->
      <el-table-column
        label="是否172项重大水利工程"
        align="center"
        prop="isMajorProject"
        :formatter="isBelongPlanningFormat"
        min-width="120"
      />
      <el-table-column
        label="项目投资主体"
        align="center"
        prop="investmentSubject"
        min-width="120"
      />
      <el-table-column
        label="建设阶段"
        align="center"
        prop="constructionPhase"
        min-width="120"
      />
      <el-table-column
        label="资金下达文号"
        align="center"
        prop="fundingNumber"
        min-width="120"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['build:info:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['build:info: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="90%" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="180px">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <!-- <el-input
                v-model="form.projectName"
                placeholder="请输入项目名称"
              /> -->
              <el-select
                v-model="form.projectName"
                placeholder="请输入项目名称"
                filterable
                clearable
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in projectNameOptions"
                  :key="dict.projectName + dict.id"
                  :label="dict.projectName + '[' + dict.projectCode + ']'"
                  :value="dict.projectName + '[' + dict.projectCode + ']'"
                  @click.native="getId(dict)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目法人" prop="unitLeader">
              <el-input
                v-model="form.unitLeader"
                placeholder="请输入项目法人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目法人" prop="projectLegalPerson">
              <el-input
                v-model="form.projectLegalPerson"
                placeholder="请输入项目法人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目法人单位" prop="projectLegalUnit">
              <el-input
                v-model="form.projectLegalUnit"
                placeholder="请输入项目法人单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位负责人电话" prop="projectLegalPhone">
              <el-input
                v-model="form.projectLegalPhone"
                placeholder="请输入单位负责人电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程等别" prop="engineeringGrade">
              <el-input
                v-model="form.engineeringGrade"
                placeholder="请输入工程等别"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程用途" prop="engineeringPurposes">
              <el-input
                v-model="form.engineeringPurposes"
                placeholder="请输入工程用途"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目建设地址" prop="constructionAddress">
              <el-input
                v-model="form.constructionAddress"
                placeholder="请输入项目建设地址"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设模式" prop="constructionMode">
              <el-input
                v-model="form.constructionMode"
                placeholder="请输入建设模式"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设性质" prop="constructionNature">
              <el-input
                v-model="form.constructionNature"
                placeholder="请输入建设性质"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设内容" prop="constructionContent">
              <el-input
                v-model="form.constructionContent"
                placeholder="请输入建设内容"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="adcd">
              <el-input v-model="form.adcd" placeholder="请输入行政区划" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属流域" prop="basin">
              <el-input v-model="form.basin" placeholder="请输入所属流域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="projectType">
              <el-select
                v-model="form.projectType"
                placeholder="请选择项目类型"
                style="width: 100%"
              >
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中央资金" prop="centralArrangedFunds">
              <el-input
                v-model="form.centralArrangedFunds"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中央资金比例" prop="centralArrangedFundsRate">
              <el-input
                v-model="form.centralArrangedFundsRate"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省级资金" prop="provincialFunding">
              <el-input
                v-model="form.provincialFunding"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省级资金比例" prop="provincialFundingRate">
              <el-input
                v-model="form.provincialFundingRate"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地市资金" prop="cityLevelFunds">
              <el-input
                v-model="form.cityLevelFunds"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地市资金比例" prop="cityLevelFundsRate">
              <el-input
                v-model="form.cityLevelFundsRate"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区县资金" prop="countyLevelFunds">
              <el-input
                v-model="form.countyLevelFunds"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区县资金比例" prop="cityLevelFundsRate">
              <el-input
                v-model="form.countyLevelFundsRate"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="乡镇及群众自筹和其他投资"
              prop="countyLevelFunds"
            >
              <el-input
                v-model="form.townshipInvestments"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="乡镇及群众自筹和其他投资比例"
              prop="cityLevelFundsRate"
            >
              <el-input
                v-model="form.townshipInvestmentsRate"
                placeholder="请输入资金来源"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否172项重大水利工程" prop="isMajorProject">
              <!-- <el-input
                v-model="form.isMajorProject"
                placeholder="请输入是否172项重大水利工程"
              /> -->
              <el-select
                v-model="form.isMajorProject"
                placeholder="是否172项重大水利工程"
                clearable
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in isMajorProjectOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目投资主体" prop="investmentSubject">
              <el-input
                v-model="form.investmentSubject"
                placeholder="请输入项目投资主体"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设阶段" prop="constructionPhase">
              <el-input
                v-model="form.constructionPhase"
                placeholder="请输入建设阶段"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资金下达文号" prop="fundingNumber">
              <el-input
                v-model="form.fundingNumber"
                placeholder="请输入资金下达文号"
              />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <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 {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  exportInfo,
} from "@/api/build/projectInfo";
import {
  listProposal,
  getProposal,
  delProposal,
  addProposal,
  updateProposal,
  exportProposal,
  getProjectCode,
} from "@/api/earlyStage/proposal";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目基本信息管理表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: null,
        data: {
          projectName: null,
          unitLeader: null,
          projectLegalPerson: null,
          projectLegalUnit: null,
          projectLegalPhone: null,
          engineeringGrade: null,
          engineeringPurposes: null,
          constructionAddress: null,
          constructionMode: null,
          constructionNature: null,
          constructionContent: null,
          adcd: null,
          basin: null,
          projectType: null,
          // fundingSource: null,

          centralArrangedFunds: null,
          centralArrangedFundsRate: null,
          provincialFunding: null,
          provincialFundingRate: null,
          cityLevelFunds: null,
          cityLevelFundsRate: null,
          countyLevelFunds: null,
          countyLevelFundsRate: null,
          townshipInvestments: null,
          townshipInvestmentsRate: null,

          isMajorProject: null,
          investmentSubject: null,
          constructionPhase: null,
          fundingNumber: null,
          proCode: null,
          proNo: null,
          createUid: null,
          createTime: null,
          updateUid: null,
          updateTime: null,
          owerDept: null,
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectLegalPerson: [
          // { required: true, message: "项目法人不能为空", trigger: "blur" },
        ],
      },
      //项目名称
      projectNameOptions: [],
      proNo: "",
      // 是否是是否172项重大水利工程
      isMajorProjectOptions: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("whether").then((response) => {
      this.isMajorProjectOptions = response.data;
    });
  },
  methods: {
    // 是否是是否172项重大水利工程
    isBelongPlanningFormat(row, column) {
      return this.selectDictLabel(
        this.isMajorProjectOptions,
        row.isMajorProject
      );
    },
    projectNameFormat(row) {
      if (row.projectName)
        return row.projectName.slice(0, row.projectName.indexOf("["));
    },
    getId(res) {
      // console.log(res)
      getProposal(res.id).then((response) => {
        // console.log(response.data)
        // this.projectType = response.data.projectType;
        this.proNo = res.proNo;
      });
    },
    /** 查询项目基本信息管理列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        unitLeader: null,
        projectLegalPerson: null,
        projectLegalUnit: null,
        projectLegalPhone: null,
        engineeringGrade: null,
        engineeringPurposes: null,
        constructionAddress: null,
        constructionMode: null,
        constructionNature: null,
        constructionContent: null,
        adcd: null,
        basin: null,
        projectType: null,
        // fundingSource: null,

        centralArrangedFunds: null,
        centralArrangedFundsRate: null,
        provincialFunding: null,
        provincialFundingRate: null,
        cityLevelFunds: null,
        cityLevelFundsRate: null,
        countyLevelFunds: null,
        countyLevelFundsRate: null,
        townshipInvestments: null,
        townshipInvestmentsRate: null,

        isMajorProject: null,
        investmentSubject: null,
        constructionPhase: null,
        fundingNumber: null,
        proCode: null,
        proNo: null,
        createUid: null,
        createTime: null,
        updateUid: null,
        updateTime: null,
        owerDept: null,
      };
      this.resetForm("form");
    },
    // 查询表单重置
    resetQueryForm() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        data: {
          id: null,
          projectName: null,
          unitLeader: null,
          projectLegalPerson: null,
          projectLegalUnit: null,
          projectLegalPhone: null,
          engineeringGrade: null,
          engineeringPurposes: null,
          constructionAddress: null,
          constructionMode: null,
          constructionNature: null,
          constructionContent: null,
          adcd: null,
          basin: null,
          projectType: null,
          // fundingSource: null,

          centralArrangedFunds: null,
          centralArrangedFundsRate: null,
          provincialFunding: null,
          provincialFundingRate: null,
          cityLevelFunds: null,
          cityLevelFundsRate: null,
          countyLevelFunds: null,
          countyLevelFundsRate: null,
          townshipInvestments: null,
          townshipInvestmentsRate: null,

          isMajorProject: null,
          investmentSubject: null,
          constructionPhase: null,
          fundingNumber: null,
          proCode: null,
          proNo: null,
          createUid: null,
          createTime: null,
          updateUid: null,
          updateTime: null,
          owerDept: null,
        },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryForm();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      listProposal(this.queryParams).then((response) => {
        this.projectNameOptions = response.records;
      });
      this.open = true;
      this.title = "添加项目基本信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        listProposal(this.queryParams).then((response) => {
          this.projectNameOptions = response.records;
        });
        this.open = true;
        this.title = "修改项目基本信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInfo(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (ids) {
        this.$confirm("是否删除选中的数据？", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return delInfo(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      } else {
        this.$message.warning("请选择要删除的数据！！");
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      let message = "是否确认导出所有的数据项?";
      if (this.ids) {
        message = "是否确认导出选中的数据项?";
        queryParams.ids = this.ids;
      }
      this.$confirm(message, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportInfo(queryParams);
        })
        .then((response) => {
          this.downloadFile(response, true, response.msg);
          // this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep {
  .el-dialog {
    margin-top: 2vh !important;
    height: 95%;
    overflow-y: auto;
    .content {
      display: flex;
      flex-direction: column;
      align-items: center;
      .margin-top {
        width: 95%;
        border-left: 1px solid #f0f0f0;
        border-right: 1px solid #f0f0f0;
      }
    }
  }
  .el-dialog__title {
    font-weight: 700;
    font-size: 28px;
    // font-size: 24px;
    // line-height: 32px;
  }
}
</style>
