<template>
  <div id="ActivityList">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <table-list
      rowKey="id"
      ref="tableList"
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      :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="pcost" slot-scope="props">
        <div>税收：40%</div>
        <div>税收：40%</div>
      </template>
      <template slot="fcost" slot-scope="props">
        <div>税收：40</div>
        <div>税收：40</div>
      </template>
      <template slot="status" slot-scope="props">
        <el-checkbox
          @change="changeStatus"
          :checked="!!props.scope.row.status"
        ></el-checkbox>
      </template>
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="handelClick(2, props.scope.row)"
          >复制</el-button
        >
        <el-button type="text" @click="handelClick(1, props.scope.row)"
          >修改</el-button
        >
        <el-button type="text" @click="handelDel(props.scope.row.id)"
          >删除</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize.sync="formData.pageSize"
      :currentPage.sync="formData.pageNum"
      @search="search"
    />
    <dislog-set
      :dialogVisible.sync="dialogVisible"
      :id="id"
      :type="type"
      @search="search"
    ></dislog-set>
  </div>
</template>

<script>
import dislogSet from "./dislogSet.vue";
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
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,
        changeWidth: "160px",
        custom: false,
        backFatherBtn: {
          status: false,
          title: "配置下单填选模板",
        },
      },
      //搜索
      formList: [
        {
          prop: "name",
          placeholder: "请输入模板名称",
        },
      ],
      formData: {
        pageSize: 10,
        pageNum: 1,
      },
      // 表单
      tableSet: [
        {
          label: "模板名称",
          prop: "name",
          hidden: true,
        },
        {
          label: "百分比成本",
          prop: "posterTempName",
          hidden: true,
          scope: "solt",
          soltName: "pcost",
        },
        {
          label: "固定成本成本",
          prop: "posterTempName1",
          hidden: true,
          scope: "solt",
          soltName: "fcost",
        },
        {
          label: "默认状态",
          prop: "startTime",
          hidden: true,
          scope: "solt",
          soltName: "status",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      dialogVisible: false,
      id: "",
      type: 0,
    };
  },
  created() {
    this.search();
  },
  methods: {
    handelClick(type, id) {
      this.dialogVisible = true;
      this.type = type;
      if (type !== 0) {
        this.id = id;
      }
    },
    changeStatus(val) {
      console.log(val);
    },
    batchDel() {
      let len = this.$refs.tableList.allCheckData.length;
      if (!len) {
        return this.$message.warning("请先勾选模板");
      }
      const ids = this.$refs.tableList.allCheckData.map((item) => item.id);
    },
    handelDel(id) {
      this.$confirm("此操作将删除该模板, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.editUser(id).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功");
              this.search();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    search(v) {
      this.loading = true;
      if (v === 2) {
        this.formData = {
          pageSize: 10,
          pageNum: 1,
        };
      }
      var data = JSON.parse(JSON.stringify(this.formData));
      // if (this.formData.classStartTime) {
      //   data.classStartTime = parseInt(data.classStartTime / 1000);
      // }
      // if (this.formData.classEndTime) {
      //   data.classEndTime = parseInt(data.classEndTime / 1000);
      // }
      this.$api
        .distributiontemplist(data)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
  },
};
</script>

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