<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标名称" prop="goalName">
        <el-input
          v-model="queryParams.goalName"
          placeholder="请输入年度销售目标名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据日期" prop="documentDateRange" style="margin-left: 44px">
        <el-date-picker
          v-model="queryParams.documentDateRange"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          @change="lookValue"
          @keyup.enter.native="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年度" prop="annual">
        <el-select v-model="queryParams.annual" filterable placeholder="请输入年度" clearable>
          <el-option v-for="item1 in annualList" :key="item1.value" :label="item1.label" :value="item1.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="custom">
        <dr-popover-select placeholder="请输入客户" clearable @keyup.enter.native="handleQuery" v-model="queryParams.custom" title="客户" type="CUSTOMER_PARAM" :dataMapping="{
          // customCode: 'code',
          custom: 'name'
        }" :source.sync="queryParams"></dr-popover-select>
      </el-form-item>
      <el-form-item label="销售区域" prop="saleZone">
        <dr-popover-select placeholder="请输入销售区域" clearable @keyup.enter.native="handleQuery" v-model="queryParams.saleZone" title="销售区域" type="ORG_PARAM" :dataMapping="{
          saleZone: 'name'
        }" :source.sync="queryParams"></dr-popover-select>
      </el-form-item>
      <el-form-item label="制单人" prop="creator">
        <dr-popover-select
          placeholder="请输入制单人"
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.creator"
          title="制单人"
          type="CONTACTS_PARAM" :dataMapping="{ creator: 'name' }" :source.sync="queryParams">
        </dr-popover-select>
      </el-form-item>
      <el-form-item label="部门" prop="dept" style="margin-left: -43px">
        <dr-popover-select
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.dept"
          title="制单人"
          type="DEPT_PARAM" :dataMapping="{ dept: 'name' }" :source.sync="queryParams">
        </dr-popover-select>
      </el-form-item>
      <el-form-item style="margin-left: 34px">
        <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="['goal_management:annualSaleGoal: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="['goal_management:annualSaleGoal: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="['goal_management:annualSaleGoal: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="['goal_management:annualSaleGoal:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="annualSaleGoalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column label="编码" align="center" prop="code" width="180" />
      <el-table-column label="目标名称" align="center" prop="goalName" width="180" />
      <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" width="180" />
      <el-table-column label="销售区域" align="center" prop="saleZone" width="180" />
      <el-table-column label="制单人" align="center" prop="creator" />
      <el-table-column label="部门" align="center" prop="dept" width="180" />
      <el-table-column label="目标合计" align="center" prop="goalTotal" />
      <el-table-column label="备注" align="center" prop="notes" width="180" />
      <el-table-column label="单据状态" align="center" prop="documentStatus" />
      <el-table-column label="创建人" align="center" prop="createBy"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy"></el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180px" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goal_management:annualSaleGoal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goal_management:annualSaleGoal:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row.id)"
            v-hasPermi="['goal_management:annualSaleGoal:query']"
          >查看</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="年度销售目标填报--新增" :visible.sync="drawerAdd" :direction="direction" :before-close="handleClose" size="100%">
      <el-form :rules="rules" :model="form" ref="form" size="small" :inline="true" v-show="showSearch" label-width="110px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="编码后端自动生成" disabled />
        </el-form-item>
        <el-form-item label="目标名称" prop="goalName" style="margin-left: 44px">
          <el-input v-model="form.goalName" placeholder="目标名称后端自动生成" disabled />
        </el-form-item>
        <el-form-item label="单据日期" prop="documentDate" style="margin-left: 46px">
          <el-date-picker clearable
                          v-model="form.documentDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择单据日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年度" prop="annual" style="margin-left: 17px">
          <el-select v-model="form.annual" filterable placeholder="请选择年度">
            <el-option
              v-for="item in annualList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户" prop="custom">
          <dr-popover-select v-model="form.custom" title="客户" type="CUSTOMER_PARAM" :dataMapping="{
            customCode: 'code',
            custom: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="销售区域" prop="saleZone">
          <dr-popover-select v-model="form.saleZone" title="销售区域" type="ORG_PARAM" :dataMapping="{
            saleZoneCode: 'code',
            saleZone: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="制单人" prop="creator">
          <dr-popover-select v-model="form.creator" title="制单人" type="CONTACTS_PARAM" :dataMapping="{
            creatorCode: 'code',
            creator: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="部门" prop="dept">
          <dr-popover-select v-model="form.dept" title="部门" type="DEPT_PARAM" :dataMapping="{
            deptCode: 'code',
            dept: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="目标合计" prop="goalTotal">
          <el-input v-model="form.goalTotal" placeholder="请输入目标合计" disabled />
        </el-form-item>
        <el-form-item label="备注" prop="notes" style="margin-left: 46px">
          <el-input v-model="form.notes" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="单据状态" prop="documentStatus" style="margin-left: 46px">
          <el-input v-model="form.documentStatus" placeholder="请输入单据状态" disabled />
        </el-form-item>
        <el-form-item style="margin-left: 115px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="addOrEdit">新增</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer title="年度销售目标填报--修改" :visible.sync="drawEdit" :direction="direction" :before-close="handleClose" size="100%">
      <el-form :rules="rules" :model="form" ref="form" size="small" :inline="true" v-show="showSearch" label-width="110px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="编码后端自动生成" disabled />
        </el-form-item>
        <el-form-item label="目标名称" prop="goalName" style="margin-left: 44px">
          <el-input v-model="form.goalName" placeholder="目标名称后端自动生成" disabled />
        </el-form-item>
        <el-form-item label="单据日期" prop="documentDate" style="margin-left: 46px">
          <el-date-picker clearable
                          v-model="form.documentDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择单据日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年度" prop="annual" style="margin-left: 17px">
          <el-select v-model="form.annual" filterable placeholder="请选择年度">
            <el-option
              v-for="item in annualList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户" prop="custom">
          <dr-popover-select v-model="form.custom" title="客户" type="CUSTOMER_PARAM" :dataMapping="{
            customCode: 'code',
            custom: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="销售组织" prop="saleZone">
          <dr-popover-select v-model="form.saleZone" title="销售组织" type="ORG_PARAM" :dataMapping="{
            saleZoneCode: 'code',
            saleZone: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="制单人" prop="creator">
          <dr-popover-select v-model="form.creator" title="制单人" type="CONTACTS_PARAM" :dataMapping="{
            creatorCode: 'code',
            creator: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="部门" prop="dept">
          <dr-popover-select v-model="form.dept" title="部门" type="DEPT_PARAM" :dataMapping="{
            deptCode: 'code',
            dept: 'name',
          }" :source.sync="form"></dr-popover-select>
        </el-form-item>
        <el-form-item label="目标合计" prop="goalTotal">
          <el-input v-model="form.goalTotal" placeholder="请输入目标合计" disabled />
        </el-form-item>
        <el-form-item label="备注" prop="notes" style="margin-left: 46px">
          <el-input v-model="form.notes" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="单据状态" prop="documentStatus" style="margin-left: 46px">
          <el-input v-model="form.documentStatus" placeholder="请输入单据状态" disabled />
        </el-form-item>
        <el-form-item style="margin-left: 115px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="addOrEdit">修改</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8" type="flex" justify="end">
        <el-col :span="1.5" style="margin-right: 73%"><span>年度销售目标填报</span></el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="openDetailsDrawer"
            v-hasPermi="['goal_management:annualSaleGoalDetails:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDetailsDelete"
            v-hasPermi="['goal_management:annualSaleGoalDetails:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            disabled
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['goal_management:annualSaleGoalDetails:export']"
          >导出</el-button>
        </el-col>
        <!--        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      </el-row>

      <el-table size="small" v-loading="loading" :data="annualSaleGoalDetailsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed />
        <el-table-column label="id唯一标识" align="center" prop="id" />
        <el-table-column label="编码" align="center" prop="code" />
        <el-table-column label="销售组织" align="center" prop="saleOrg" />
        <el-table-column label="销售区域" align="center" prop="saleZone" />
        <el-table-column label="客户" align="center" prop="custom" />
        <el-table-column label="负责人" align="center" prop="creator" />
        <el-table-column label="一级分类编码" align="center" prop="oneLevelClassifyCode" />
        <el-table-column label="一级分类" align="center" prop="oneLevelClassify" />
        <el-table-column label="二级分类编码" align="center" prop="twoLevelClassifyCode" />
        <el-table-column label="二级分类编码" align="center" prop="twoLevelClassify" />
        <el-table-column label="物料编码" align="center" prop="materialCode" />
        <el-table-column label="物料" align="center" prop="material" />
        <el-table-column label="总计" align="center" prop="totalGoal" />
        <el-table-column label="一月目标" align="center" prop="januaryGoal" />
        <el-table-column label="二月目标" align="center" prop="februaryGoal" />
        <el-table-column label="三月目标" align="center" prop="marchGoal" />
        <el-table-column label="四月目标" align="center" prop="aprilGoal" />
        <el-table-column label="五月目标" align="center" prop="mayGoal" />
        <el-table-column label="六月目标" align="center" prop="juneGoal" />
        <el-table-column label="七月目标" align="center" prop="julyGoal" />
        <el-table-column label="八月目标" align="center" prop="augustGoal" />
        <el-table-column label="九月目标" align="center" prop="septemberGoal" />
        <el-table-column label="十月目标" align="center" prop="octoberGoal" />
        <el-table-column label="十一月目标" align="center" prop="novemberGoal" />
        <el-table-column label="十二月目标" align="center" prop="decemberGoal" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateDetails(scope.row)"
              v-hasPermi="['goal_management:annualSaleGoalDetails:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDetailsDelete(scope.row)"
              v-hasPermi="['goal_management:annualSaleGoalDetails:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="totalDetails>0"
        :total="totalDetails"
        :page.sync="queryDetailsParams.pageNum"
        :limit.sync="queryDetailsParams.pageSize"
        @pagination="getDetailList"
      />

      <el-drawer
        title="年度销售目标细节"
        :visible.sync="drawerDetails"
        :direction="direction"
        :append-to-body="true"
        :before-close="handleCloseDetails">
        <el-form ref="formDetails" :model="formDetails" :rules="ruleDetails" label-width="120px">
          <el-form-item label="主表编码" prop="code">
            <el-input v-model="formDetails.code" placeholder="请输入编码" disabled />
          </el-form-item>
          <el-form-item label="销售组织" prop="saleOrg">
            <dr-popover-select
              v-model="formDetails.saleOrg"
              title="销售组织"
              type="ORG_PARAM" :dataMapping="{ saleOrg: 'name' }" :source.sync="formDetails">
            </dr-popover-select>
          </el-form-item>
          <el-form-item label="销售区域" prop="saleZone">
            <dr-popover-select
              v-model="formDetails.saleZone"
              title="销售区域"
              type="ORG_PARAM" :dataMapping="{ saleZone: 'name' }" :source.sync="formDetails">
            </dr-popover-select>
          </el-form-item>
          <el-form-item label="客户" prop="custom">
            <dr-popover-select
              v-model="formDetails.custom"
              title="客户"
              type="CUSTOMER_PARAM" :dataMapping="{ custom: 'name' }" :source.sync="formDetails">
            </dr-popover-select>
          </el-form-item>
          <el-form-item label="负责人" prop="creator">
            <dr-popover-select
              v-model="formDetails.creator"
              title="负责人"
              type="CONTACTS_PARAM" :dataMapping="{ creator: 'name' }" :source.sync="formDetails">
            </dr-popover-select>
          </el-form-item>
          <el-form-item label="一级分类编码" prop="oneLevelClassifyCode">
            <el-input v-model="formDetails.oneLevelClassifyCode" placeholder="请输入一级分类编码" disabled />
          </el-form-item>
          <el-form-item label="一级分类" prop="oneLevelClassify">
            <el-select
              v-model="formDetails.oneLevelClassify" clearable placeholder="请输入一级分类"
              @focus="chooseTreeRefer('MATERIALCLASSIFY_PARAM', false, '一级物料分类')"
              @change="changeDetailsCode" style="width: 100%"
            >
              <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="二级分类编码" prop="twoLevelClassifyCode">
            <el-input v-model="formDetails.twoLevelClassifyCode" placeholder="请输入二级分类编码" disabled />
          </el-form-item>
          <el-form-item label="二级分类编码" prop="twoLevelClassify">
            <el-select v-model="formDetails.twoLevelClassify" placeholder="请输入二级分类编码" clearable
              @focus="chooseTreeRefer('MATERIALCLASSIFY_PARAM', false, '二级物料分类')"
              @change="changeDetailsCode" style="width: 100%"
            >
              <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="物料" prop="material">
            <dr-popover-select
              v-model="formDetails.material"
              title="物料"
              placeholder="请输入物料"
              type="MATERIAL_PARAM" :dataMapping="{ material: 'name', materialCode: 'code' }"
              :source.sync="formDetails">
            </dr-popover-select>
          </el-form-item>
          <el-form-item label="一月目标" prop="januaryGoal">
            <el-input v-model="formDetails.januaryGoal" placeholder="请输入一月目标" />
          </el-form-item>
          <el-form-item label="二月目标" prop="februaryGoal">
            <el-input v-model="formDetails.februaryGoal" placeholder="请输入二月目标" />
          </el-form-item>
          <el-form-item label="三月目标" prop="marchGoal">
            <el-input v-model="formDetails.marchGoal" placeholder="请输入三月目标" />
          </el-form-item>
          <el-form-item label="四月目标" prop="aprilGoal">
            <el-input v-model="formDetails.aprilGoal" placeholder="请输入四月目标" />
          </el-form-item>
          <el-form-item label="五月目标" prop="mayGoal">
            <el-input v-model="formDetails.mayGoal" placeholder="请输入五月目标" />
          </el-form-item>
          <el-form-item label="六月目标" prop="juneGoal">
            <el-input v-model="formDetails.juneGoal" placeholder="请输入六月目标" />
          </el-form-item>
          <el-form-item label="七月目标" prop="julyGoal">
            <el-input v-model="formDetails.julyGoal" placeholder="请输入七月目标" />
          </el-form-item>
          <el-form-item label="八月目标" prop="augustGoal">
            <el-input v-model="formDetails.augustGoal" placeholder="请输入八月目标" />
          </el-form-item>
          <el-form-item label="九月目标" prop="septemberGoal">
            <el-input v-model="formDetails.septemberGoal" placeholder="请输入九月目标" />
          </el-form-item>
          <el-form-item label="十月目标" prop="octoberGoal">
            <el-input v-model="formDetails.octoberGoal" placeholder="请输入十月目标" />
          </el-form-item>
          <el-form-item label="十一月目标" prop="novemberGoal">
            <el-input v-model="formDetails.novemberGoal" placeholder="请输入十一月目标" />
          </el-form-item>
          <el-form-item label="十二月目标" prop="decemberGoal">
            <el-input v-model="formDetails.decemberGoal" placeholder="请输入十二月目标" />
          </el-form-item>
          <el-form-item style="margin-left: 140px">
            <el-button type="primary" @click="submitFormDetails">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
    </el-drawer>

    <el-drawer title="年度销售目标填报--详情" :visible.sync="drawLook" :direction="direction" :before-close="handleClose" size="100%">
      <el-form :model="form" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="编码后端自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="目标名称" prop="goalName">
              <el-input v-model="form.goalName" placeholder="目标名称后端自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="单据日期" prop="documentDate">
              <el-date-picker clearable disabled
                              v-model="form.documentDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择单据日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="年度" prop="annual">
              <el-select v-model="form.annual" filterable placeholder="请选择年度" disabled>
                <el-option
                  v-for="item in annualList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="客户" prop="custom">
              <dr-popover-select disabled v-model="form.custom" title="客户" type="CUSTOMER_PARAM" :dataMapping="{
            customCode: 'code',
            custom: 'name',
          }" :source.sync="form"></dr-popover-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="销售组织" prop="saleZone" style="margin-left: -45px">
              <dr-popover-select disabled v-model="form.saleZone" title="销售组织" type="ORG_PARAM" :dataMapping="{
            saleZoneCode: 'code',
            saleZone: 'name',
          }" :source.sync="form"></dr-popover-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="制单人" prop="creator" style="margin-left: -45px">
              <dr-popover-select disabled v-model="form.creator" title="制单人" type="CONTACTS_PARAM" :dataMapping="{
            creatorCode: 'code',
            creator: 'name',
          }" :source.sync="form"></dr-popover-select>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="部门" prop="dept" style="margin-left: -15px">
              <dr-popover-select disabled v-model="form.dept" title="部门" type="DEPT_PARAM" :dataMapping="{
            deptCode: 'code',
            dept: 'name',
          }" :source.sync="form"></dr-popover-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="目标合计" prop="goalTotal">
              <el-input disabled v-model="form.goalTotal" placeholder="请输入目标合计" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="备注" prop="notes">
              <el-input disabled v-model="form.notes" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="单据状态" prop="documentStatus">
              <el-input disabled v-model="form.documentStatus" placeholder="请输入单据状态" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="annualSaleGoalDetailsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed />
        <el-table-column label="id唯一标识" align="center" prop="id" />
        <el-table-column label="编码" align="center" prop="code" />
        <el-table-column label="销售组织" align="center" prop="saleOrg" />
        <el-table-column label="销售区域" align="center" prop="saleZone" />
        <el-table-column label="客户" align="center" prop="custom" />
        <el-table-column label="负责人" align="center" prop="creator" />
        <el-table-column label="一级分类编码" align="center" prop="oneLevelClassifyCode" />
        <el-table-column label="一级分类" align="center" prop="oneLevelClassify" />
        <el-table-column label="二级分类编码" align="center" prop="twoLevelClassifyCode" />
        <el-table-column label="二级分类编码" align="center" prop="twoLevelClassify" />
        <el-table-column label="物料编码" align="center" prop="materialCode" />
        <el-table-column label="物料" align="center" prop="material" />
        <el-table-column label="一月目标" align="center" prop="januaryGoal" />
        <el-table-column label="二月目标" align="center" prop="februaryGoal" />
        <el-table-column label="三月目标" align="center" prop="marchGoal" />
        <el-table-column label="四月目标" align="center" prop="aprilGoal" />
        <el-table-column label="五月目标" align="center" prop="mayGoal" />
        <el-table-column label="六月目标" align="center" prop="juneGoal" />
        <el-table-column label="七月目标" align="center" prop="julyGoal" />
        <el-table-column label="八月目标" align="center" prop="augustGoal" />
        <el-table-column label="九月目标" align="center" prop="septemberGoal" />
        <el-table-column label="十月目标" align="center" prop="octoberGoal" />
        <el-table-column label="十一月目标" align="center" prop="novemberGoal" />
        <el-table-column label="十二月目标" align="center" prop="decemberGoal" />
      </el-table>
      <pagination
        v-show="totalDetails>0"
        :total="totalDetails"
        :page.sync="queryDetailsParams.pageNum"
        :limit.sync="queryDetailsParams.pageSize"
        @pagination="getDetailList"
      />
    </el-drawer>

    <TreeRefers ref="tree" :single="true" @doSubmit="selectionsToTreeInput"></TreeRefers>
  </div>
</template>

<script>
// 年度目标填报
import { listAnnualSaleGoal, getAnnualSaleGoal, delAnnualSaleGoal, addAnnualSaleGoal, updateAnnualSaleGoal } from "@/api/business/spd/goal_management/annualSaleGoal";
// 年度目标填报明细
import {
  getAnnualSaleGoalDetails,
  delAnnualSaleGoalDetails,
  addAnnualSaleGoalDetails,
  updateAnnualSaleGoalDetails,
  getDetailsByCode
} from "@/api/business/spd/goal_management/annualSaleGoalDetails";
// 树形参照
import TreeRefers from '@/components/Refers/treeRefer.vue'

export default {
  name: "AnnualSaleGoal",
  components: {
    TreeRefers
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年度销售目标表格数据
      annualSaleGoalList: [],
      // 弹出层标题
      title: "年度销售目标明细",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        goalName: null,
        documentDate: null,
        annual: null,
        customCode: null,
        custom: null,
        saleZoneCode: null,
        saleZone: null,
        creatorCode: null,
        creator: null,
        deptCode: null,
        dept: null,
        goalTotal: null,
        notes: null,
        documentStatus: null,
        deleteStatus: 0,
        documentDateRange: null
      },
      queryDetailsParams: {
        pageNum: 1,
        pageSize: 10,
        code: null
      },
      // 表单参数
      form: {
        id: null,
        code: null,
        goalName: null,
        documentDate: null,
        annual: null,
        customCode: null,
        custom: null,
        saleZoneCode: null,
        saleZone: null,
        creatorCode: null,
        creator: null,
        deptCode: null,
        dept: null,
        goalTotal: 0,
        notes: null,
        documentStatus: '开立态',
        deleteStatus: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      },
      formDetails: {
        id: null,
        code: null,
        saleOrg: null,
        saleZone: null,
        custom: null,
        creator: null,
        oneLevelClassifyCode: null,
        oneLevelClassify: null,
        twoLevelClassifyCode: null,
        twoLevelClassify: null,
        materialCode: null,
        material: null,
        januaryGoal: null,
        februaryGoal: null,
        marchGoal: null,
        aprilGoal: null,
        mayGoal: null,
        juneGoal: null,
        julyGoal: null,
        augustGoal: null,
        septemberGoal: null,
        octoberGoal: null,
        novemberGoal: null,
        decemberGoal: null
      },
      // 表单校验
      rules: {
        documentDate: [{required: true, message: '单据日期不能为空', trigger: 'blur'}],
        annual: [{required: true, message: '年度不能为空', trigger: 'blur'}],
        custom: [{required: true, message: '客户不能为空', trigger: 'blur'}],
        saleZone: [{required: true, message: '销售区域不能为空', trigger: 'blur'}],
        creator: [{required: true, message: '制单人不能为空', trigger: 'blur'}],
        dept: [{required: true, message: '部门不能为空', trigger: 'blur'}]
      },
      ruleDetails: {
        saleOrg: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }],
        saleZone: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }],
        custom: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }],
        creator: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }],
        oneLevelClassify: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }],
        twoLevelClassify: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }],
        material: [{ required: true, message: '销售组织不能为空', trigger: 'blur' }]
      },
      // 添加抽屉
      drawerAdd: false,
      // 修改抽屉
      drawEdit: false,
      // 查看抽屉
      drawLook: false,
      // 细节抽屉
      drawerDetails: false,
      // 抽屉打开方向
      direction: 'rtl',
      // 年度列表
      annualList: [
        { value: '2023', label: '2023' },
        { value: '2024', label: '2024' },
        { value: '2025', label: '2025' },
        { value: '2026', label: '2026' },
        { value: '2027', label: '2027' },
        { value: '2028', label: '2028' },
        { value: '2029', label: '2029' },
        { value: '2030', label: '2030' },
        { value: '2031', label: '2031' },
        { value: '2034', label: '2034' },
        { value: '2035', label: '2035' },
        { value: '2036', label: '2036' },
        { value: '2037', label: '2037' },
        { value: '2038', label: '2038' },
        { value: '2039', label: '2039' },
        { value: '2040', label: '2040' },
        { value: '2041', label: '2041' },
        { value: '2042', label: '2042' },
        { value: '2043', label: '2043' },
        { value: '2044', label: '2044' },
        { value: '2045', label: '2045' },
      ],
      // 时间选择器：一周内，一个月内，三个月内
      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]);
          }
        }]
      },
      // 年度销售目标明细
      annualSaleGoalDetailsList: [],
      totalDetails: 0,
      // 参照条件
      referCondition: { type: '', isPage: true, title: '' },
      classList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询年度销售目标列表 */
    getList() {
      this.loading = true;
      listAnnualSaleGoal(this.queryParams).then(response => {
        this.annualSaleGoalList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.annualSaleGoalList);
      });
    },
    // 取消按钮
    cancel() {
      this.drawerDetails = false;
      this.resetDetails();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        goalName: null,
        documentDate: null,
        annual: null,
        customCode: null,
        custom: null,
        saleZoneCode: null,
        saleZone: null,
        creatorCode: null,
        creator: null,
        deptCode: null,
        dept: null,
        goalTotal: 0,
        notes: null,
        documentStatus: '开立态',
        deleteStatus: 0
      };
      this.resetForm("form");
    },
    resetDetails() {
      this.formDetails = {
        id: null,
        code: null,
        saleOrg: null,
        saleZone: null,
        custom: null,
        creator: null,
        oneLevelClassifyCode: null,
        oneLevelClassify: null,
        twoLevelClassifyCode: null,
        twoLevelClassify: null,
        materialCode: null,
        material: null,
        januaryGoal: null,
        februaryGoal: null,
        marchGoal: null,
        aprilGoal: null,
        mayGoal: null,
        juneGoal: null,
        julyGoal: null,
        augustGoal: null,
        septemberGoal: null,
        octoberGoal: null,
        novemberGoal: null,
        decemberGoal: null
      };
      this.resetForm("formDetails");
    },
    /** 搜索按钮操作 */
    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.drawerAdd = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAnnualSaleGoal(id).then(response => {
        this.form = response.data
        this.drawEdit = true
        this.getDetailList()
      });
    },
    handleUpdateDetails(row) {
      this.resetDetails()
      const id = row.id || this.ids
      getAnnualSaleGoalDetails(id).then(response => {
        this.formDetails = response.data
        this.drawerDetails = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAnnualSaleGoal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.drawEdit = false;
              this.getList();
            });
          } else {
            addAnnualSaleGoal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.drawerAdd = false;
              this.getList();
            });
          }
        }
      });
    },
    addOrEdit() {
      console.log(this.form);
      this.submitForm()
    },
    submitFormDetails() {
      console.log(this.formDetails);
      this.$refs["formDetails"].validate(valid => {
        if (valid) {
          if (this.formDetails.id != null) {
            updateAnnualSaleGoalDetails(this.formDetails).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.drawerDetails = false;
              this.getDetailList()
              getAnnualSaleGoal(this.form.id).then(response => {
                this.form = response.data
              })
            });
          } else {
            addAnnualSaleGoalDetails(this.formDetails).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.drawerDetails = false;
              this.getDetailList()
              getAnnualSaleGoal(this.form.id).then(response => {
                this.form = response.data
              })
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除年度销售目标编号为"' + ids + '"的数据项？').then(function() {
        return delAnnualSaleGoal(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleDetailsDelete(row) {
      const ids = row.id || this.ids
      const code = this.form.code
      this.$modal.confirm('是否确认删除年度销售目标编号为"' + ids + '"的数据项？').then(function () {
        return delAnnualSaleGoalDetails(ids, code)
      }).then(() => {
        getAnnualSaleGoal(this.form.id).then(response => {
          this.form = response.data
        })
        this.getDetailList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goal_management/annualSaleGoal/export', {
        ...this.queryParams
      }, `annualSaleGoal_${new Date().getTime()}.xlsx`)
    },
    /** 关闭添加抽屉*/
    handleClose(done) {
      this.$confirm('确认关闭？').then(_ => {
        done();
        this.reset()
        this.getList()
      }).catch(_ => {})
    },
    handleCloseDetails(done) {
      this.$confirm('确认关闭？').then(_ => {
        done();
        this.resetDetails()
        this.getList()
      }).catch(_ => {})
    },
    /** 查看时间范围，范围是个数组*/
    lookValue() {
      console.log(this.documentDateRange);
      console.log(this.queryParams);
    },
    // 打开明细新增or编辑界面
    openDetailsDrawer() {
      this.resetDetails()

      this.formDetails.code = this.form.code
      this.formDetails.saleZone = this.form.saleZone
      this.formDetails.custom = this.form.custom
      this.formDetails.creator = this.form.creator
      this.formDetails.saleOrg = '湖南德荣医疗健康产业有限公司'

      this.getDetailList()
      this.drawerDetails = true
    },
    // 选择树形参照
    chooseTreeRefer(type, isPage, title) {
      this.referCondition.type = type
      this.referCondition.isPage = isPage
      this.referCondition.title = title
      this.$refs.tree.init(this.referCondition)
    },
    // 给参照字段赋值
    selectionsToTreeInput(selection) {
      this.classList.push(selection)
      if (this.referCondition.title === '一级物料分类') {
        if (selection.code.length !== 1) {
          return this.$message.error('请在一级分类里面选择')
        }
        this.formDetails.oneLevelClassifyCode = selection.code
        this.formDetails.oneLevelClassify = selection.name
        this.formDetails.twoLevelClassifyCode = null
        this.formDetails.twoLevelClassify = null
      } else if (this.referCondition.title === '二级物料分类') {
        if (this.formDetails.oneLevelClassifyCode === null || this.formDetails.oneLevelClassifyCode === '') {
          return this.$message.error('请先填写一级分类')
        }
        if (selection.code.length !== 4) {
          return this.$message.error('请在二级分类里面选择')
        }
        if (selection.code[0] !== this.formDetails.oneLevelClassifyCode) {
          return this.$message.error('所选择的二级分类不属于已选的一级分类')
        }
        this.formDetails.twoLevelClassifyCode = selection.code
        this.formDetails.twoLevelClassify = selection.name
      }
    },
    // 当值为null时，设置编码为0
    changeDetailsCode() {
      const one = this.formDetails.oneLevelClassify
      const two = this.formDetails.twoLevelClassify
      if (one.length === 0 || one === '') {
        this.formDetails.oneLevelClassifyCode = null
      }
      if (two.length === 0 || two === '') {
        this.formDetails.twoLevelClassifyCode = null
      }
    },
    // 得到明细数据
    getDetailList() {
      this.loading = true
      this.queryDetailsParams.code = this.form.code
      getDetailsByCode(this.queryDetailsParams).then(response => {
        this.annualSaleGoalDetailsList = response.rows
        this.totalDetails = response.total
        this.loading = false
        console.log(this.annualSaleGoalDetailsList);
      })
    },
    handleLook(id) {
      this.resetDetails()
      getAnnualSaleGoal(id).then(response => {
        this.form = response.data
        this.drawLook = !this.drawLook
        this.getDetailList()
      })
    }
  }
};
</script>
