<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="编码" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="目标名称" prop="goalName">
            <el-input
              v-model="queryParams.goalName"
              placeholder="请输入目标名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单据日期" prop="documentDate">
            <el-date-picker
              v-model="documentDateRange"
              @change="setBeginAndEnd"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <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-col>
      </el-row>
    </el-form>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="年度" prop="annual">
            <el-date-picker
              v-model="queryParams.annual"
              type="year"
              value-format="yyyy"
              placeholder="选择年度"
              clearable
              @keyup.enter.native="handleQuery">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户" prop="custom">
            <el-popover-select-v2 v-model="queryParams.custom" title="客户" valueKey="name"
                                  referName="CUSTOMER_PARAM"
                                  :dataMapping="{ customCode: 'code', custom: 'name'}"
                                  :source.sync="queryParams" placeholder="请输入客户" @keyup.enter.native="handleQuery">
            </el-popover-select-v2>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="制单人" prop="creator">
            <el-popover-select-v2 v-model="queryParams.creator" title="制单人" valueKey="name"
                                  referName="CONTACTS_PARAM"
                                  :dataMapping="{ creatorCode: 'code', creator: 'name'}"
                                  :source.sync="queryParams" placeholder="请输入制单人" @keyup.enter.native="handleQuery">
            </el-popover-select-v2>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="销售区域" prop="saleZone">
            <el-popover-select-v2 v-model="queryParams.saleZone" title="销售区域" valueKey="name"
                                  referName="MK_SALESAREA_PARAM"
                                  :dataMapping="{ saleZoneCode: 'code', saleZone: 'name'}"
                                  :source.sync="queryParams" placeholder="请输入销售区域" @keyup.enter.native="handleQuery">
            </el-popover-select-v2>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="部门" prop="dept">
            <el-popover-select-v2 v-model="queryParams.dept" title="部门" valueKey="name"
                                  referName="DEPT_PARAM"
                                  :dataMapping="{ deptCode: 'code', dept: 'name'}"
                                  :source.sync="queryParams" placeholder="请输入部门" @keyup.enter.native="handleQuery">
            </el-popover-select-v2>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8" style="float: right">
      <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="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-grape"
          size="mini"
          :disabled="multiple"
        >提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown @command="handleCommand">
          <el-button type="primary" size="mini" plain icon="el-icon-upload">
            导入<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="importModel">导入模板</el-dropdown-item>
            <el-dropdown-item command="import">导入</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown @command="handleCommand">
          <el-button type="warning" plain icon="el-icon-download" size="mini">
            导出<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="export">导出</el-dropdown-item>
            <el-dropdown-item command="exportDetails">导出明细</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="monthReturnGoalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编码" align="center" prop="code"/>
      <el-table-column label="目标名称" align="center" prop="goalName"/>
      <el-table-column label="单据日期" align="center" prop="documentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.documentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年度" align="center" prop="annual"/>
      <el-table-column label="客户" align="center" prop="custom"/>
      <el-table-column label="制单人" align="center" prop="creator"/>
      <el-table-column label="销售区域" align="center" prop="saleZone"/>
      <el-table-column label="部门" align="center" prop="dept"/>
      <el-table-column label="目标合计" align="center" prop="goalSum"/>
      <el-table-column label="备注" align="center" prop="notes"/>
      <el-table-column label="单据状态" align="center" prop="documentStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleCopy(scope.row.id)"
          >复制
          </el-button>
          <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>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer :title="title" :visible.sync="open" direction="rtl" :before-close="handleClose" size="100%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="编码后端自动生成" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="目标名称" prop="goalName">
              <el-input v-model="form.goalName" placeholder="目标名称后端自动生成" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单据日期" prop="documentDate">
              <el-date-picker clearable
                              v-model="form.documentDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择单据日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="年度" prop="annual">
              <el-date-picker
                v-model="form.annual"
                type="year"
                value-format="yyyy"
                placeholder="选择年度">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="客户" prop="custom">
              <el-popover-select-v2 v-model="form.custom" title="客户" valueKey="name"
                                    referName="CUSTOMER_PARAM"
                                    :dataMapping="{ customCode: 'code', custom: 'name'}"
                                    :source.sync="form" placeholder="请输入客户">
              </el-popover-select-v2>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="制单人" prop="creator">
              <el-popover-select-v2 v-model="form.creator" title="制单人" valueKey="name"
                                    referName="CONTACTS_PARAM"
                                    :dataMapping="{ creatorCode: 'code', creator: 'name'}"
                                    :source.sync="form" placeholder="请输入制单人">
              </el-popover-select-v2>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="销售区域" prop="saleZone">
              <el-popover-select-v2 v-model="form.saleZone" title="销售区域" valueKey="name"
                                    referName="MK_SALESAREA_PARAM"
                                    :dataMapping="{ saleZoneCode: 'code', saleZone: 'name'}"
                                    :source.sync="form" placeholder="请输入销售区域">
              </el-popover-select-v2>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门" prop="dept">
              <el-popover-select-v2 v-model="form.dept" title="部门" valueKey="name"
                                    referName="DEPT_PARAM"
                                    :dataMapping="{ deptCode: 'code', dept: 'name'}"
                                    :source.sync="form" placeholder="请输入部门">
              </el-popover-select-v2>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="目标合计" prop="goalSum">
              <el-input v-model="form.goalSum" placeholder="目标合计自动计算" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="备注" prop="notes">
              <el-input v-model="form.notes" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单据状态" prop="documentStatus">
              <el-input v-model="form.documentStatus" placeholder="未提交" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div>
        <el-row :gutter="10" class="mb8" style="margin-left: 94%">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddDetails">增行</el-button>
          </el-col>
        </el-row>
        <el-tabs v-model="activeName">
          <el-tab-pane label="月回款目标明细" name="monthReturnGoalDetails">
            <el-table v-loading="loading" :data="monthReturnGoalDetailsList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" fixed />
              <el-table-column label="销售组织" align="center" prop="saleOrg" width="180" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-popover-select-v2 v-model="monthReturnGoalDetailsList[scope.$index].saleOrg" title="销售组织" valueKey="name"
                                        referName="ORG_PARAM"
                                        :dataMapping="{saleOrg: 'name'}"
                                        :source.sync="monthReturnGoalDetailsList[scope.$index]" placeholder="请输入销售组织">
                  </el-popover-select-v2>
                </template>
              </el-table-column>
              <el-table-column label="销售区域" align="center" prop="saleZone" width="180" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-popover-select-v2 v-model="monthReturnGoalDetailsList[scope.$index].saleZone" title="销售区域" valueKey="name"
                                        referName="MK_SALESAREA_PARAM"
                                        :dataMapping="{saleZone: 'name'}"
                                        :source.sync="monthReturnGoalDetailsList[scope.$index]" placeholder="请输入销售区域">
                  </el-popover-select-v2>
                </template>
              </el-table-column>
              <el-table-column label="客户" align="center" prop="custom" width="180" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-popover-select-v2 v-model="monthReturnGoalDetailsList[scope.$index].custom" title="客户" valueKey="name"
                                        referName="CUSTOMER_PARAM"
                                        :dataMapping="{custom: 'name'}"
                                        :source.sync="monthReturnGoalDetailsList[scope.$index]" placeholder="请输入客户">
                  </el-popover-select-v2>
                </template>
              </el-table-column>
              <el-table-column label="部门" align="center" prop="dept" width="180" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-popover-select-v2 v-model="monthReturnGoalDetailsList[scope.$index].dept" title="部门" valueKey="name"
                                        referName="DEPT_PARAM"
                                        :dataMapping="{dept: 'name'}"
                                        :source.sync="monthReturnGoalDetailsList[scope.$index]" placeholder="请输入客户">
                  </el-popover-select-v2>
                </template>
              </el-table-column>
              <el-table-column label="制单人" align="center" prop="creator" width="180" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-popover-select-v2 v-model="monthReturnGoalDetailsList[scope.$index].creator" title="负责人" valueKey="name"
                                        referName="CONTACTS_PARAM"
                                        :dataMapping="{creator: 'name'}"
                                        :source.sync="monthReturnGoalDetailsList[scope.$index]" placeholder="请输入负责人">
                  </el-popover-select-v2>
                </template>
              </el-table-column>
              <el-table-column label="月份" align="center" prop="monthly" width="250" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-date-picker v-model="monthReturnGoalDetailsList[scope.$index].monthly" value-format="yyyy-MM" type="month" placeholder="选择月份">
                  </el-date-picker>
                </template>
              </el-table-column>
              <el-table-column label="目标值" align="center" prop="goalSum" width="220" :render-header="addRedStar">
                <template slot-scope="scope">
                  <el-input-number @change="computeTotal" v-model="monthReturnGoalDetailsList[scope.$index].goalSum" :precision="2" :step="1" :min="0"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px" fixed="right">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDeleteDetails(scope.$index, scope.row)"
                  >删除</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleCopyDetails(scope.row)"
                  >复制</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
        <div slot="footer" class="dialog-footer" style="margin-left: 88%; margin-top: 1%">
          <el-button type="primary" @click="submitForm" size="medium">确 定</el-button>
          <el-button @click="cancel" size="medium">返 回</el-button>
        </div>
      </div>
    </el-drawer>

  </div>
</template>

<script>

import {
  listMonthReturnGoal,
  getMonthReturnGoal,
  addMonthReturnGoal,
  updateMonthReturnGoal,
  delMonthReturnGoal
} from "@/api/business/spd/goal_management/monthReturnGoal";
import {
  getMonthReturnGoalDetails,
  delMonthReturnGoalDetails
} from "@/api/business/spd/goal_management/monthReturnGoalDetails"

// 树形参照
import TreeRefers from '@/components/Refers/treeRefer.vue'
import ElPopoverSelectV2 from "@/components/popover-select-v2"

export default {
  name: "MonthReturnGoal",
  components: {
    TreeRefers, ElPopoverSelectV2
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 月回款目标填报表格数据
      monthReturnGoalList: [],
      monthReturnGoalDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        goalName: null,
        documentDate: null,
        annual: null,
        customCode: null,
        custom: null,
        creatorCode: null,
        creator: null,
        saleZoneCode: null,
        saleZone: null,
        deptCode: null,
        dept: null,
        goalSum: null,
        notes: null,
        documentStatus: null,
        params: { beginTime: null, endTime: null,}
      },
      // 表单参数
      form: {
        id: null,
        code: null,
        goalName: null,
        documentDate: null,
        annual: null,
        customCode: null,
        custom: null,
        creatorCode: null,
        creator: null,
        saleZoneCode: null,
        saleZone: null,
        deptCode: null,
        dept: null,
        goalSum: null,
        notes: null,
        documentStatus: null,
        monthReturnGoalDetailsList: null,
      },
      formDetails: {
        id: null,
        code: null,
        saleOrg: null,
        saleZone: null,
        custom: null,
        dept: null,
        creator: null,
        monthly: null,
        goalSum: null
      },
      // 表单校验
      rules: {
        documentDate: [{ required: true, message: '单据日期不能为空', trigger: 'blur' }],
        annual: [{ required: true, message: '年度不能为空', trigger: 'blur' }],
        custom: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
        creator: [{ required: true, message: '制单人不能为空', trigger: 'blur' }],
        saleZone: [{ required: true, message: '销售区域不能为空', trigger: 'blur' }],
        dept: [{ required: true, message: '部门不能为空', trigger: 'blur' }],
      },
      // 子表导航名
      activeName: 'monthReturnGoalDetails',
      documentDateRange: null,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询月销售目标填报列表 */
    getList() {
      this.loading = true;
      listMonthReturnGoal(this.queryParams).then(response => {
        this.monthReturnGoalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListDetails() {
      this.loading = true
      getMonthReturnGoalDetails(this.form.id).then(response => {
        this.monthReturnGoalDetailsList = response.data
        this.computeTotal()
        this.form.monthReturnGoalDetailsList = this.monthReturnGoalDetailsList
        updateMonthReturnGoal(this.form).then(response => {
        })
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        goalName: null,
        documentDate: null,
        annual: null,
        customCode: null,
        custom: null,
        creatorCode: null,
        creator: null,
        saleZoneCode: null,
        saleZone: null,
        deptCode: null,
        dept: null,
        goalSum: null,
        notes: null,
        documentStatus: null,
        monthReturnGoalDetailsList: 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.monthReturnGoalDetailsList = []
      this.open = true;
      this.title = "添加--月回款目标填报";
      this.form.documentDate = new Date().getFullYear().toString() + '-' + (new Date().getMonth() + 1).toString().padStart(2, '0') + '-' + new Date().getDate().toString().padStart(2, '0')
      this.form.annual = new Date().getFullYear().toString()
      this.form.creator = this.$store.state.user.nickName
      this.form.dept = this.$store.state.user.deptName
    },
    handleAddDetails() {
      let list = {
        id: null,
        code: null,
        saleOrg: this.$store.state.user.orgName,
        saleZone: this.form.saleZone,
        custom: this.form.custom,
        dept: this.form.dept,
        creator: this.form.creator,
        monthly: new Date().getFullYear().toString() + '-' + (new Date().getMonth() + 1).toString().padStart(2, '0'),
        goalSum: 0
      }
      this.monthReturnGoalDetailsList.push(list)
      this.computeTotal()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMonthReturnGoal(id).then(response => {
        this.form = response.data;
        console.log(this.form);
        this.monthReturnGoalDetailsList = JSON.parse(JSON.stringify(this.form.monthReturnGoalDetailsList))
        this.open = true;
        this.title = "修改--月回款目标填报";
      });
    },
    // 复制按钮操作
    handleCopy(id) {
      this.reset();
      getMonthReturnGoal(id).then(response => {
        this.form = response.data;
        this.form.id = null
        this.form.code = null
        this.form.documentDate = new Date().getFullYear().toString() + '-' + (new Date().getMonth() + 1).toString().padStart(2, '0') + '-' + new Date().getDate().toString().padStart(2, '0')
        this.form.annual = new Date().getFullYear().toString()
        this.monthReturnGoalDetailsList = JSON.parse(JSON.stringify(this.form.monthReturnGoalDetailsList))
        for (const element of this.monthReturnGoalDetailsList) {
          element.id = null
          element.code = null
        }
        this.open = true;
        this.title = "新增--月销售目标填报";
      })
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.justiceDetailsList()) {
        return this.$message.error('子表中有必填字段未填')
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.monthReturnGoalDetailsList = JSON.parse(JSON.stringify(this.monthReturnGoalDetailsList))
            updateMonthReturnGoal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.documentStatus = '未提交'
            this.form.monthReturnGoalDetailsList = JSON.parse(JSON.stringify(this.monthReturnGoalDetailsList))
            console.log(this.form);
            addMonthReturnGoal(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 delMonthReturnGoal(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleDeleteDetails(index, row) {
      if (this.form.id === null) {
        this.monthReturnGoalDetailsList.splice(index, 1)
        this.computeTotal()
      } else {
        if (row.id !== null) {
          this.$modal.confirm('是否确认删除月销售目标填报明细编号为"' + row.id + '"的数据项？').then(function () {
            return delMonthReturnGoalDetails(row.id);
          }).then(() => {
            this.getListDetails();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {
          });
        } else {
          this.monthReturnGoalDetailsList.splice(index, 1)
          this.$message.success('删除成功')
          this.computeTotal()
        }
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mk/monthReturnGoal/export', {
        ...this.queryParams
      }, `monthReturnGoal_${new Date().getTime()}.xlsx`)
    },
    handleExportDetails() {
      this.download('mk/monthReturnGoalDetails/export', {
        ...this.queryParams
      }, `monthReturnGoalDetails_${new Date().getTime()}.xlsx`)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
    },
    // 计算主表合计
    computeTotal() {
      let list = this.monthReturnGoalDetailsList
      let sum = 0
      for (const listElement of list) {
        sum = (sum * 1000000 + listElement.goalSum * 1000000) / 1000000
      }
      this.form.goalSum = sum
    },
    // 复制明细
    handleCopyDetails(row) {
      let list = {
        id: null,
        code: row.code,
        saleOrg: row.saleOrg,
        saleZone: row.saleZone,
        custom: row.custom,
        dept: row.dept,
        creator: row.creator,
        monthly: row.monthly,
        goalSum: row.goalSum
      }
      this.monthReturnGoalDetailsList.push(list)
      this.computeTotal()
    },
    setBeginAndEnd() {
      let array = this.documentDateRange
      if (array !== null) {
        this.queryParams.params.beginTime = array[0]
        this.queryParams.params.endTime = array[1]
      } else {
        this.queryParams.beginTime = null
        this.queryParams.endTime = null
      }
    },
    // 给table添加必填项
    addRedStar(h, { column }) {
      return [
        h('span', { style: 'color: #F56C6C' }, '*'),
        h('span', '' + column.label)
      ]
    },
    // 判断子表的字段是否都填了
    justiceDetailsList() {
      const arr = JSON.parse(JSON.stringify(this.monthReturnGoalDetailsList))
      for (const element of arr) {
        if (element.saleOrg === null || element.saleZone === null || element.custom === null || element.dept === null || element.creator === null || element.monthly === null || element.goalSum === null) {
          return false
        }
      }
      return true
    },
    handleCommand(command) {
      // 执行对应的功能
      if (command === 'importModel') {
        // 执行选项1的功能
        console.log('导入模板');
      } else if (command === 'import') {
        // 执行选项2的功能
        console.log('导入');
      } else if (command === 'export') {
        console.log('导出主表');
        this.handleExport()
      } else if (command === 'exportDetails') {
        console.log('导出明细');
        this.handleExportDetails()
      }
    }
  }
}
</script>

<style scoped>

</style>
