<template>
  <div class="rule-type">
    <DialogCom
      :title="id ? '安全责任书存档记录详情' : '新增安全责任书存档记录'"
      :visible.sync="isShow"
      @close="onHide"
      width="1200px"
    >
      <div class="page-body">
        <el-form
          :model="formData"
          :rules="formDataRules"
          size="small"
          ref="form"
          label-position="right"
          label-width="180px"
          label-prefix=":"
        >
          <el-descriptions
            class="margin-top"
            :column="1"
            size="medium"
            border
            :label-style="labelStyle"
            :contentStyle="content_style"
          >
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="签署层级"
            >
              {{ getLabel(dict.type.sign_level, formData.signLevel) }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="签署责任书类型"
            >
              {{ getLabel(dict.type.safety_book_type, formData.type) }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="所属年度"
            >
              {{ formData.year }}
            </el-descriptions-item>
            <!-- <el-form-item prop="orgName" label="签署人所在机构:" >
                  <el-input
                :readonly="true"
                v-model="formData.orgName"
                :disabled="true"
              ></el-input>
               
              </el-form-item> -->
            <el-descriptions-item labelClassName="gx_info_label" label="甲方姓名">
              {{ formData.partyA }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="甲方机构"
              v-if="formData.type == 1 || formData.type == 3"
            >
              {{ formData.partyAOrgName }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="甲方机构"
              v-if="formData.type == 2"
            >
              <!-- <tree-select
                :disabled="true"
                v-model="formData.partyAOrg"
                :options="deptOptions"
                :show-count="true"
                :normalizer="tenantIdnormalizer"
                :props="{ checkStrictly: true, label: 'shortName' }"
                placeholder="请选择乙方所属机构"
              /> -->
              {{ formData.partyAOrgName }}
            </el-descriptions-item>
            <el-descriptions-item labelClassName="gx_info_label" label="乙方姓名">
              {{ formData.partyB }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="乙方机构"
              v-if="formData.type == 1 || formData.type == 3"
            >
              <!-- <tree-select
                :disabled="true"
                v-model="formData.partyBOrg"
                :options="deptOptions"
                :show-count="true"
                :normalizer="tenantIdnormalizer"
                :props="{ checkStrictly: true, label: 'shortName' }"
                placeholder="请选择乙方所属机构"
              /> -->
              {{ formData.partyBOrgName }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="乙方机构"
              v-if="formData.type == 2"
            >
              {{ formData.partyBOrgName }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="签署时间"
            >
              {{
                formData.time
                  ? dayjs(formData.time).format("YYYY年MM月DD日")
                  : ""
              }}
            </el-descriptions-item>
            <el-descriptions-item
              labelClassName="gx_info_label"
              label="签署文件"
            >
              <div
                v-for="(file, index) in formData.files"
                :key="index"
                class="container"
              >
                <el-image
                  class="item"
                  v-if="isPic(file.url)"
                  :src="file.url"
                  :preview-src-list="[file.url]"
                  fit="contain"
                ></el-image>
                <div v-else="isPic(file.url)">
                  <el-link :href="file.url" :underline="false" target="_blank">
                    <span class="el-icon-document"> {{ file.name }} </span>
                  </el-link>
                </div>
              </div>
            </el-descriptions-item>
          </el-descriptions>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="margin-top: 10px">
        <el-button @click="isShow = false">确定</el-button>
        <!-- <el-button type="primary" @click="onSubmit">确定</el-button> -->
      </div>
    </DialogCom>
    <!-- <DialogSelect ref="DialogSelect" @success="getSign"></DialogSelect> -->
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { getLabel } from "@/views/commonOption.js";
import {
  listSafetyBook,
  getSafetyBook,
  editOrAdd,
  delSafetyBook,
} from "@/api/safetyBookNew/index";
import { deptTreeSelect } from "@/api/system/public";
//   import DialogSelect from "./dialog.sign";
import dayjs from "dayjs";
export default {
  dicts: ["safety_book_type", "sign_level"],
  data() {
    return {
      labelStyle: {
        color: "#000",
        "text-align": "center",
        height: "40px",
        "min-width": "150px",
        "word-break": "keep-all",
      },
      content_style: {
        "text-align": "left",
        "min-width": "150px",
        "word-break": "break-all",
      },
      id: null,
      isShow: false,
      timeClearable: true,
      formData: this.reset(),
      tableData: [],
      //修改新增中的机构树
      deptOptions: [],
      formDataRules: {
        orgId: [{ required: true, message: "请选择签署责任人所在机构" }],
        type: [{ required: true, message: "请选择签署责任书类型" }],
        year: [{ required: true, message: "请选择所属年度" }],
      },
    };
  },

  watch: {},
  computed: {
    ...mapState([]),
  },
  methods: {
    isPic(url) {
      if (url.endsWith("jpg") || url.endsWith("png")) {
        return true;
      }
      return false;
    },
    getLabel,
    ...mapMutations([]),
    dayjs,
    //添加签署后回调
    //   getSign(data) {
    //     this.tableData.push(data);
    //     // console.log(data, "ddd");
    //   },
    //新增签署
    openSelect() {
      // this.$refs.DialogSelect.show();
    },
    /** 查询机构树数据 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** treeSelect组件自定义数据*/
    tenantIdnormalizer(node, instanceId) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.shortName,
        children: node.children,
      };
    },
    removeRow(row) {
      this.tableData = this.tableData.filter((item) => item !== row);
    },
    reset() {
      return {
        id: null,
        type: null,
        year: null,
      };
    },
    async refresh(id) {
      if (id != null && id != undefined) {
        await getSafetyBook(id).then((res) => {
          // console.log(res.data,"res")
          this.formData = res.data;
          this.tableData = res.data.bookUsers;
        });
      }
    },
    async show(id) {
      // console.log(id, "id");
      this.getDeptTree();
      this.formData = this.reset();
      this.tableData = [];
      this.id = id;
      await this.refresh(id);
      this.isShow = true;
    },

    // 事件
    onHide() {
      this.formData = this.reset();
      this.$refs.form.resetFields();
    },
    onSubmit() {
      console.log(this.formData, "this.formData");
      this.$refs.form.validate(async (isValidate) => {
        if (!isValidate) return;
        this.formData.bookUsers = this.tableData;
        await editOrAdd(this.formData);
        this.$emit("success");
        this.isShow = false;
      });
    },

    // 事件
    //apimark//
  },
  mounted() {},
  components: {},
};
</script>

<style lang="scss" scoped>
.brand_info {
  .el-form {
    width: 600px;
    padding-top: 40px;
  }
}
.container {
  width: 50px;
  height: 50px;
  float: left;
  .item {
    width: 100%;
    height: 100%;
  }
  .item img {
    width: 100% !important;
    object-fit: cover !important;
  }
}
</style>
