<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <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="['business:IMPORTANTPROJECT: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="['business:IMPORTANTPROJECT: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="['business:IMPORTANTPROJECT: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="['business:IMPORTANTPROJECT:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      :data="IMPORTANTPROJECTList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="重点项目数据	主键" align="center" prop="id"> -->
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.id"/>
        </template> -->
      <!-- </el-table-column> -->
      <el-table-column label="项目名称 " align="center" prop="xmmc">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.xmmc"/>
        </template> -->
      </el-table-column>
      <el-table-column label="建设单位 " align="center" prop="jsdw">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.jsdw"/>
        </template> -->
      </el-table-column>
      <el-table-column label="承建单位 " align="center" prop="cjdw">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.cjdw"/>
        </template> -->
      </el-table-column>
      <el-table-column label="建设规模 " align="center" prop="jsgm">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.jsgm"/>
        </template> -->
      </el-table-column>
      <el-table-column label="建设内容 " align="center" prop="jsnr">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.jsnr"/>
        </template> -->
      </el-table-column>
      <el-table-column label="年度投资计划 " align="center" prop="ndtzjh">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.ndtzjh"/>
        </template> -->
      </el-table-column>
      <el-table-column label="实际完成投资额 " align="center" prop="sjwctze">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.sjwctze"/>
        </template> -->
      </el-table-column>
      <el-table-column
        label="年度投资完成百分比 "
        align="center"
        prop="ndtzwcbfb"
      >
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.ndtzwcbfb"/>
        </template> -->
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.createTime"/>
        </template> -->
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.createBy"/>
        </template> -->
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.updateTime"/>
        </template> -->
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.updateBy"/>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="部门主键" align="center" prop="deptId"> -->
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.deptId"/>
        </template> -->
      <!-- </el-table-column> -->
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="130"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:IMPORTANTPROJECT:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:IMPORTANTPROJECT: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" :close-on-click-modal="false" width="35%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="项目名称" prop="xmmc">
          <el-input v-model="form.xmmc" placeholder="请输入项目名称"></el-input>
        </el-form-item>

        <el-form-item label="建设单位" prop="jsdw">
          <el-select
            v-model="buildValue"
            @change="selectBuild"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in deptList"
              :key="index"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="承设单位" prop="cjdw">
          <!-- {{ buildValue  }} -->
          <el-select
            v-model="deptValue"
            @change="selectDept"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in deptList"
              :key="index"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="建设规模 " prop="jsgm">
          <el-input
            v-model="form.jsgm"
            placeholder="请输入建设规模 "
          ></el-input>
        </el-form-item>
        <el-form-item label="建设内容 " prop="jsnr">
          <el-input
            v-model="form.jsnr"
            placeholder="请输入建设内容 "
          ></el-input>
        </el-form-item>

        <el-form-item label="年度投资计划 " prop="ndtzjh">
          <el-input
            v-model="form.ndtzjh"
            placeholder="请输入年度投资计划 "
          ></el-input>
        </el-form-item>

        <el-form-item label="实际完成投资额 " prop="sjwctze">
          <el-input
            v-model="form.sjwctze"
            placeholder="请输入实际完成投资额 "
          ></el-input>
        </el-form-item>

        <el-form-item label="年度投资完成百分比 " prop="ndtzwcbfb">
          <el-input
            v-model="form.ndtzwcbfb"
            placeholder="请输入年度投资完成百分比 "
          ></el-input>
        </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 { listDept } from "@/api/system/dept";

import {
  listIMPORTANTPROJECT,
  getIMPORTANTPROJECT,
  delIMPORTANTPROJECT,
  addIMPORTANTPROJECT,
  updateIMPORTANTPROJECT,
} from "@/api/portal/IMPORTANTPROJECT/IMPORTANTPROJECT";

export default {
  name: "IMPORTANTPROJECT",
  data() {
    return {
      // 单位列表
      deptList: [],
      deptValue: null,
      buildValue: null,

      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 重点项目数据							 表格数据
      IMPORTANTPROJECTList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        xmmc: null,
        jsdw: null,
        cjdw: null,
        jsgm: null,
        jsnr: null,
        ndtzjh: null,
        sjwctze: null,
        ndtzwcbfb: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {
            required: true,
            message: "重点项目数据	主键不能为空",
            trigger: "blur",
          },
        ],
        xmmc: [
          { required: true, message: "项目名称 不能为空", trigger: "blur" },
        ],
        jsdw: [
          { required: true, message: "建设单位 不能为空", trigger: "blur" },
        ],
        cjdw: [
          { required: true, message: "承建单位 不能为空", trigger: "blur" },
        ],
        jsgm: [
          { required: true, message: "建设规模 不能为空", trigger: "blur" },
        ],
        jsnr: [
          { required: true, message: "建设内容 不能为空", trigger: "blur" },
        ],
        ndtzjh: [
          { required: true, message: "年度投资计划 不能为空", trigger: "blur" },
        ],
        sjwctze: [
          {
            required: true,
            message: "实际完成投资额 不能为空",
            trigger: "blur",
          },
        ],
        ndtzwcbfb: [
          {
            required: true,
            message: "年度投资完成百分比 不能为空",
            trigger: "blur",
          },
        ],
        deptId: [
          {
            required: true,
            message: "部门主键,根据[建设单位 ]获得不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    listDept().then((res) => {
      res.data.forEach((item) => {
        if (item.type == 3) {
          this.deptList.push(item);
        }
      });
      console.log(this.deptList);
    });
  },
  methods: {
    /** 查询重点项目数据							 列表 */
    getList() {
      this.loading = true;
      listIMPORTANTPROJECT(this.queryParams).then((response) => {
        this.IMPORTANTPROJECTList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    selectDept(val) {
      this.form.cjdw = val;
    },
    selectBuild(val) {
      this.form.jsdw = val;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xmmc: null,
        jsdw: null,
        cjdw: null,
        jsgm: null,
        jsnr: null,
        ndtzjh: null,
        sjwctze: null,
        ndtzwcbfb: null,
        deptId: null,
      };
      this.deptValue = null;
      this.buildValue = 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;
      getIMPORTANTPROJECT(id).then((response) => {
        this.deptList.forEach((item) => {
          if (item.deptId == response.data.jsdw) {
            this.deptValue = item.deptName;
          } else if (item.deptId == response.data.cjdw) {
            this.buildValue = item.deptName;
          }
        });
        this.form = response.data;
        this.open = true;
        this.title = "修改重点项目数据							 ";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateIMPORTANTPROJECT(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.deptId = this.$store.state.user.
            addIMPORTANTPROJECT(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 delIMPORTANTPROJECT(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/IMPORTANTPROJECT/export",
        {
          ...this.queryParams,
        },
        `IMPORTANTPROJECT_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
