<!-- 年度销售目标填报 -->
<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)"
    ></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-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="AnnualSaleGoalSuperTable"
      @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="useEdit(scope.row)"
            >修改
          </el-button>
          <el-button
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            :size="size"
            type="text"
            @click="useDelete(scope.row)"
            >删除
          </el-button>
          <el-button
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            :size="size"
            type="text"
            @click="useSubmit(scope.row)"
            >提交
          </el-button>
          <el-button
            v-if="scope.row.status == '1'"
            :size="size"
            type="text"
            @click="useReback(scope.row)"
            >收回
          </el-button>
        </template>
      </ux-table-column>
    </el-super-ux-table>

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

<script>
import { dicts } from "./common/dicts";
import useColumns from "./common/AnnualSaleGoal/columns";
import {
  listAnnualSaleGoal,
  delAnnualSaleGoal,
  submit,
} from "@/api/business/spd/goal_management/annualSaleGoal";

import { rollBack } from "@/api/business/spd/goal_management/publicInterface";

export default {
  name: "AnnualSaleGoal",
  dicts: [...dicts, "oa_templete_id"],
  components: {
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
    AnnualSaleGoalDetails: () =>
      import("./common/AnnualSaleGoal/details/index.vue"),
  },
  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: [],
      rowDetails: {},
      addType: "add",
    };
  },
  created() {
    this.getList(this.params, this.page);
  },
  methods: {
    /** 查询年度销售目标列表 */
    async getList(params, page) {
      try {
        this.loading = true;
        let { code, rows, total } = await listAnnualSaleGoal({
          ...params,
          ...page,
        });
        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);
    },
    //
    useSee(row) {
      this.addType = "see";
      this.rowDetails = row;
      const { setVisible } = this.$refs.AnnualSaleGoalDetails;
      setVisible(true);
    },
    // 新增
    handleAdd() {
      this.addType = "add";
      this.rowDetails = {};
      const { setVisible } = this.$refs.AnnualSaleGoalDetails;
      setVisible(true);
    },
    // 编辑
    useEdit(row) {
      this.addType = "edit";
      this.rowDetails = row;
      const { setVisible } = this.$refs.AnnualSaleGoalDetails;
      setVisible(true);
    },
    // 删除
    useDelete(row) {
      const { id, code } = row;
      this.$modal
        .confirm('是否确认删除年销售目标填报单据编号为"' + code + '"的数据项？')
        .then(async () => {
          try {
            let { code, msg } = await delAnnualSaleGoal(id);
            if (code == 200) {
              this.$notify.success(msg);
              this.getList(this.params, this.page);
            }
          } catch (error) {}
        })
        .catch(() => {});
    },
    // 提交
    async useSubmit(row) {
      this.$modal
        .confirm("是否确认提交？")
        .then(async () => {
          try {
            this.$modal.loading("处理中，请稍后...");
            const { msg, code } = await submit(row.id);
            if (code === 200) {
              this.$modal.notifySuccess(msg);
              row.status = "1";
            }
          } catch (err) {
            console.error(err);
          } finally {
            this.$modal.closeLoading();
          }
        })
        .catch(() => {});
    },
    // 收回
    async useReback(row) {
      try {
        this.$modal.loading("处理中，请稍后...");
        let params = {
          billCode: row.code,
          fdId: row.flowId,
          fdTemplateId: this.dict.type.oa_templete_id.find((item) => {
            return item.label == "销售目标";
          }).value,
          billMaker: row.createBy,
          type: "1",
        };
        let { code, msg } = await rollBack(params);
        if (code == 200) {
          this.$modal.notifySuccess(msg);
          this.getList(this.params, this.page);
        }
      } catch (error) {
      } finally {
        this.$modal.closeLoading();
      }
    },
    // 导出
    handleCommand(command) {
      if (command === "export") {
        // 导出主表
        this.download(
          "goal_management/annualSaleGoal/export",
          {
            ...this.params,
          },
          `年度销售目标填报_${new Date().getTime()}.xlsx`
        );
      }
      if (command === "exportDetails") {
        // 导出明细
        this.download(
          "goal_management/annualSaleGoalDetails/export",
          {
            ...this.params,
          },
          `年度销售目标填报明细_${new Date().getTime()}.xlsx`
        );
      }
    },
  },
};
</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>
