<!-- 年销售目标合并 -->
<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="getList(params, page)"
    >
      <template slot="goalCategory" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          @clear="goalCategoryClear(scope.row)"
        >
          <el-option
            v-for="item in dict.type[scope.attr.dictName]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </component>
      </template>
      <template slot="saleZone" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="params.goalCategory !== '销售区域'"
        >
        </component>
      </template>
      <template slot="oneLevelClassify" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="params.goalCategory !== '一级分类'"
          @change="
            (prop, columns) => changeOneLevel(scope.row, { prop, columns })
          "
        >
        </component>
      </template>
      <template slot="twoLevelClassify" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :disabled="params.goalCategory !== '二级分类'"
          @change="
            (prop, columns) => changeTwoLevel(scope.row, { prop, columns })
          "
        >
        </component>
      </template>
    </el-super-search>

    <el-row
      :gutter="10"
      class="mb10"
      type="flex"
      justify="end"
      style="margin-top: 20px"
    >
      <el-col :span="1.5">
        <el-button type="primary" :size="size" @click="handleAdd"
          >新 增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown @command="handleCommand">
          <el-button :size="size">
            导出<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-item command="exportZoneSum"
              >导出区域目标汇总</el-dropdown-item
            >
            <el-dropdown-item command="exportCustomSum"
              >导出客户目标汇总</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>

    <el-super-ux-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      index
      pagination
      convenitentOperation
      highlight-current-row
      storage-key="AnnualSaleGoalMergeSuperTable"
      @row-dblclick="useSee"
      @pagination="getList(params, page)"
      style="margin: 16px 0 0"
    >
      <ux-table-column fixed="right" title="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button :size="size" type="text" @click="useSee(scope.row)"
            >查看
          </el-button>
          <el-button
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            :size="size"
            type="text"
            @click="handleUpdate(scope.row)"
            >修改
          </el-button>
          <el-button
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            :size="size"
            type="text"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
          <el-button
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            :size="size"
            type="text"
            @click="handleSubmit(scope.row)"
            >提交</el-button
          >
          <el-button
            v-if="scope.row.status == '1' && scope.row.flowId"
            :size="size"
            type="text"
            @click="handleReback(scope.row)"
            >收回
          </el-button>
        </template>
      </ux-table-column>
    </el-super-ux-table>

    <asgm-details
      ref="AnnualSaleGoalMergeDetails"
      :dict="dict"
      :size="size"
      :data="rowDetails"
      :addType="addType"
      @refresh="getList(params, page)"
    ></asgm-details>
  </el-card>
</template>

<script>
import { dicts } from "./common/dicts";
import useColumns from "./common/AnnualSaleGoalMerge/columns";
import { rollBack } from "@/api/business/spd/goal_management/publicInterface";
import {
  submit,
  delAnnualSaleGoalMerge,
  listAnnualSaleGoalMerge,
} from "@/api/business/spd/goal_management/annualSaleGoalMerge";
export default {
  name: "AnnualSaleGoalMerge",
  components: {
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
    AsgmDetails: () => import("./common/AnnualSaleGoalMerge/details/index.vue"),
    ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
    ElPopoverTreeSelect: () =>
      import("@/components/popover-tree-select/index.vue"),
  },
  dicts: [...dicts, "oa_templete_id"],
  data() {
    const { SearchColumns, TableColumns } = useColumns();
    const params = this.$init.params(SearchColumns);
    const page = this.$init.page();
    return {
      page,
      params,
      TableColumns,
      SearchColumns,
      size: "mini",
      loading: false,
      tableData: [],
      addType: "add",
      rowDetails: {},
    };
  },
  created() {
    this.getList(this.params, this.page);
  },
  methods: {
    async getList(params, page) {
      try {
        this.loading = true;
        let queryParams = {
          ...params,
          ...page,
          params: {
            beginTime: params.documentDateRange
              ? params.documentDateRange[0]
              : null,
            endTime: params.documentDateRange
              ? params.documentDateRange[1]
              : null,
          },
        };
        delete queryParams.documentDateRange;
        let { code, rows, total } = await listAnnualSaleGoalMerge({
          ...queryParams,
        });
        if (code == 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    // 重置
    useReset() {
      this.params = this.$init.params(this.SearchColumns);
      this.page = this.$init.page();
      this.getList(this.params, this.page);
    },
    // 新增
    handleAdd() {
      this.addType = "add";
      const { setVisible } = this.$refs.AnnualSaleGoalMergeDetails;
      setVisible(true);
    },
    // 导出
    handleCommand(command) {
      let url = "";
      let fileName = "";
      switch (command) {
        case "export":
          url = "goal_management/annualSaleGoalMerge/export";
          fileName = "annualSaleGoalMerge";
          break;
        case "exportDetails":
          url = "goal_management/annualSaleMergeDetails/export";
          fileName = "annualSaleMergeDetails";
          break;
        case "exportZoneSum":
          url = "goal_management/annualSaleMergeDetails/exportZoneSum";
          fileName = "zoneSum";
          break;
        case "exportCustomSum":
          url = "goal_management/annualSaleMergeDetails/exportCustomSum";
          fileName = "customSum";
          break;
        default:
          break;
      }
      this.download(
        url,
        {
          ...this.params,
          ...this.page,
        },
        `${fileName}_${new Date().getTime()}.xlsx`
      );
    },
    // 查看
    useSee(row) {
      this.addType = "see";
      this.rowDetails = row;
      const { setVisible } = this.$refs.AnnualSaleGoalMergeDetails;
      setVisible(true);
    },
    // 修改
    handleUpdate(row) {
      this.addType = "edit";
      this.rowDetails = row;
      const { setVisible } = this.$refs.AnnualSaleGoalMergeDetails;
      setVisible(true);
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(async () => {
          try {
            this.$modal.loading("处理中，请稍后...");
            console.log(row.id, "id");
            const { code, msg } = await delAnnualSaleGoalMerge(row.id);
            if (code == 200) {
              this.$modal.notifySuccess(msg);
              this.getList(this.params, this.page);
            }
          } catch (error) {
          } finally {
            this.$modal.closeLoading();
          }
        })
        .catch(() => {});
    },
    // 收回
    async handleReback(row) {
      this.$modal
        .confirm("是否确认收回？")
        .then(async () => {
          try {
            this.$modal.loading("处理中，请稍后...");
            const { code: billCode, flowId: fdId, createBy: billMaker } = row;
            let params = {
              fdId,
              billCode,
              billMaker,
              type: "2",
              fdTemplateId: this.dict.type.oa_templete_id.find((item) => {
                return item.label == "销售目标";
              }).value,
            };

            const { code, msg } = await rollBack(params);
            if (code == 200) {
              this.$modal.notifySuccess(msg);
              this.getList(this.params, this.page);
            }
          } catch (error) {
          } finally {
            this.$modal.closeLoading();
          }
        })
        .catch(() => {});
    },
    // 提交
    handleSubmit(row) {
      this.$modal
        .confirm("是否确认提交？")
        .then(async () => {
          try {
            this.$modal.loading("处理中，请稍后...");
            const { code, msg } = await submit(row.id);
            if (code == 200) {
              this.$modal.notifySuccess(msg);
              this.getList(this.params, this.page);
            }
          } catch (error) {
          } finally {
            this.$modal.closeLoading();
            row.status = '0';
          }
        })
        .catch(() => {});
    },
    // 目标类型
    goalCategoryClear(prop) {
      prop.oneLevelClassify = null;
      prop.oneLevelClassifyCode = null;
      prop.twoLevelClassify = null;
      prop.twoLevelClassifyCode = null;
      prop.saleZone = null;
      prop.saleZoneCode = null;
    },
    // 一级分类
    changeOneLevel(row, { prop, columns }) {
      const select = prop[0];
      if (select.code.length > 2) {
        row.oneLevelClassify = null;
        row.oneLevelClassifyCode = null;
        return this.$message.warning("请选择一级分类");
      }
    },
    // 二级分类
    changeTwoLevel(row, { prop, columns }) {
      const select = prop[0];
      if (
        !(select.code.lastIndexOf("-") == 3 || select.code.indexOf("90") == 0)
      ) {
        row.twoLevelClassify = null;
        row.twoLevelClassifyCode = null;
        return this.$message.warning("请选择二级分类");
      }
    },
  },
};
</script>

<style scoped lang="scss">
.el-card {
  width: calc(100% - 32px);
  height: calc(100vh - 32px);
  margin: 16px;
  padding: 16px;
  border-radius: 8px;
  overflow-y: auto;
  overflow-x: hidden;
}
</style>
