<template>
  <div id="ActivityList">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <table-list
      rowKey="tpId"
      ref="tableList"
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @sortChange="sortChange"
      :loading="loading"
    >
      <template slot="customize">
        <el-button type="primary" @click="handelClick(0)"> 新增 </el-button>
        <el-button @click="batchDel" type="warning"> 批量删除 </el-button>
      </template>
      <template slot="status" slot-scope="props">
        <el-checkbox
          :true-label="1"
          :false-label="0"
          @change="(val) => changeStatus(props.scope.row, val)"
          v-model="props.scope.row.defaultStatus"
        >
        </el-checkbox>
      </template>
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="handelClick(2, props.scope.row.tpId)"
          >复制</el-button
        >
        <el-button type="text" @click="handelClick(1, props.scope.row.tpId)"
          >修改</el-button
        >
        <el-button type="text" @click="handelDel(props.scope.row.tpId)"
          >删除</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize.sync="formData.pageSize"
      :currentPage.sync="formData.pageNum"
      @search="search"
    />
    <dislog-set
      :dialogVisible.sync="dialogVisible"
      :tpId="tpId"
      :type="type"
      @search="search"
    ></dislog-set>
  </div>
</template>

<script>
import dislogSet from "./dislogOrganSet.vue";
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
import {
  costList,
  editDefaultStatus,
  editCost,
  batchCost,
} from "@/api/financed/index";
export default {
  name: "Cost",
  components: { searchBoxNew, tableList, pagination, dislogSet },
  data() {
    return {
      loading: false,
      navText: {
        title: "成本模板",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: true,
        openCheckMore: true,
        custom: false,
      },
      formList: [
        {
          prop: "tenantId",
          placeholder: "关联机构",
          scope: "systemtenantlist",
        },
        {
          prop: "tpName",
          placeholder: "请输入模板名称",
        },
      ],
      formData: {},
      tableSet: [
        {
          label: "模板名称",
          prop: "tpName",
          hidden: true,
        },
        {
          label: "关联机构",
          prop: "tenantName",
          hidden: true,
          sort: "custom",
        },
        {
          label: "默认状态",
          prop: "startTime",
          hidden: true,
          scope: "solt",
          soltName: "status",
        },
      ],
      tableData: [],
      total: 0,
      dialogVisible: false,
      tpId: "",
      type: 0,
    };
  },
  created() {
    this.search(2);
  },
  methods: {
    handelClick(type, tpId) {
      this.dialogVisible = true;
      this.type = type;
      this.tpId = tpId;
    },
    changeStatus({ tpId, tenantId }, defaultStatus) {
      editDefaultStatus({ tpId, tenantId, defaultStatus }).then((res) => {
        this.$message.success("状态修改成功");
        this.search();
      });
    },
    batchDel() {
      const ids = this.$refs.tableList.allCheckData.map((item) => item.tpId);
      if (!ids.length) {
        return this.$message.warning("请先勾选模板");
      }
      this.handelDel(ids, 1);
    },
    handelDel(tpId, type = 0) {
      this.$confirm(
        `此操作将删除${type ? "批量" : "该"}模板, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          if (type) {
            batchCost({ ids: tpId }).then((res) => {
              this.$message.success("删除成功");
              this.$refs.tableList.clearMoreActive();
              this.search();
            });
          } else {
            editCost({ tpId, status: -1 }).then((res) => {
              this.$message.success("删除成功");
              this.$refs.tableList.clearMoreActive();
              this.search();
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    search(v) {
      this.loading = true;
      if (v === 2) {
        this.formData = {
          pageSize: 10,
          pageNum: 1,
          tenantSort: undefined,
        };
        this.$nextTick(() => {
          this.$refs.tableList.clearMoreActive();
        });
      }
      costList(this.formData)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    sortChange(data) {
      this.formData.tenantSort = data.order
        ? data.order == "ascending"
          ? 1
          : 0
        : undefined;
      this.formData.pageNum = 1;
      this.search();
    },
    init() {
      this.search(2);
    },
  },
};
</script>

<style lang="scss" scoped></style>
