import * as API from "api/virtual/refunds";
export default {
  name: "Companies",
  components: {
    UiEdit: resolve => require(["views/virtual/refunds/edit"], resolve)
  },
  data() {
    return {
      params: {
        sn: "",
        carrier_operator: "",
        company_name: "",
        package_name: "",
        time: [],
        refunded_time: []
      },
      type: 0,
      trashed: null,
      list_data: null,
      editObj: {
        show: false,
        data: null
      },
      search: {
        show: false
      },
      table_titles: [
        {
          title: "ID",
          key: "id",
          width: 120
        },
        {
          title: "SIM",
          key: "sim",
          width: 150
        },
        {
          title: "订单编号",
          key: "order_sn",
          minWidth: 210
        },
        {
          title: "原企业名",
          key: "company_name",
          minWidth: 170
        },
        {
          title: "原套餐名",
          key: "package_name",
          minWidth: 120
        },
        {
          title: "原运营商",
          key: "carrier_operator",
          width: 120
        },
        {
          title: "订单时间",
          key: "created_at",
          render: (h, { row, column, index }) => {
            return h("span", this.moment(row.created_at).format("YYYY-MM-DD"));
          },
          width: 170
        },
        {
          title: "退货时间",
          key: "refunded_at",
          width: 170
        }
      ]
    };
  },
  created() {
    this.index(1);
  },
  methods: {
    /**
     * [index 列表]
     * @param  {Number} page [description]
     * @return {[type]}      [description]
     */
    index(page = 1) {
      let params = JSON.parse(JSON.stringify(this.params));

      if (
        this.params.refunded_time.length &&
        this.params.refunded_time[0] &&
        this.params.refunded_time[1]
      ) {
        let refunded_time = this.parseTime(this.params.refunded_time);
        params.refunded_starttime = refunded_time.starttime;
        params.refunded_endtime = refunded_time.endtime;
      }

      params.refunded_time = undefined;

      let data = this.searchDataHandle({}, { page }, params);
      this.isShowLoading(true);
      API.index(data)
        .then(res => {
          this.isShowLoading(false);
          if (res.code == 0) {
            this.list_data = res.data;
          }
        })
        .catch(() => {
          this.isShowLoading(false);
        });
    },

    /**
     * [openEdit 打开编辑弹窗]
     * @return {[type]} [description]
     */
    openEdit(bool, data = null) {
      this.editObj = {
        show: bool,
        data
      };
    },

    /**
     * [request 刷新]
     * @return {[type]} [description]
     */
    request() {
      const result = this.list_data;
      let page = result.current_page;

      if (this.list_data.data.length == 1) {
        page = this.returnPage(
          result.total,
          result.current_page,
          result.per_page
        );
      }

      this.index(page);
    },

    resetSearch() {
      for (let k in this.params) {
        if (k === "time" || k === "refund_tim") {
          this.params[k] = [];
        } else {
          this.params[k] = "";
        }
      }
      this.index(1);
    }
  }
};
