<script>
import dealerApi from "@/api/marketing/dealer-authorization";
import useColumns from "./columns";
import { dicts } from "./dicts";

export default {
  name: "dealerAuthorization",
  dicts: [...dicts],
  components: {
    authprivAdd: () => import("./add/index.vue"),
    // authprivAdd: () => import("./authprivAdd.vue"),
    // authprivSee: () => import("./authprivSee.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
  },

  data() {
    const { TableColumns, SearchColumns } = useColumns();
    const page = this.$init.page();
    const params = this.$init.params(SearchColumns);
    return {
      params: params,
      loading: false,
      size: "mini",
      SearchColumns: SearchColumns,
      TableColumns: TableColumns,
      tableData: [],
      checkedList: [],
      page: page,
    };
  },
  methods: {
    // 查询---列表查询
    async useQuery() {
      this.loading = true;

      try {
        let { privilegedTime } = this.params;
        let params = {
          ...this.params,
          startTime: privilegedTime && privilegedTime[0],
          endTime: privilegedTime && privilegedTime[1],
          ...this.page,
        };
        delete params["privilegedTime"];
        let { code, rows, total } = await dealerApi.list(params);
        if (code == 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (error) {
        console.log(error);
      } finally {
        this.loading = false;
      }
    },
    // 重置
    reset() {
      this.page = this.$init.page();
      this.params = this.$init.params(this.SearchColumns);
      this.useQuery();
    },

    // 选择列表
    handleSelect(selection, row) {
      this.checkedList = [...selection];
    },
    // 新增、编辑授权信息
    handleOpenAddDialog(rows) {
      const { setVisible, setFormData } = this.$refs.addDialogRef;
      setFormData(rows, rows.id ? "edit" : "add");
      setVisible(true);
    },
    // 查看
    handleOpenSeeDialog(rows) {
      const { setVisible, setFormData } = this.$refs.addDialogRef;
      setFormData(rows, "check");
      setVisible(true);
      // const { setVisible, setFormData } = this.$refs.seeDialogRef;
      // setFormData(rows);
      // setVisible(true);
    },
    // 删除
    handleDelDialog(rows) {
      let { id } = rows;

      this.$confirm("此操作将删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            this.loading = true;
            let { code, msg } = await dealerApi.remove({ id });
            if (code == 200) {
              this.useQuery();
            }
          } catch (error) {
            console.log(error);
          } finally {
            this.loading = false;
          }
        })
        .catch(() => {});
    },
    // 提交OA
    async handleSubmit(row) {
      this.$confirm("此操作将提交该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            // name:工号   nickName:名字
            const { name, nickName } = this.$store.state.user;

            const {
              id,
              authorizedProductsCode,
              dealerName,
              authorizedProductsName,
              authorizedAreaName,
            } = row;

            let data = {
              authorizedProductsCode,
              id,
              createPerson: nickName,
              createBy: name,
              date: new Date().Format(),
              dealerName,
              authorizedProductsName,
              authorizedAreaName,
            };

            this.loading = true;
            let { code, msg } = await dealerApi.submitOA(data);
            if (code == 200) {
              this.useQuery();
            }
          } catch (error) {
            console.log(error);
          } finally {
            this.loading = false;
          }
        })
        .catch(() => {});
    },
    // 下载
    async handleDownload(row) {
      try {
        const {
          id,
          authorizedProductsCode,
          dealerName,
          authorizedAreaName,
          authorizedProductsName,
        } = row;

        const data = {
          dealerName,
          authorizedAreaName,
          authorizedProductsName,
          id,
          authorizedProductsCode,
        };

        // await this.download('/dealer/download', data, `授权书${new Date().getTime()}.doc`);
        let file = await dealerApi.download(data);
        let blob = new Blob([file], {
          type: `application/msword`, //word文档为msword,pdf文档为pdf，msexcel 为excel
        });
        let objectUrl = URL.createObjectURL(blob);
        let link = document.createElement("a");
        let fname = `授权书.doc`; //下载文件的名字+后缀名
        link.href = objectUrl;
        link.setAttribute("download", fname);
        document.body.appendChild(link);
        link.click();
      } catch (error) {
        console.log(error);
      } finally {
      }
    },
  },
  created() {
    this.useQuery();
  },
};
</script>

<!-- 经销商授权信息管理--列表 -->
<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <authprivAdd
      ref="addDialogRef"
      :dict="dict"
      :size="size"
      @close="useQuery"
    ></authprivAdd>
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="reset"
      @submit="useQuery"
    ></el-super-search>

    <!-- 操作区 -->
    <el-row :gutter="24" style="margin: 16px 0px">
      <el-col :span="24" style="text-align: right">
        <el-button
          :size="size"
          type="primary"
          @click="handleOpenAddDialog"
          v-hasPermi="['pu:authorization:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-super-ux-table
      v-model="tableData"
      :dict="dict"
      :columns="TableColumns"
      :size="size"
      pagination
      index
      checkbox
      convenitentOperation
      :page="page"
      storage-key="DealerAuthorizationSuperTable"
      @pagination="useQuery"
      @row-dblclick="handleOpenSeeDialog"
      @select="handleSelect"
    >
      <ux-table-column fixed="right" title="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            :size="size"
            @click="handleOpenAddDialog(scope.row)"
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            v-hasPermi="['pu:authorization:edit']"
            >编辑</el-button
          >
          <el-button
            type="text"
            :size="size"
            @click="handleDelDialog(scope.row)"
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            v-hasPermi="['pu:authorization:remove']"
            >删除</el-button
          >
          <el-button
            type="text"
            :size="size"
            v-if="scope.row.status == '0' || scope.row.status == '3'"
            @click="handleSubmit(scope.row)"
            >提交</el-button
          >
          <el-button
            type="text"
            :size="size"
            v-if="scope.row.status == '2'"
            @click="handleDownload(scope.row)"
            >下载授权书</el-button
          >
        </template>
      </ux-table-column>
    </el-super-ux-table>
  </el-card>
</template>

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