<template>
  <div class="adjustDetail pagePadding clear" v-loading="loading">
    <div class="left">
      <div class="title clear">
        <div class="title-left">#{{ ruleForm.bugNo }}</div>
        <div class="title-right">
          <el-input
            class="notshow"
            v-model="ruleForm.title"
            :placeholder="'标题'"
            :size="'medium'"
          />
        </div>
      </div>
      <div class="stockApply-com">
        <el-tabs :size="'mini'" v-model="activeName">
          <el-tab-pane label="描述详情" name="1">
            <div class="desc">
              <tinymce
                v-model="ruleForm.remark"
                :width="'100%'"
                :height="'350px'"
              />
            </div>
            <div class="desc">
              <div v-html="html" class="show-html"></div>
            </div>
            <div class="pinglun">
              <div class="pinglun-title">评论</div>
              <div
                class="pinglun-input"
                v-if="!pinglunEdit"
                @click="pinglunEdit = true"
              >
                请输入评论： Ctrl+V可粘贴截图
              </div>
              <div v-else>
                <comModule
                  :id="'add'"
                  @cancel="pinglunEdit = false"
                  @submit="plunsubmit"
                />
              </div>
            </div>
            <div class="tableData">
              <div
                class="tableData-main"
                v-for="(item, index) in tableData"
                :key="item.talk_note + 'key'"
                :style="{
                  borderLeft:
                    index + 1 === tableData.length
                      ? '2px solid transparent'
                      : '2px solid #252b3a',
                }"
              >
                <div class="tableData-title clear">
                  <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="user fl">{{ item.talk_name }}</div>
                  <div class="time fr">{{ item.addtime }}</div>
                  <div
                    class="operation fr"
                    v-if="!item.edit && item.is_edit === '0'"
                  >
                    <i class="el-icon-edit" @click="editItem(index)"></i>
                    <i class="el-icon-delete" @click="deleteById(item.id)"></i>
                  </div>
                </div>
                <div class="tableData-talk_note">
                  <div v-if="item.edit">
                    <comModule
                      :id="item.id"
                      :value="item.talk_note"
                      @cancel="tableData[index].edit = false"
                      @submit="plunsubmit"
                    />
                  </div>
                  <div
                    class="talk-note-main"
                    v-else
                    v-html="item.talk_note"
                  ></div>
                </div>
              </div>
              <div
                class="my-page"
                v-if="pageInfo.total > 5"
                style="margin: -40px 0 0 0"
              >
                <el-pagination
                  @current-change="handleCurrentChange"
                  :current-page="pageInfo.page"
                  :page-size="pageInfo.size"
                  layout="total, prev, pager, next"
                  :total="pageInfo.total"
                >
                </el-pagination>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="操作记录" name="2">
            <record :newTime="newTime" :id="queryId" />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <div class="right">
      <div class="title">
        基本信息
        <el-button
          class="fr"
          :size="'mini'"
          v-if="!isDetail"
          type="primary"
          style="margin: 6px 0 0 0"
          @click="submitForm"
          >保存
        </el-button>
      </div>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :size="'mini'"
        label-width="80px"
        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="处理人:">
          <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>
          <div class="setUser" @click="showModel = true">
            <i class="el-icon-setting"></i>
          </div>
        </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-item label="功能区:">
          <good-class
            class="notshow"
            :value="ruleForm.model_id"
            :size="'mini'"
            @handleChange="goods_class_change"
            :disabled="isDetail"
            :type="'1'"
            :placeholder="'功能区'"
          />
        </el-form-item>
        <el-form-item label="创建人:">
          <div class="show-input">{{ ruleForm.apply_name }}</div>
        </el-form-item>

        <el-form-item label="创建人部门:">
          <div class="show-input hei">
            <el-tag
              style="margin: 0 5px 0 0"
              type="info"
              size="mini"
              v-for="(ditem, dindex) in ruleForm.company_name"
              :key="ditem + dindex"
            >
              <span
                v-for="(cItem, cindex) in ditem"
                :key="cItem + dindex + cindex"
                :style="{ marginLeft: cindex > 0 ? '0px' : '0px' }"
              >
                <span v-show="cindex > 0">/</span><span>{{ cItem.name }}</span>
              </span>
              <!-- {{ ditem.name }} -->
            </el-tag>
          </div>
        </el-form-item>
      </el-form>
      <div class="tijiaoBTN"></div>
    </div>
    <select-handler-model
      :id="queryId"
      :show-model="showModel"
      :type="platformType"
      @refresh="(showModel = false), getMuser()"
      @cancel="showModel = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseAndSale/adjust";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import record from "./record";
import comModule from "./comModule";
import Tinymce from "@/components/Tinymce";
import selectHandlerModel from "./select-handler-model";
import {
  statusOptions,
  levelOptions,
  typeOptions,
  platformType,
} from "./columns";
export default {
  name: "adjustDetail",
  mixins: [resToken],
  components: {
    Tinymce,
    record,
    comModule,
    selectHandlerModel,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "adjustDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      pinglunEdit: false,
      parmValue: {
        page: 1, // 页码
        size: 5, // 每页显示条数
        id: this.queryId,
      },
      tableData: [],
      pageInfo: {
        size: 5,
        curr: 1,
        total: 0,
      },
      statusOptions: statusOptions,
      levelOptions: levelOptions,
      typeOptions: typeOptions,
      userOptions: [],
      ruleForm: {},
      activeName: "1",
      newTime: "",
      showModel: false,
      queryId: "",
      loading: false,
      rulesThis: this.rules,
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initForm();
  },
  methods: {
    async handleCurrentChange(val) {
      this.parmValue.page = val;
      this.pageInfo.curr = val;
      await this.searchList();
    },
    async initForm() {
      this.loading = true;
      this.InitializeForm();
      await this.resetForm();
      await this.initData();
      this.loading = false;
      this.getNewTime();
      await this.searchList();
    },

    getNewTime() {
      this.newTime = new Date().valueOf() + "";
    },
    editItem(index) {
      this.tableData.forEach((v) => {
        v.edit = false;
      });
      this.tableData[index].edit = true;
    },
    goods_class_change(e) {
      this.ruleForm.model_id = e;
      this.$refs.ruleForm.validateField("model_id");
    },
    async deleteById(id) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            id: id,
          };
          const res = await asyncRequest.comDel(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async plunsubmit(e) {
      if (!this.loading) {
        this.loading = true;
        const { id, input } = e;
        let model = {
          id: id,
          talk_note: input,
          bugNo: this.ruleForm.bugNo,
        };
        let res = {};
        if (id === "add") {
          delete model["id"];
          res = await asyncRequest.comAdd(model);
        } else {
          res = await asyncRequest.comUpdate(model);
        }
        this.loading = false;
        if (res && res.code === 0) {
          this.$notify.success({
            title: id === "add" ? "添加成功！" : "修改成功！",
            message: "",
          });
          if (id === "add") {
            this.pinglunEdit = false;
          }
          await this.initForm();
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      }
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.queryId });
      if (res && res.code === 0) {
        let {
          id,
          bugNo,
          title,
          company_type,
          level,
          type,
          remark,
          status,
          apply_name,
          model_id,
          deal_id,
          company_name,
        } = res.data;
        // console.log(res.data);
        this.ruleForm = {
          id: id,
          bugNo: bugNo,
          title: title || "",
          company_type: company_type || platformType,
          remark: remark || "",
          status: status || "",
          level: level || "",
          type: type || "",
          apply_name: apply_name || "",
          model_id: model_id.split(",") || [],
          deal_id: deal_id || "",
          company_name: company_name || [],
        };
        await this.getMuser();
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async getMuser() {
      this.userOptions = [];
      const res = await asyncRequest.MUser({ manange_name: "1" });
      if (res && res.code === 0) {
        let list = res.data;
        list.forEach((v) => {
          let model = {
            value: v.manange_id,
            label: v.manange_name,
          };
          this.userOptions.push(model);
        });
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    // 刷新表格
    async searchList() {
      this.tableData = [];
      this.parmValue.id = this.queryId;
      const res = await asyncRequest.cloudlist(this.parmValue);
      if (res && res.code === 0 && res.data) {
        let { list, count } = res.data;
        list.forEach((v) => {
          v.edit = false;
        });
        this.tableData = list;
        this.pageInfo.total = Number(count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.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: "",
        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));
          delete model["company_name"];
          delete model["apply_name"];
          let length = 5000;
          if (model.remark.length > length) {
            this.$message.warning("描述文字及标签总长度不能超过5000！");
            this.loading = false;
            return;
          }
          let res = await asyncRequest.update(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "修改成功",
              message: "",
            });

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

<style lang="scss" scoped>
.adjustDetail {
  width: 100%;
  padding: 16px;
  box-sizing: border-box;
  /deep/ .notshow {
    &: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;
    .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;
    }
    .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;
      }
      .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;
        }
      }
    }
    .tableData {
      width: 100%;
      box-sizing: border-box;
      padding: 26px 0 0 5px;
      .tableData-main {
        position: relative;

        .tableData-title {
          height: 26px;
          position: absolute;
          z-index: 2;
          left: -4px;
          top: -13px;
          line-height: 26px;
          width: 100%;
          padding: 0 16px 0 0;
          .dian-mian {
            position: relative;
            width: 5px;
            height: 26px;
            line-height: 26px;
            .dian {
              width: 6px;
              height: 6px;
              border-radius: 50%;
              overflow: hidden;
              z-index: 3;
              position: absolute;
              margin: 9px 0 0 0;
              background: #252b3a;
            }
            .dian-zhe {
              background: #fff;
              width: 2px;
              height: 13px;
              position: absolute;
              left: 2px;
              bottom: 0;
            }
          }

          .user {
            font-size: 12px;
            padding: 0 0 0 18px;
          }
          .time {
            font-size: 12px;
            padding: 0 0 0 16px;
            color: #8a8e99;
          }
          .operation {
            i {
              margin: 0 0 0 8px;
              color: #8a8e99;
              &:hover {
                cursor: pointer;
              }
            }
          }
        }
        .tableData-talk_note {
          margin: -11px 0 0 0;
          padding: 32px 16px 50px 32px;
          font-size: 12px;
          .talk-note-main {
            word-break: break-all;
            line-height: 22px;
            /deep/ * {
              word-break: break-all;
            }
          }
        }
      }
    }
  }
  .right {
    width: 350px;
    float: right;
    padding: 40px 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;
      display: inline-block;
      list-style: none;
      font-size: 14px;
      font-weight: 500;
      color: #303133;
      position: relative;
      border-bottom: 2px solid rgb(223, 228, 237);
      margin: 0 0 18px 0;
    }
    .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);
          }
        }
        &.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>