<template>
  <div class="adjustDetail pagePadding clear">
    <el-image-viewer
      v-show="showViewer"
      :on-close="closeViewer"
      :url-list="srcListView"
    />
    <div class="left">
      <show-data-table
        style="margin: 0; padding: 0"
        :sitem="sitem"
        :newTime="newTime"
        v-if="newTime !== ''"
        :columns="detail_columns"
        border
      >
        <template slot="company_type">
          <el-tag
            :size="'mini'"
            v-text="
              (company_type.find((item) => item.id == sitem.order_type) || {}).label ||
              '--'
            "
          ></el-tag>
        </template>
        <template slot="can">
          <el-tag :size="'mini'">
            <span v-for="(s, i) in sitem.can" :key="s.id + i"
              >{{ i !== 0 ? "/" : "" }}{{ s.rename }}</span
            ></el-tag
          >
        </template>
        <template slot="remark">
          <div
            v-html="sitem.remark"
            class="remark"
            ref="contentf"
            @click="imageChange($event)"
          />
        </template>
      </show-data-table>
      <div class="stockApply-com">
        <div class="pinglun">
          <div class="pinglun-title">回复记录</div>
          <div class="my-page fr" v-if="parmValue.total > 5">
            <el-pagination
              @current-change="handleCurrentChange"
              @prev-click="handleCurrentChange"
              @next-click="handleCurrentChange"
              :current-page="parmValue.page"
              :page-size="parmValue.size"
              layout="total, prev, pager, next"
              :total="parmValue.total"
            >
            </el-pagination>
          </div>
        </div>
        <div class="tableData">
          <div
            class="tableData-main clear"
            v-for="(item, index) in tableData"
            :key="item.talk_note + 'key'"
          >
            <div class="tableData-title">
              <div class="label">
                <div class="user">{{ item.talk_name }}</div>
                <div class="time">{{ item.addtime }}</div>
              </div>
            </div>
            <div
              class="tableData-talk_note"
              :style="{
                borderLeft:
                  index + 1 === tableData.length
                    ? '2px solid transparent'
                    : '2px solid #e4e7ed',
              }"
            >
              <div class="dian-mian fl">
                <div class="dian">&nbsp;</div>
                <div class="dian-zhe" v-if="index + 1 === tableData.length">&nbsp;</div>
              </div>
              <div
                class="talk-note-main"
                v-html="item.talk_note"
                ref="contentff"
                @click="imageChange($event)"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <div v-loading="loading">
        <div class="title border">
          <b>状态信息</b>
          <el-button
            class="fr"
            :size="'mini'"
            v-if="powers.some((item) => item == '002')"
            type="primary"
            style="margin: 6px 0 0 12px"
            @click="submitForm"
            >保存
          </el-button>
          <el-button
            v-if="powers.some((item) => item == '002')"
            class="fr"
            :size="'mini'"
            plain
            style="margin: 6px 0 0 0"
            @click="showModel = true"
            >操作记录
          </el-button>
        </div>
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :size="'mini'"
          label-width="80px"
          :disabled="!powers.some((item) => item == '002')"
          class="demo-ruleForm"
        >
          <el-form-item label="处理状态:">
            <el-select
              class="notshow"
              v-model="ruleForm.status"
              style="width: 100%"
              placeholder="处理状态:"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="处理人:" v-if="level == '1'">
            <el-select
              class="notshow"
              v-model="ruleForm.deal_id"
              style="width: 100%"
              filterable
              clearable
              placeholder="处理人:"
            >
              <el-option
                v-for="item in userOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="优先级:">
            <el-select
              class="notshow"
              v-model="ruleForm.level"
              style="width: 100%"
              :size="'mini'"
              placeholder="优先级"
            >
              <el-option
                v-for="item in levelOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="需求类型:">
            <el-select
              class="notshow"
              v-model="ruleForm.type"
              style="width: 100%"
              :size="'mini'"
              placeholder="需求类型"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <comModule
        :id="'add'"
        :bugNo="bugNo"
        @submit="plunsubmit"
        v-if="powers.some((item) => item == '003')"
      />
    </div>
    <record :show-model="showModel" :bugNo="bugNo" @cancel="showModel = false" />
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import record from "@/components/record";
import comModule from "@/components/comModule";
import TinymceEditor from "@/components/TinymceEditor";
import asyncRequest from "@/apis/service/purchaseAndSale/adjust";
import {
  statusOptions,
  levelOptions,
  typeOptions,
  detail_columns,
  company_type,
} from "@/views/pucColumns";
export default {
  name: "adjustDetail",
  mixins: [resToken],
  components: {
    TinymceEditor,
    record,
    comModule,
    "el-image-viewer": () => import("element-ui/packages/image/src/image-viewer"),
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size", "level"]),
    powers() {
      return this.$store.getters.btnList;
    },
  },
  data() {
    return {
      showViewer: false,
      srcListView: [],
      sitem: null,
      parmValue: {
        page: 1, // 页码
        size: 5, // 每页显示条数
        id: this.queryId,
        total: 0,
      },
      tableData: [],
      bugNo: "",
      statusOptions,
      levelOptions,
      typeOptions,
      detail_columns,
      company_type,
      type: 1,
      userOptions: [],
      ruleForm: {},
      activeName: "1",
      newTime: "",
      showModel: false,
      queryId: "",
      loading: false,
      rulesThis: this.rules,
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initForm();
  },
  methods: {
    imageChange(e) {
      if (e.target.nodeName === "IMG" || e.target.nodeName === "img") {
        const img = e.target.currentSrc;
        this.srcListView = [img];
        this.showViewer = true;
      }
    },
    closeViewer() {
      this.showViewer = false;
    },
    async handleCurrentChange(val) {
      // console.log(val);
      this.parmValue.page = val;
      await this.searchList();
    },
    async initForm() {
      this.loading = true;
      this.newTime = "";
      this.InitializeForm();
      await this.resetForm();
      await this.initData();
      this.loading = false;
      await this.searchList();
    },

    getNewTime() {
      this.newTime = new Date().valueOf() + "";
    },
    async plunsubmit() {
      this.parmValue.page = 1;
      await this.searchList();
    },
    //获取初始化数据
    async initData() {
      const { code, data, message } = await asyncRequest.detail({ id: this.queryId });
      if (code === 0) {
        this.sitem = data;
        const {
          id,
          bugNo,
          title,
          company_type,
          level,
          type,
          remark,
          status,
          apply_name,
          model_id,
          deal_id,
          company_name,
          noteNo,
        } = data;
        this.bugNo = bugNo;
        this.ruleForm = {
          id: String(id),
          bugNo: String(bugNo),
          title: String(title) || "",
          company_type: company_type,
          remark: remark || "",
          status: String(status) || "",
          level: String(level) || "",
          type: String(type) || "",
          noteNo: String(noteNo) || "",
          apply_name: String(apply_name) || "",
          model_id: model_id || [], //.split(",")
          deal_id: deal_id || "",
          company_name: String(company_name)|| "",
        };
        // this.getimg(remark);
        this.getNewTime();
        await this.getMuser();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    // getimg(str) {
    //   console.log(str);
    //   let arrlist = [];
    //   let arr = str.split("<img");
    //   if (arr.length > 1) {
    //     arrlist.push({ type: "text", value: arr[0] });
    //   }

    //   console.log(arr);
    // },
    //获取处理人
    async getMuser() {
      this.userOptions = [];
      const { code, data, message } = await asyncRequest.allUser({ level: "1" });
      if (code === 0) {
        let list = data;
        list.forEach((v) => {
          let model = {
            value: v.id,
            label: v.nickname,
          };
          this.userOptions.push(model);
        });
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    // 评论列表
    async searchList() {
      this.parmValue.id = this.queryId;
      const { code, data, message } = await asyncRequest.cloudlist(this.parmValue);
      if (code === 0) {
        let { list, count } = data;
        list.forEach((v) => {
          v.edit = false;
        });
        this.tableData = list;
        this.parmValue.total = Number(count);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.parmValue.total = 0;
      }
    },
    // 重置
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.InitializeForm();
        }
      });
    },
    InitializeForm() {
      this.ruleForm = {
        id: "",
        bugNo: "",
        title: "",
        company_type: "",
        remark: "",
        status: "",
        level: "",
        type: "",
        apply_name: "",
        noteNo: "",
        deal_id: "",
        model_id: [],
        company_name: [],
      };
    },
    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          // model.model_id = model.model_id.toString();
          delete model["company_name"];
          delete model["apply_name"];
          let remark = model.remark;
          remark = remark.replace(/\n/g, "");
          remark = remark.replace(/"/g, "'");
          model.remark = remark;
          let length = 5000;
          if (model.remark.length > length) {
            this.$message.warning("描述文字及标签总长度不能超过5000！");
            this.loading = false;
            return;
          }
          const { code, data, message } = await asyncRequest.update(model);
          if (code === 0) {
            this.$notify.success({
              title: "修改成功",
              message: "",
            });

            await this.initForm();
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
          this.loading = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.adjustDetail {
  width: 100%;
  padding: 16px;
  position: relative;
  height: calc(100vh - 50px);
  min-height: calc(100vh - 50px);
  box-sizing: border-box;
  background: #fff;
  /deep/ .notshow {
    .el-textarea__inner {
      border-color: transparent !important;
      background: transparent !important;
      &:hover {
        cursor: pointer;
        background: rgb(242, 245, 250);
      }
    }
    &:hover {
      cursor: pointer;
      background: rgb(242, 245, 250);
    }
    &.el-input--medium {
      input {
        color: #000 !important;
        font-size: 16px;
      }
    }
    input {
      border-color: transparent !important;
      background: transparent !important;
    }
    // &.el-input--mini {
    input::-webkit-input-placeholder {
      font-size: 12px !important;
    }
    input::-moz-placeholder {
      /* Mozilla Firefox 19+ */
      font-size: 12px !important;
    }
    input:-moz-placeholder {
      /* Mozilla Firefox 4 to 18 */
      font-size: 12px !important;
    }
    input:-ms-input-placeholder {
      /* Internet Explorer 10-11 */
      font-size: 12px !important;
    }
    // }
    .el-input__suffix {
      i {
        color: transparent !important;
      }
    }
  }
  .left {
    width: calc(100% - 350px);
    float: left;
    // padding: 16px;
    box-sizing: border-box;
    .remark {
      display: inline-block;
    }
    .title {
      padding: 0 0 4px 0;
      .title-left {
        float: left;
        height: 36px;
        line-height: 36px;
        width: 190px;
      }
      .title-right {
        float: left;
        width: calc(100% - 190px);
        height: 30px;
        line-height: 30px;
      }
    }

    .desc {
      width: 100%;
      box-sizing: border-box;
      padding: 0 2px 0 0;
      height: 421px;

      .desc-main {
        position: relative;
        // border: 2px solid #dfe4ed;
        line-height: 40px;
        width: 100%;
        height: 100%;
        padding: 16px;
        box-sizing: border-box;
        // overflow-y: scroll;
      }
    }
    .pinglun {
      width: 100%;
      box-sizing: border-box;
      padding: 0 0 16px 0;
      .pinglun-title {
        color: #252b3a;
        color: var(--devui-text, #252b3a);
        font-size: 14px;
        height: 50px;
        line-height: 60px;
        font-weight: 700;
      }
    }
    .tableData {
      width: 100%;
      box-sizing: border-box;
      .tableData-main {
        .tableData-title {
          height: 26px;
          width: 135px;
          float: left;
          text-align: right;
          line-height: 18px;
          padding: 0 15px 0 0;

          .label {
            font-size: 12px;
            margin: -6px 0 0 0;
          }
          .user {
            line-height: 20px;
          }
          .time {
            color: #8a8e99;
            line-height: 16px;
          }
          .operation {
            i {
              margin: 0 0 0 8px;
              color: #8a8e99;
              &:hover {
                cursor: pointer;
              }
            }
          }
        }
        .tableData-talk_note {
          float: right;
          width: calc(100% - 135px);

          .dian-mian {
            position: relative;
            width: 8px;
            height: 26px;
            line-height: 26px;
            .dian {
              width: 8px;
              height: 8px;
              border-radius: 50%;
              overflow: hidden;
              z-index: 3;
              position: absolute;
              margin: 0px 0 0 -5px;
              background: #409eff;
            }
            .dian-zhe {
              background: #fff;
              width: 2px;
              height: 13px;
              position: absolute;
              left: 2px;
              bottom: 0;
            }
          }

          .talk-note-main {
            padding: 0px 0px 8px 16px;
            line-height: 22px;
            min-height: 50px;
            font-size: 12px;
            /deep/ * {
              word-break: break-all;
            }
          }
        }
      }
    }
    .my-page {
      margin: -30px 0 10px 0;
      width: 70%;
      text-align: right;
      box-sizing: border-box;
    }
  }
  .pinglun-input {
    padding: 0 12px;
    font-size: 14px;
    height: 30px;
    line-height: 30px;
    color: #c0c4cc;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    &:hover {
      cursor: pointer;
    }
  }
  .right {
    width: 350px;
    float: right;
    padding: 0px 0 0 18px;
    box-sizing: border-box;

    .title {
      width: 100%;
      padding: 0 20px;
      height: 40px;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      line-height: 40px;
      font-weight: bold;
      display: inline-block;
      list-style: none;
      font-size: 14px;
      font-weight: 500;
      color: #303133;
      position: relative;

      margin: 0 0 20px 0;
      &.border {
        border-bottom: 1px solid #eee;
        //rgb(223, 228, 237);
      }
    }
    .demo-ruleForm {
      /deep/ .el-form-item--mini.el-form-item {
        margin-bottom: 10px !important;
      }
      /deep/ .el-form-item--mini .el-form-item__label {
        font-size: 12px !important;
        font-weight: lighter !important;
        color: #000 !important;
        padding: 0 3px 0 0 !important;
      }

      .show-input {
        height: 28px !important;
        line-height: 28px !important;
        overflow: hidden !important;
        font-size: 12px !important;
        color: #606266 !important;
        padding: 0 15px !important;
        box-sizing: border-box !important;
        &.ho {
          &:hover {
            cursor: pointer;
            background: rgb(242, 245, 250);
          }
        }
        &.disabled {
          color: #c0c4cc !important;
          // cursor: not-allowed !important;
        }
        &.hei {
          height: auto !important;
          line-height: 28px auto !important;
        }
      }
      .setUser {
        position: absolute;
        right: 0;
        top: 0;
        width: 30px;
        height: 28px;
        line-height: 28px;
        text-align: center;
        &:hover {
          cursor: pointer;
        }
        i {
          font-size: 16px;
        }
      }
    }
  }
}
.atuser {
  width: 100%;
  border: 1px solid red;
  .editor {
    width: 700px;
    height: 160px;
    overflow: hidden;
    border: 0px;
    outline: none;
    resize: none;
    -webkit-appearance: none;
  }
}
</style>
