<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="useAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-super-ux-table
      v-model="tableList"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      index
      pagination
      convenitentOperation
      highlight-current-row
      storage-key="targetTemplateSuperTable"
      @row-dblclick="useDoubleClick"
      @pagination="getList({ ...page, ...params })"
      style="margin: 16px 0 0"
    >
      <!-- 模板 -->
      <template slot="year" slot-scope="scope">
        {{ scope.row.year.substring(0, 4) }}
      </template>
      <ux-table-column fixed="right" title="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="text" :size="size" @click="useDoubleClick(scope.row)"
            >查看</el-button
          >
          <el-button type="text" :size="size" @click="useEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" :size="size" @click="useDel(scope.row)"
            >删除</el-button
          >
        </template>
      </ux-table-column>
    </el-super-ux-table>
    <Detail
      ref="targetTemplateDetail"
      :row="row"
      :size="size"
      :dict="dict"
      :openMode="openMode"
      @success="getList({ ...page, ...params })"
    />
  </el-card>
</template>

<script>
import useColumns from "./columns";
import { dicts } from "./dicts";
import {
  listTargetTemplate,
  delTargetTemplate,
  checkQuote,
} from "@/api/business/spd/starget/targetTemplate";

export default {
  name: "targettemplate",
  dicts: [...dicts],
  components: {
    Detail: () => import("./details/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
  },
  data() {
    const { SearchColumns, TableColumns } = useColumns();
    const params = this.$init.params(SearchColumns);
    const page = this.$init.page();
    return {
      page,
      params,
      SearchColumns,
      TableColumns,
      size: "mini",
      loading: false,
      //列表数据
      tableList: [],
      //编辑页面打开方式，add或edit或see
      openMode: "",
      //行数据
      row: {},
    };
  },
  created() {
    this.getList(this.params, this.page);
  },
  methods: {
    //新增
    useAdd() {
      this.openMode = "add";
      let { setVisible } = this.$refs.targetTemplateDetail;
      setVisible(true);
    },
    //编辑
    async useEdit(row) {
      const { code, data } = await checkQuote(row.id);
      if (code == "200") {
        if (data) {
          this.$modal.msgError("该目标模板存在引用，无法修改！");
          this.visible = false;
          return;
        }
      } else {
        this.$modal.msgError("校验该目标模板是否存在引用异常，无法修改！");
        this.visible = false;
        return;
      }
      this.openMode = "edit";
      this.row = row;
      let { setVisible } = this.$refs.targetTemplateDetail;
      setVisible(true);
    },
    //删除
    async useDel(row) {
      const { code, data } = await checkQuote(row.id);
      if (code == "200") {
        if (data) {
          this.$modal.msgError("该目标模板存在引用，无法删除！");
          this.visible = false;
          return;
        }
      } else {
        this.$modal.msgError("校验该目标模板是否存在引用异常，无法删除！");
        this.visible = false;
        return;
      }
      this.$modal
        .confirm("确认删除选择数据")
        .then(() => {
          delTargetTemplate(row.id).then((res) => {
            if (res.code === 200) {
              this.$modal.msgSuccess("删除成功");
              this.getList(this.params, this.page);
            }
          });
        })
        .catch(() => {});
    },
    //查询列表
    async getList(prop, page) {
      try {
        this.loading = true;
        let params = {
          ...prop,
          ...page,
          params: {
            beginTime: prop["dateRange"] ? prop["dateRange"][0] : "",
            endTime: prop["dateRange"] ? prop["dateRange"][1] : "",
          },
        };
        delete params.dateRange;
        let { code, rows, total } = await listTargetTemplate(params);
        if (code === 200) {
          this.tableList = rows;
          this.page.total = total;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    //双击
    useDoubleClick(row) {
      this.openMode = "see";
      this.row = row;
      let { setVisible } = this.$refs.targetTemplateDetail;
      setVisible(true);
    },

    //重置
    useReset() {
      this.params = this.$init.params(this.SearchColumns);
      this.page = this.$init.page();
      this.getList(this.params, this.page);
    },
  },
};
</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;
}

.el-button-group + .el-button-group {
  margin: 0 0 0 8px;
}
</style>
