<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="投入台账名称" prop="investmentName">
        <el-input
          v-model="queryParams.investmentName"
          placeholder="请输入投入台账名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投入年度" prop="investmentYear">
        <el-date-picker
          v-model="queryParams.investmentYear"
          type="year"
          value-format="yyyy"
          size="small"
          clearable
          @keyup.enter.native="handleQuery"
          placeholder="选择年度">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="投入类型" prop="investmentType">
        <el-select v-model="queryParams.investmentType" placeholder="请选择投入类型" clearable size="small">
          <el-option
            v-for="dict in investmentOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="investmentList">
      <el-table-column label="投入台账名称" align="center" prop="investmentName" width="300px"/>
      <el-table-column label="投入年度" align="center" prop="investmentYear" />
      <el-table-column label="投入资金" align="center" prop="investmentFunds" />
      <el-table-column label="投入类型" align="center" prop="investmentType" :formatter="investmentFormat"  />
      <el-table-column label="同期收入" align="center" prop="income" >
        <template slot-scope="scope">
          <span v-if="scope.row.income != null && scope.row.income != ''">{{scope.row.income}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="提取方式" align="center" prop="drawType" :formatter="drawFormat"  />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="23">
            <el-form-item label="投入台账名称" prop="investmentName">
              <el-input v-model="form.investmentName" placeholder="请输入投入台账名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="投入年度" prop="investmentYear">
              <el-date-picker
                v-model="form.investmentYear"
                type="year"
                value-format="yyyy"
                placeholder="选择年度"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投入资金" prop="investmentFunds">
              <el-input v-model="form.investmentFunds" placeholder="请输入投入资金" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="投入类型" prop="investmentType">
              <el-select v-model="form.investmentType" placeholder="请选择投入类型" filterable clearable style="width: 100%">
                <el-option
                  v-for="dict in investmentOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="同期收入" prop="income">
              <el-input v-model="form.income" placeholder="请输入同期收入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="提取方式" prop="drawType">
              <el-select v-model="form.drawType" placeholder="请选择提取方式" filterable clearable style="width: 100%">
                <el-option
                  v-for="dict in drawOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时间单位" prop="timeUnit" v-if="form.drawType == '1'">
              <el-select v-model="form.timeUnit" placeholder="请选择提取时间单位" filterable clearable style="width: 100%">
                <el-option
                  v-for="dict in timeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目名称" prop="projectName" v-if="form.drawType == '2'">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="评价" prop="evaluate">
              <el-input type="textarea" v-model="form.evaluate" placeholder="请输入评价" />
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listInvestment, getInvestment, delInvestment, addInvestment, updateInvestment, exportInvestment } from "@/api/safetyManagement/investment";

export default {
  name: "Investment",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投入台账表格数据
      investmentList: [],
      // 日期范围
      dateRange: [],
      investmentOptions: [],
      drawOptions: [],
      timeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        investmentName: null,
        investmentYear: null,
        investmentFunds: null,
        investmentType: null,
        income: null,
        drawType: null,
        evaluate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        investmentName: [
          { required: true, message: "请输入投入台账名称", trigger: "blur" }
        ],
        investmentYear: [
          { required: true, message: "请选择投入年度", trigger: "blur" }
        ],
        investmentFunds: [
          { required: true, message: "请输入投入资金", trigger: "blur" }
        ],
        investmentType: [
          { required: true, message: "请选择投入类型", trigger: "blur" }
        ],
        drawType: [
          { required: true, message: "请选择提取方式", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_investment_type").then(response => {
      this.investmentOptions = response.data;
    });
    this.getDicts("t_draw_type").then(response => {
      this.drawOptions = response.data;
    });
    this.getDicts("t_time_unit").then(response => {
      this.timeOptions = response.data;
    });
  },
  methods: {
    // 投入类型
    investmentFormat(row, column) {
      return this.selectDictLabel(this.investmentOptions, row.investmentType);
    },
    // 提取方式
    drawFormat(row, column) {
      return this.selectDictLabel(this.drawOptions, row.drawType);
    },
    /** 查询投入台账列表 */
    getList() {
      this.loading = true;
      listInvestment(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.investmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        investmentName: null,
        investmentYear: null,
        investmentFunds: null,
        investmentType: null,
        income: null,
        timeUnit: null,
        projectName: null,
        drawType: null,
        evaluate: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投入台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInvestment(id).then(response => {
        this.form = response.data;
        console.log("this.form",this.form)
        // investmentYear投入年度数据库中为year类型，类中为short
        this.form.investmentYear = "'" + this.form.investmentYear + "'";
        this.open = true;
        this.title = "修改投入台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log("this.form", this.form)
        if (valid) {
          // income同期收入，数值类型
          if(this.form.income == ''){
            this.form.income = null;
          }
          if(this.form.investmentType == '1'){
            this.form.projectName = null;
          } else if(this.form.investmentType == '2'){
            this.form.timeUnit = null;
          }
          // investmentYear投入年度数据库中为year类型，类中为short
          this.form.investmentYear = eval(this.form.investmentYear);
          if (this.form.id != null) {
            updateInvestment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvestment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有投入台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInvestment(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
