<template>
  <div class="my_qa">
    <el-breadcrumb separator="/" class="bread_crumb1 bread_left">
      <el-breadcrumb-item :to="{ path: '/user' }">{{
        $t("lang.personal_center")
      }}</el-breadcrumb-item>
      <el-breadcrumb-item>{{
        $t("lang.my_questions_and_answers")
      }}</el-breadcrumb-item>
    </el-breadcrumb>
    <BlockRadius class="info_block user_qa">
      <div class="left_user">
        <div class="img_head">
          <img :src="user_info.picture_path || head_pic" class="img_head_in" />
        </div>
        <div class="left_word">
          <p class="left_name">{{ user_info.user_name }}</p>
          <p class="left_role">{{ user_info.role }}</p>
        </div>
      </div>
      <div class="right_list">
        <div class="user_qa_card my_q">
          <img src="@/assets/imgs/ic_wodetiwen.png" />
          <div class="card_word">
            <p class="card_name">我的提问</p>
            <p>
              {{ helper.numberFormat(user_info.question_num)
              }}<span v-if="user_info.question_num > 10000" class="card_wan">
                万
              </span>
            </p>
          </div>
        </div>
        <div class="user_qa_card my_a">
          <img src="@/assets/imgs/ic_wodehuida.png" />
          <div class="card_word">
            <p class="card_name">我的回答</p>
            <p>
              {{ helper.numberFormat(user_info.answer_num)
              }}<span v-if="user_info.answer_num > 10000" class="card_wan">
                万
              </span>
            </p>
          </div>
        </div>
        <div class="user_qa_card my_view">
          <img src="@/assets/imgs/ic_liulanshu.png" />
          <div class="card_word">
            <p class="card_name">浏览数</p>
            <p>
              {{ helper.numberFormat(user_info.view_num)
              }}<span v-if="user_info.view_num > 10000" class="card_wan">
                万
              </span>
            </p>
          </div>
        </div>
      </div>
    </BlockRadius>
    <BlockRadius class="block_down info_block">
      <el-tabs v-model="activeName" @tab-click="clickTab">
        <el-tab-pane label="我的提问" name="0"> </el-tab-pane>
        <el-tab-pane label="我的回答" name="1"> </el-tab-pane>
        <el-tab-pane label="被删除" name="2"> </el-tab-pane>
      </el-tabs>
      <div class="qa_list apaas_scroll">
        <div v-for="(item, index) in data_list" :key="index" class="qa_cell">
          <div class="qa_cell_in">
            <div :class="activeName == 2 ? 'qa_is_del' : 'qa_init'">
              <p
                v-if="item.answer != ''"
                class="cell_answer"
                v-text="getContentText(item.answer)"
              ></p>
              <p
                @click="gotodeta(item.question_id)"
                :class="activeName == 0 ? 'cell_title' : 'cell_ans_title'"
              >
                {{ item.title }}
              </p>
              <p
                class="cell_cont"
                v-if="item.question_content && item.question_content != ''"
                v-text="getContentText(item.question_content)"
              ></p>
              <p
                class="cell_cont"
                v-else-if="item.content && item.content != ''"
                v-text="getContentText(item.content)"
              ></p>
              <p class="cell_cont" v-else></p>
              <p v-if="activeName == 2" class="cell_other">
                <span>
                  删除时间：{{ helper.dateStringTransform(item.deleted_time) }}
                </span>
                <span>删除人：{{ item.delete_user }}</span>
                <span v-if="item.delete_reason"
                  >删除理由：{{ item.delete_reason }}</span
                >
              </p>
              <p v-else class="cell_other">
                <span>{{ helper.dateStringTransform(item.created) }}</span>
                <span>评论 {{ item.answer_num }}</span>
                <span>阅读 {{ item.view }}</span>
              </p>
            </div>
            <div v-if="activeName == 0 || activeName == 1" class="qa_del">
              <img
                src="@/assets/imgs/msg_icon_shanchu.png"
                @click="delectItem(item)"
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 分页 -->
      <section class="com-pagination">
        <div class="com_page_total">共 {{ total }} 个条目</div>
        <div class="com_page_item">
          <div class="com_page_num">
            <div>每页条数：</div>
            <div class="com_page_num_sel">
              <el-select
                v-model="pagination.rowsPerPage"
                placeholder="请选择"
                size="small"
                @change="handleSizeChange"
              >
                <el-option
                  v-for="item in pageOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </div>
          <div class="com_page_control">
            <el-button
              icon="el-icon-arrow-left"
              circle
              size="mini"
              @click="handleCurrentChange(-1)"
              :disabled="pagination.page == 1"
            ></el-button>
            &nbsp;&nbsp;第{{ pagination.page }}页 / 共{{
              Math.ceil((total == 0 ? 1 : total) / pagination.rowsPerPage)
            }}页
            <el-button
              icon="el-icon-arrow-right"
              circle
              size="mini"
              @click="handleCurrentChange(1)"
              :disabled="
                pagination.page >=
                Math.ceil((total == 0 ? 1 : total) / pagination.rowsPerPage)
              "
            ></el-button>
          </div>
        </div>
      </section>
    </BlockRadius>
    <el-dialog
      title="确认删除？"
      :visible.sync="diaDelItem"
      :modal-append-to-body="false"
      width="440px"
    >
      <div class="change_psw">您确认删除？</div>
      <div class="dia_btn_footer">
        <el-button class="previous" @click="cancelDel">取消</el-button>
        <el-button class="registe" @click="submitDel">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BlockRadius from "@/components/general/block-radius";
import helper from "@/services/helper.js";
export default {
  components: {
    BlockRadius,
  },
  data: () => ({
    helper,
    activeName: "0",
    pagination: {
      rowsPerPage: 10,
      page: 1,
    },
    total: 0,
    pageOptions: [
      {
        value: "10",
        label: "10",
      },
      {
        value: "20",
        label: "20",
      },
      {
        value: "50",
        label: "50",
      },
    ],
    data_list: [],
    user_info: {
      user_name: "",
      role: "",
      question_num: 0,
      answer_num: 0,
      view_num: 0,
      picture_path: "",
    },
    diaDelItem: false,
    delItem: {},
    head_pic: require("@/assets/imgs/img_head.png"),
  }),
  watch: {
    activeName: {
      handler(val) {
        this.data_list = [];
        this.total = 0;
        this.pagination = {
          rowsPerPage: 10,
          page: 1,
        };
        if (val == "0") {
          this.getQList();
        } else if (val == "1") {
          this.getAList();
        } else if (val == "2") {
          this.getDeleteList();
        }
      },
    },
  },
  methods: {
    clickTab() {},
    handleSizeChange(val) {
      this.pagination.rowsPerPage = val;
      this.refreshData();
    },
    handleCurrentChange(val) {
      this.pagination.page += val;
      this.refreshData();
    },
    refreshData() {
      if (this.activeName == "0") {
        this.getQList();
      } else if (this.activeName == "1") {
        this.getAList();
      } else if (this.activeName == "2") {
        this.getDeleteList();
      }
    },
    pageResize() {
      let listWidth = this.$refs.commodityCardIn.clientWidth;
      let rowCardNum = Math.floor(listWidth / 310);
      let cardArr = this.$refs.commodityCardIn.children;
      for (let i = 0; i < cardArr.length; i++) {
        cardArr[i].style.width = listWidth / rowCardNum - 1 + "px";
      }
    },
    delectItem(item) {
      this.delItem = item;
      console.log(this.delItem);
      this.diaDelItem = true;
    },
    getUserQA() {
      this.$api.user.getUserQA().then((response) => {
        if (response.data.success == 1) {
          this.user_info = response.data.data;
        }
      });
    },
    getQList() {
      let query = {
        search: "",
        style: 2,
        create_user: "",
        start: "",
        end: "",
        limit: this.pagination.rowsPerPage,
        page: this.pagination.page,
        order: "created",
      };
      this.$api.user.getQuestionList(query).then((response) => {
        if (response.data.success == 1) {
          this.data_list = response.data.data;
          this.total = response.data.total;
        }
      });
    },
    getAList() {
      let query = {
        search: "",
        style: 2,
        create_user: "",
        start: "",
        end: "",
        limit: this.pagination.rowsPerPage,
        page: this.pagination.page,
      };
      this.$api.user.getAnswerList(query).then((response) => {
        if (response.data.success == 1) {
          this.data_list = response.data.data;
          this.total = response.data.total;
        }
      });
    },
    getDeleteList() {
      this.$api.user.getDeleteList().then((response) => {
        if (response.data.success == 1) {
          this.data_list = response.data.data;
          this.total = response.data.total;
        }
      });
    },
    gotodeta(val) {
      this.$router.push("/technical_support/answer_center/detail/" + val);
    },
    cancelDel() {
      this.diaDelItem = false;
    },
    submitDel() {
      if (this.activeName == 0) {
        this.delQ();
      } else if (this.activeName == 1) {
        this.delA();
      }
    },
    delQ() {
      let items = [{ id: this.delItem.id }];
      this.diaDelItem = false;
      this.$api.user.delQuestions(items).then((response) => {
        if (response.data.success == 1) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getUserQA();
          this.refreshData();
        }
      });
    },
    delA() {
      let items = [{ id: this.delItem.id }];
      this.diaDelItem = false;
      this.$api.user.delAnswers(items).then((response) => {
        if (response.data.success == 1) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getUserQA();
          this.refreshData();
        }
      });
    },
    getContentText(content) {
      let text = content.replace(/<img[^>]+>/g, "[图片]");
      text = text.replace(/<iframe(([\s\S])*?)<\/iframe>/g, "[视频]");
      text = text.replace(/<video(([\s\S])*?)<\/video>/g, "[视频]");
      text = text.replace(/<pre(([\s\S])*?)<\/pre>/g, "[代码]");
      text = text.replace(/<\/?.+?>/g, "");

      return text;
    },
  },
  created() {
    if (this.$route.query.name !== undefined) {
      this.activeName = this.$route.query.name;
    }
  },
  mounted() {
    this.getUserQA();
    this.getQList();
  },
};
</script>

<style scoped>
.my_qa {
  /* height: calc(100% - 20px); */
  height: calc(100%);
  overflow: hidden;
}
.info_block {
  margin: 0 20px 20px;
  position: relative;
}
.block_down {
  height: calc(100% - 214px);
}
.user_qa {
  height: 120px;
  display: flex;
  justify-content: space-between;
}
.left_user {
  width: 300px;
  display: flex;
  justify-content: space-between;
}
.img_head {
  width: 76px;
  height: 76px;
  margin: 2px 10px;
  border-radius: 50%;
  overflow: hidden;
  border: solid 2px #e3e5ef;
}
.img_head_in {
  width: 100%;
  height: 100%;
}
.left_word {
  width: 200px;
  padding-top: 5px;
}
.left_name {
  color: #1d1e20;
  font-size: 22px;
  line-height: 44px;
}
.left_role {
  color: #8890a7;
  font-size: 14px;
}
.right_list {
  width: 60%;
  min-width: 750px;
  max-width: 1000px;
  display: flex;
  justify-content: space-between;
}
.user_qa_card {
  width: 31%;
  height: 80px;
  border-radius: 10px 10px 10px 40px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 34px;
  font-family: Arial;
  font-weight: bold;
  text-align: right;
}
.my_q {
  background-color: #fcefd6;
  color: #ef9433;
}
.my_a {
  background-color: #e7fdfc;
  color: #25bdb1;
}
.my_view {
  background-color: #e5f4fe;
  color: #38aef9;
}
.card_word {
}
.card_name {
  color: #242c43;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: normal;
}
.card_wan {
  font-size: 14px;
}
.qa_list {
  height: calc(100% - 114px);
  overflow: auto;
}
.qa_cell {
  padding: 0 20px;
  border-radius: 12px;
}
.qa_cell:hover {
  background-color: #f8f9fd;
}
.qa_cell_in {
  height: 100%;
  padding: 20px 0;
  border-bottom: #f4f7fc 2px solid;
  display: flex;
  justify-content: space-between;
}
.qa_cell:nth-last-child(1) .qa_cell_in {
  border-bottom: none;
}
.qa_init {
  width: calc(100% - 80px);
}
.qa_init .cell_answer {
  color: #1a2236;
  font-size: 16px;
  margin: 0 0 16px;
  cursor: pointer;
}
.qa_init .cell_ans_title {
  color: #58617a;
  font-size: 16px;
  margin: 0 0 8px;
  cursor: pointer;
}
.qa_init .cell_title {
  color: #242c43;
  font-size: 16px;
  margin: 0 0 8px;
  cursor: pointer;
}
.qa_init .cell_cont {
  color: #8890a7;
  font-size: 14px;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
.qa_init .cell_other {
  color: #58617a;
  font-size: 14px;
  margin: 14px 0 0;
}
.qa_init .cell_other span {
  display: inline-block;
  padding: 0 16px 0 0;
  position: relative;
}
.qa_init .cell_other span::after {
  content: "";
  position: absolute;
  width: 2px;
  height: 11px;
  background-color: #e6ebfe;
  right: 5px;
  top: 4px;
}
.qa_init .cell_other span:nth-last-child(1)::after {
  content: none;
}
.qa_is_del {
  width: calc(100% - 80px);
}
.qa_is_del .cell_answer {
  color: #a9aec0;
  font-size: 16px;
  margin: 0 0 16px;
  cursor: pointer;
}
.qa_is_del .cell_ans_title {
  color: #a9aec0;
  font-size: 16px;
  margin: 0 0 8px;
  cursor: pointer;
}
.qa_is_del .cell_title {
  color: #a9aec0;
  font-size: 16px;
  margin: 0 0 8px;
  cursor: pointer;
}
.qa_is_del .cell_cont {
  color: #a9aec0;
  font-size: 14px;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
.qa_is_del .cell_other {
  color: #8890a7;
  font-size: 14px;
  margin: 14px 0 0;
}
.qa_is_del .cell_other span {
  display: inline-block;
  padding: 0 16px 0 0;
  position: relative;
}
.qa_is_del .cell_other span::after {
  content: "";
  position: absolute;
  width: 2px;
  height: 11px;
  background-color: #e6ebfe;
  right: 5px;
  top: 4px;
}
.qa_is_del .cell_other span:nth-last-child(1)::after {
  content: none;
}
.qa_del {
  width: 70px;
  justify-content: center;
  align-items: center;
  display: none;
}
.qa_cell:hover .qa_del {
  display: flex;
}
.qa_del img {
  cursor: pointer;
}
</style>
<style scoped>
.com-pagination {
  margin-top: 20px;
  font-size: 14px;
  padding: 0 5%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.com_page_num {
  width: 140px;
  margin-right: 20px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.com_page_num_sel {
  width: 50px;
  font-size: 14px;
  position: relative;
  top: 1px;
}
.com_page_item {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.commodity_card {
}
.commodity_card_list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.commodity_card_item {
  margin: 10px 0;
}
.commodity_card_item_in {
  margin: 0 auto;
}
.change_psw {
  text-align: left;
}
.dia_item {
  width: 100%;
  margin-bottom: 12px;
}
.dia_item p {
  padding-left: 15px;
  color: #242c43;
}
.dia_btn_footer {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0 0;
}
.previous {
  width: 100px;
  background-color: #c3caf8;
  color: #0f2683;
}
.registe {
  width: 100px;
  background-color: #0f2683;
  color: #f8f9fd;
}
</style>
<style>
.com_page_control .el-button {
  border: 0;
}
.com_page_num .el-input__inner {
  border: 0;
  padding: 0;
}
</style>