<template>
  <div class="app-container">
    <div v-if="-2 == $store.state.user.enterpriseId">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input
            v-model="queryParams.enterpriseName"
            placeholder="请输入企业名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="timeChange"
        ></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"
          v-hasPermi="['system:sup: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"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</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="supList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" :show-overflow-tooltip="true"/>
      <el-table-column label="天然气计划量/wm³" align="center" prop="fPlanQuan" />
      <el-table-column label="天然气批复量/wm³" align="center" prop="fApprQuan" />
      <el-table-column label="天然气供气量/wm³" align="center" prop="fSupGasVol" />
      <el-table-column label="天然气存储量/wm³" align="center" prop="fStorageVol" />
      <el-table-column label="液化气计划量/吨" align="center" prop="fPlanYhq"/>
      <el-table-column label="液化气批复量/吨" align="center" prop="fApprYhq"/>
      <el-table-column label="液化气供气量/吨" align="center" prop="fSupGasYhq"/>
      <el-table-column label="液化气存储量/吨" align="center" prop="fStorageYhq"/>
      <el-table-column label="监管气量日期" align="center" prop="fSupDate" />
      <el-table-column label="上报省厅状态" align="center" prop="fGovUploadStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.fGovUploadStatus == '0'">未上报</span>
          <span v-else-if="scope.row.fGovUploadStatus == '1'">已上报</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="上报省厅时间" align="center" prop="fGovUploadTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.fGovUploadTime">{{ parseTime(scope.row.fGovUploadTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <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>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleReport(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"
    />
    </div>

    <!-- 添加或修改气量监管对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业uuid" prop="fCompanyInfoId">
          <el-input v-model="form.fCompanyInfoId" placeholder="请输入企业uuid" />
        </el-form-item>
        <el-form-item label="天然气计划量" prop="fPlanQuan">
          <el-input v-model="form.fPlanQuan" placeholder="请输入天然气计划量" />
        </el-form-item>
        <el-form-item label="天然气批复量" prop="fApprQuan">
          <el-input v-model="form.fApprQuan" placeholder="请输入天然气批复量" />
        </el-form-item>
        <el-form-item label="天然气供气量" prop="fSupGasVol">
          <el-input v-model="form.fSupGasVol" placeholder="请输入天然气供气量" />
        </el-form-item>
        <el-form-item label="天然气存储量" prop="fStorageVol">
          <el-input v-model="form.fStorageVol" placeholder="请输入天然气存储量" />
        </el-form-item>
        <el-form-item label="液化气计划量" prop="fPlanYhq">
          <el-input v-model="form.fPlanYhq" placeholder="请输入液化气计划量" />
        </el-form-item>
        <el-form-item label="液化气批复量" prop="fApprYhq">
          <el-input v-model="form.fApprYhq" placeholder="请输入液化气批复量" />
        </el-form-item>
        <el-form-item label="液化气供气量" prop="fSupGasYhq">
          <el-input v-model="form.fSupGasYhq" placeholder="请输入液化气供气量" />
        </el-form-item>
        <el-form-item label="液化气存储量" prop="fStorageYhq">
          <el-input v-model="form.fStorageYhq" placeholder="请输入液化气存储量" />
        </el-form-item>
        <el-form-item label="监管气量日期" prop="fSupDate">
          <el-input v-model="form.fSupDate" placeholder="请输入监管气量日期" />
        </el-form-item>
        <el-form-item label="上报状态 0-未上报，1-已上报" prop="fRepStatus">
          <el-select v-model="form.fRepStatus" placeholder="请选择上报状态 0-未上报，1-已上报">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="上报日期" prop="fRepDate">
          <el-date-picker clearable size="small"
            v-model="form.fRepDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上报日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否删除(0正常，1删除)" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入是否删除(0正常，1删除)" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" 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 v-if="-2 != $store.state.user.enterpriseId" class="report-content">
      <el-row style="margin-left: -3.5px;margin-right: -3.5px">
        <el-col :span="12" style="padding: 0px 3.5px 0px 3.5px">
          <div class="report-title">
            <div class="title">上报记录</div>
          </div>
          <div class="content">
            <!-- 日历 -->
            <Calendar
              width="100%"
              v-model="reportDate"
              :scheduleData="scheduleData"
              @dateChange="dateChange"
            />
          </div>
        </el-col>

        <el-col :span="12" style="padding: 0px 3.5px 0px 3.5px">
          <div class="report-title flex-between">
            <div class="title">上报情况</div>
            <el-button
              type="primary"
              size="small"
              @click="reportInfo"
            >
              上报
            </el-button>
          </div>
          <div class="content">
            <div class="state-warp">
              <el-row>
                <el-col :span="13">
                  <div class="label-warp">状态</div>
                  <div class="value-warp">
                    <span v-if="reportForm.fRepStatus == '1'">已上报</span>
                    <span v-else>未上报</span>
                  </div>
                </el-col>
                <el-col :span="11" style="padding-left: 45px">
                  <div class="label-warp">上报时间</div>
                  <div class="value-warp">
                    <span v-if="reportForm.fRepDate">{{ reportForm.fRepDate }}</span>
                    <span v-else>暂无</span>
                  </div>
                </el-col>
              </el-row>
            </div>

            <el-form :rules="rules" label-position="top" label-width="80px" :model="reportForm">
              <el-form-item label="天然气计划量" prop="fPlanQuan">
                <el-input v-model="reportForm.fPlanQuan">
                  <template slot="append">万立方</template>
                </el-input>
              </el-form-item>
              <el-form-item label="天然气批复量" prop="fApprQuan">
                <el-input v-model="reportForm.fApprQuan">
                  <template slot="append">万立方</template>
                </el-input>
              </el-form-item>
              <el-form-item label="天然气供气量" prop="fSupGasVol">
                <el-input v-model="reportForm.fSupGasVol">
                  <template slot="append">万立方</template>
                </el-input>
              </el-form-item>
              <el-form-item label="天然气存储量" prop="fStorageVol">
                <el-input v-model="reportForm.fStorageVol">
                  <template slot="append">万立方</template>
                </el-input>
              </el-form-item>
              <el-form-item label="液化气计划量" prop="fPlanYhq">
                <el-input v-model="reportForm.fPlanYhq">
                  <template slot="append">吨</template>
                </el-input>
              </el-form-item>
              <el-form-item label="液化气批复量" prop="fApprYhq">
                <el-input v-model="reportForm.fApprYhq">
                  <template slot="append">吨</template>
                </el-input>
              </el-form-item>
              <el-form-item label="液化气供气量" prop="fSupGasYhq">
                <el-input v-model="reportForm.fSupGasYhq">
                  <template slot="append">吨</template>
                </el-input>
              </el-form-item>
              <el-form-item label="液化气存储量" prop="fStorageYhq">
                <el-input v-model="reportForm.fStorageYhq">
                  <template slot="append">吨</template>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>

    </div>

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listSup, getSup, delSup, addSup, updateSup, exportSup, getReportInfoByCompany, reportInfoList, reportSupBalGasSupRecInfo } from "@/api/supplybalance/gas";
import Calendar from "@/components/Calendar";
import moment from "moment";
import DetailInfo from "./components/DetailInfo";
export default {
  name: "Sup",
  components: {
    Calendar,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 气量监管表格数据
      supList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fCompanyInfoId: null,
        fPlanQuan: null,
        fApprQuan: null,
        fSupGasVol: null,
        fStorageVol: null,
        fRepStatus: '1',
        beginRepDate: null,
        endRepDate: null,
        enterpriseName: null
      },
      // 表单参数
      form: {},
      // 所属区域字典
      townshipOptions: [],
      // 表单校验
      rules: {
        fPlanQuan: [
          { required: true, message: "天然气计划量不能为空", trigger: "blur" }
        ],
        fApprQuan: [
          { required: true, message: "天然气批复量不能为空", trigger: "blur" }
        ],
        fSupGasVol: [
          { required: true, message: "天然气供气量不能为空", trigger: "blur" }
        ],
        fStorageVol: [
          { required: true, message: "天然气存储量不能为空", trigger: "blur" }
        ],
        fPlanYhq: [
          { required: true, message: "液化气计划量不能为空", trigger: "blur" }
        ],
        fApprYhq: [
          { required: true, message: "液化气批复量不能为空", trigger: "blur" }
        ],
        fSupGasYhq: [
          { required: true, message: "液化气供气量不能为空", trigger: "blur" }
        ],
        fStorageYhq: [
          { required: true, message: "液化气存储量不能为空", trigger: "blur" }
        ]
      },
      reportDate: moment().format('YYYY-MM-DD'),
      scheduleData:[
        {date:'2024-06-19',content:'已上报'},
        {date:'2024-06-17',content:'未上报'},
        {date:'2024-06-15',content:'已上报'},
        {date:'2024-06-14',content:'已上报'}
      ],
      reportForm:{},
      dateRange: []
    };
  },
  created() {
    this.getDicts("t_township").then(response => {
      this.townshipOptions = response.data;
    });
    if(-2 == this.$store.state.user.enterpriseId){
      this.getList();
      return;
    }
    const moment = require('moment');
    // 获取当月开始时间
    const startOfMonth = moment().startOf('month').format('YYYY-MM-DD HH:mm:ss');
    // 获取当月结束时间
    const endOfMonth = moment().endOf('month').format('YYYY-MM-DD HH:mm:ss');
    this.currentMonthReportInfo(startOfMonth,endOfMonth);
  },
  mounted(){
    this.getReportInfo();
  },
  methods: {
    // 所属区域字典翻译
    townshipFormat(row, column) {
      return this.selectDictLabel(this.townshipOptions, row.township);
    },
    /** 查询气量监管列表 */
    getList() {
      this.loading = true;
      listSup(this.queryParams).then(response => {
        this.supList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fGasSupId: null,
        fCompanyInfoId: null,
        fPlanQuan: null,
        fApprQuan: null,
        fSupGasVol: null,
        fStorageVol: null,
        fPlanYhq: null,
        fApprYhq: null,
        fSupGasYhq: null,
        fStorageYhq: null,
        fSupDate: null,
        fRepStatus: null,
        fRepDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetReportForm() {
      this.reportForm = {
        fGasSupId: null,
        fCompanyInfoId: null,
        fPlanQuan: null,
        fApprQuan: null,
        fSupGasVol: null,
        fStorageVol: null,
        fPlanYhq: null,
        fApprYhq: null,
        fSupGasYhq: null,
        fStorageYhq: null,
        fSupDate: null,
        fRepStatus: null,
        fRepDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.queryParams.beginRepDate = null;
      this.queryParams.endRepDate = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fGasSupId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加气量监管";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fGasSupId = row.fGasSupId || this.ids
      getSup(fGasSupId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改气量监管";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fGasSupId != null) {
            updateSup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fGasSupIds = row.fGasSupId || this.ids;
      this.$confirm('是否确认删除气量监管编号为"' + fGasSupIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSup(fGasSupIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有气量监管数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSup(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //日期切换
    dateChange(val){
      this.resetReportForm();
      this.getReportInfo();
      if(val == 0){
        const startOfMonth = moment(this.reportDate).startOf('month').format('YYYY-MM-DD HH:mm:ss');
        // 获取当月结束时间
        const endOfMonth = moment(this.reportDate).endOf('month').format('YYYY-MM-DD HH:mm:ss');
        this.currentMonthReportInfo(startOfMonth,endOfMonth);
      }
    },
    getReportInfo(){
      getReportInfoByCompany({companyId:this.$store.state.user.enterpriseId,calendarDate:this.reportDate}).then(res =>{
        if(res.code && res.data){
          this.reportForm = res.data;
        }
      })
    },
    //上报
    reportInfo(){
      if (this.reportForm.fGasSupId != null) {
        let that = this;
        this.$confirm('当前已上报，是否确认再次上报?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          that.reportForm.fRepDate =  moment().format('YYYY-MM-DD HH:mm:ss');
          that.reportForm.calendarDate = that.reportDate;
          that.reportForm.fRepStatus = '1';
          return updateSup(that.reportForm);
        }).then(() => {
          that.getReportInfo();
          that.msgSuccess("上报成功");
        }).catch(() => {});
      } else {
        this.reportForm.fRepDate =  moment().format('YYYY-MM-DD HH:mm:ss');
        this.reportForm.calendarDate = this.reportDate;
        this.reportForm.fRepStatus = '1';
        addSup(this.reportForm).then(response => {
          this.msgSuccess("上报成功");
          const startOfMonth = moment(this.reportDate).startOf('month').format('YYYY-MM-DD HH:mm:ss');
          // 获取当月结束时间
          const endOfMonth = moment(this.reportDate).endOf('month').format('YYYY-MM-DD HH:mm:ss');
          this.currentMonthReportInfo(startOfMonth,endOfMonth);
          this.getReportInfo();
        });
      }
    },
    currentMonthReportInfo(beginCurrentMonthDate,endCurrentMonthDate){
      reportInfoList({fCompanyInfoId:this.$store.state.user.enterpriseId,beginCurrentMonthDate:beginCurrentMonthDate,endCurrentMonthDate:endCurrentMonthDate}).then(res =>{
        if(res.code == 200 && res.data){
          this.scheduleData = [];
          res.data.forEach(item =>{
            this.scheduleData.push({date:item.calendarDate,content:item.fRepStatus == '1'?"已上报":"未上报"})
          })

        }
      })
    },
    timeChange(val){
      this.queryParams.beginRepDate = val[0];
      this.queryParams.endRepDate = val[1];
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fGasSupId);
    },
    //上传
    handleReport(row) {
      this.$confirm('是否确认上报气量监管编号为"' + row.fGasSupId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return reportSupBalGasSupRecInfo({fGasSupId : row.fGasSupId});
      }).then(() => {
        this.getList();
        this.msgSuccess("上报成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped lang="scss">
  .report-content{
    //border: 1px solid;
    margin: auto;
    max-width: 1200px;
    padding: 30px
  }

  .report-title{
    background: rgb(241, 241, 242);
    padding: 20px 30px;
    border-radius: 3px;
  }

  .report-title .title{
    line-height: 32px;
    font-weight: 500;
  }

  .content{
    background: var(--custom-background-soft);
    box-shadow: 0 3px 14px #a9aaab80;
    border-radius: 3px;
    padding: 32px 48px;
    height: 525px;
  }

  .state-warp{
    border-bottom: 1px solid #d4d4d4;
    height: 54px;
    margin-top: -10px;
    padding-bottom: 8px;
    margin-bottom: 24px;
    color: var(--custom-main-text-color);
  }
  .label-warp{
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(24, 31, 67, .5);
    line-height: 20px;
  }

  .value-warp{
    font-weight: 500;
    font-size: 18px;
    line-height: 25px;
  }

  .flex-between{
    display: flex;
    justify-content: space-between;
  }
</style>
