<template>
  <section class="hrDetail">
    <ai-detail>
      <ai-title slot="title" title="监测对象详情" isShowBottomBorder isShowBack @onBackClick="back">
        <template #rightBtn>
          <template v-if="/0/.test(detail.status)&&isGridAdmin">
            <el-button type="primary" @click="handleAccept(1)">纳入监测</el-button>
            <el-button type="primary" @click="handleAccept(0)">驳回申请</el-button>
          </template>
          <template v-if="/1/.test(detail.status)">
            <el-button type="primary" @click="handleRelieve(1)" v-if="isGridAdmin">解除风险</el-button>
            <el-button type="primary" @click="handleRelieve()" v-else>申请解除风险</el-button>
          </template>
          <template v-if="/2/.test(detail.status)">
            <el-button type="primary" @click="handleRelieve(1)" v-if="isGridAdmin">解除风险</el-button>
            <el-button type="primary" @click="handleRelieve(0)">继续监测</el-button>
          </template>
          <template v-if="/3/.test(detail.status)">
            <el-button type="primary" @click="handleAccept(1)">纳入监测</el-button>
          </template>
          <el-button v-if="/[1]/g.test(detail.status)" type="primary" icon="iconfont iconEdit" @click="gotoEdit">
            编辑
          </el-button>
          <el-button v-if="/[134]/g.test(detail.status)" icon="iconfont iconDelete" @click="handleDelete(detail.id)">
            删除
          </el-button>
        </template>
      </ai-title>
      <template #content>
        <el-form :model="detail" label-width="130px" label-position="right" size="small">
          <el-tabs tabPosition="left" v-model="currentTab">
            <el-tab-pane label="基本信息" lazy>
              <ai-card :title="detail.name||'无名氏'">
                <template #content>
                  <div flex class="flex-start">
                    <div flex class="half wrap fill">
                      <el-form-item label="身份证号" prop="name">{{ detail.idNumber }}</el-form-item>
                      <el-form-item label="性别" prop="sex">{{ dict.getLabel('sex', detail.sex) }}</el-form-item>
                      <el-form-item label="出生日期">{{ detail.birthDate }}</el-form-item>
                      <el-form-item label="年龄" prop="age">{{ detail.age }}</el-form-item>
                      <el-form-item label="文化程度">{{ dict.getLabel('fpEducation', detail.education) }}</el-form-item>
                      <el-form-item label="民族">{{ dict.getLabel('fpNation', detail.nation) }}</el-form-item>
                      <el-form-item label="政治面貌">{{
                          dict.getLabel('fpPoliticalOutlook', detail.politicsStatus)
                        }}
                      </el-form-item>
                      <el-form-item label="在校生状况">{{
                          dict.getLabel('fpStudentsInSchool', detail.schoolStatus)
                        }}
                      </el-form-item>
                      <el-form-item label="健康状况">{{ dict.getLabel('fpHealth', detail.healthyStatus) }}</el-form-item>
                      <el-form-item label="务工时间(月)" prop="idNumber">{{ detail.workeMonths }}</el-form-item>
                      <el-form-item label="劳动技能">{{
                          dict.getLabel('fpLaborSkills', detail.labourStatus)
                        }}
                      </el-form-item>
                      <el-form-item label="务工区域" prop="idNumber">{{ detail.workArea }}</el-form-item>
                      <el-form-item label="是否参加大病保险">{{
                          dict.getLabel('fpYesOrNo', detail.seriousIllnessInsurance)
                        }}
                      </el-form-item>
                      <el-form-item label="是否参加基本医保">{{
                          dict.getLabel('fpYesOrNo', detail.basicMedicalTreatment)
                        }}
                      </el-form-item>
                      <el-form-item label="享受最低生活保障">{{
                          dict.getLabel('fpYesOrNo', detail.subsistenceAllowance)
                        }}
                      </el-form-item>
                      <el-form-item label="是否参加养老保险">{{
                          dict.getLabel('fpYesOrNo', detail.endowmentInsurance)
                        }}
                      </el-form-item>
                      <el-form-item label="是否特困供养">{{
                          dict.getLabel('fpYesOrNo', detail.specialPovertySupport)
                        }}
                      </el-form-item>
                    </div>
                    <ai-avatar :instance="instance" v-model="detail.photo" :editable="false"/>
                  </div>
                </template>
              </ai-card>
              <ai-card title="联络信息">
                <template slot="content">
                  <el-form-item label="联系方式" prop="phone">{{ detail.phone }}</el-form-item>
                  <el-form-item label="现住址">{{ currentFullAddress }}</el-form-item>
                </template>
              </ai-card>
              <ai-card title="家庭情况">
                <template slot="content">
                  <div flex class="half wrap">
                    <el-form-item label="是否户主">{{ dict.getLabel('yesOrNo', detail.isHousehold) }}</el-form-item>
                    <template v-if="detail.isHousehold==1">
                      <el-form-item label="监测对象类型">{{ dict.getLabel('fpType', detail.objectType) }}</el-form-item>
                      <el-form-item label="是否危房">
                        {{ dict.getLabel('fpYesOrNo', detail.dilapidatedHousehold) }}
                      </el-form-item>
                      <el-form-item label="是否饮用水安全">
                        {{ dict.getLabel('fpYesOrNo', detail.drinkingWaterSafety) }}
                      </el-form-item>
                      <el-form-item label="是否搬迁">{{ dict.getLabel('fpYesOrNo', detail.move) }}</el-form-item>
                    </template>
                    <template v-if="detail.isHousehold==0">
                      <el-form-item label="与户主关系">
                        {{ dict.getLabel('fpRelationship', detail.householdRelation) }}
                      </el-form-item>
                      <el-form-item label="户主身份证号">{{ detail.householdIdNumber }}</el-form-item>
                    </template>
                  </div>
                </template>
              </ai-card>
              <template v-if="/[123]/.test(detail.status)">
                <ai-card title="收入情况(单位:元)">
                  <template slot="content">
                    <div flex class="half wrap">
                      <el-form-item label="工资收入">{{ detail.income1 }}</el-form-item>
                      <el-form-item label="生产经营收入">{{ detail.income2 }}</el-form-item>
                      <el-form-item label="财产性收入">{{ detail.income3 }}</el-form-item>
                      <el-form-item label="转移性收入">{{ detail.income4 }}</el-form-item>
                      <el-form-item label="理赔收入">{{ detail.income5 }}</el-form-item>
                      <el-form-item label="生产经营支出">{{ detail.income6 }}</el-form-item>
                      <el-form-item label="合规自然支出">{{ detail.income7 }}</el-form-item>
                      <el-form-item label="家庭人均纯收入">{{ detail.income8 }}</el-form-item>
                    </div>
                  </template>
                </ai-card>
                <ai-card title="帮扶情况">
                  <template slot="content">
                    <div flex class="half wrap">
                      <el-form-item label="产业帮扶">{{
                          dict.getLabel('fpIndustrialAssistance', detail.industrialAssistance)
                        }}
                      </el-form-item>
                      <el-form-item label="健康帮扶">{{
                          dict.getLabel('fpHealthAssistance', detail.healthAssistance)
                        }}
                      </el-form-item>
                      <el-form-item label="就业帮扶">{{
                          dict.getLabel('fpEmploymentAssistance', detail.employmentAssistance)
                        }}
                      </el-form-item>
                      <el-form-item label="教育帮扶">
                        {{ dict.getLabel('fpEducationalAssistance', detail.educationalAssistance) }}
                      </el-form-item>
                      <el-form-item label="金融帮扶">{{
                          dict.getLabel('fpFnancialAssistance', detail.financialAssistance)
                        }}
                      </el-form-item>
                      <el-form-item label="社会帮扶">{{
                          dict.getLabel('fpSocialAssistance', detail.socialAssistance)
                        }}
                      </el-form-item>
                      <el-form-item label="公益岗帮扶">
                        {{ dict.getLabel('fpPublicWelfarePostAssistance', detail.publicWelfarePostAssistance) }}
                      </el-form-item>
                      <el-form-item label="是否风险消除">{{
                          dict.getLabel('fpYesOrNo', detail.riskEliminated)
                        }}
                      </el-form-item>
                      <el-form-item label="风险消除日期">{{ detail.riskEliminatedTime }}</el-form-item>
                    </div>
                  </template>
                </ai-card>
                <ai-card title="家庭成员">
                  <template #content>
                    <ai-table :tableData="detail.family" :colConfigs="colConfigs" :dict="dict"
                              :isShowPagination="false">
                      <el-table-column slot="options" label="操作" align="center">
                        <template slot-scope="{row}">
                          <el-button type="text" @click="$router.push({query:{id:row.id}})">详情</el-button>
                        </template>
                      </el-table-column>
                    </ai-table>
                  </template>
                </ai-card>
              </template>
            </el-tab-pane>
            <el-tab-pane label="帮扶日志" lazy name="log">
              <ai-card title="帮扶日志">
                <template #right>
                  <el-button type="text" icon="el-icon-plus" @click="dialog=true">添加</el-button>
                </template>
                <template #content>
                  <div class="logItem" v-for="row in tableData" :key="row.id">
                    <el-row type="flex" align="middle" justify="space-between">
                      <b v-text="row.createUserName"/>
                      <span v-text="row.createTime"/>
                    </el-row>
                    <div class="content" v-text="row.detail"/>
                    <ai-uploader v-model="row.files" disabled/>
                    <div class="btns">
                      <el-button type="text" @click="handleEditLog(row)">编辑</el-button>
                      <el-button type="text" @click="handleDeleteLog(row.id)">删除</el-button>
                    </div>
                  </div>
                  <ai-empty v-if="tableData.length==0"/>
                </template>
              </ai-card>
            </el-tab-pane>
          </el-tabs>
        </el-form>
      </template>
    </ai-detail>
    <ai-dialog :visible.sync="dialog" title="新建日志" @closed="form={}" @onConfirm="submitHelpLog" width="600px">
      <el-form :model="form" :rules="rules" ref="DialogForm" size="small" label-width="80px">
        <el-form-item label="帮扶日志" prop="detail">
          <el-input type="textarea" v-model="form.detail" placeholder="请输入" maxlength="500" show-word-limit rows="5"/>
        </el-form-item>
        <el-form-item label="图片" prop="files">
          <ai-uploader v-model="form.files" :instance="instance" acceptType=".jpg,.jpeg,.png">
            <template #tips>
              最多9张,仅支持10M以内的jpg、jpeg、png格式照片
            </template>
          </ai-uploader>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "hrDetail",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    colConfigs() {
      return [
        {label: "与户主关系", prop: "householdRelation", dict: "fpRelationship"},
        {label: "姓名", prop: "name"},
        {label: "性别", prop: "sex", dict: "sex"},
        {label: "年龄", prop: "age"},
        {label: "身份证号", render: (h, {row}) => h('p', null, this.idCardNoUtil.hideId(row.idNumber))},
        {slot: "options"}
      ]
    },
    isGridAdmin() {
      return this.user.info.girdCheckType == 2
    },
    currentFullAddress() {
      let {countyName, townName, villageName, currentAddress} = this.detail
      return [countyName, townName, villageName, currentAddress].join("")
    }
  },
  watch: {
    currentTab(v) {
      if (v == "log") {
        this.getHelpLogs()
      }
    }
  },
  data() {
    return {
      detail: {},
      dialog: false,
      form: {},
      rules: {
        detail: {required: true, message: "请输入帮扶内容"}
      },
      currentTab: "",
      tableData: []
    }
  },
  methods: {
    back() {
      this.$router.push({})
    },
    getDetail() {
      let {id} = this.$route.query
      id && this.instance.post("/app/apppreventionreturntopoverty/queryDetailById", null, {
        params: {id}
      }).then(res => {
        if (res?.data) {
          this.detail = res.data
        }
      })
    },
    gotoEdit() {
      let {query} = this.$route
      this.$router.push({query, hash: "#add"})
    },
    handleDelete(ids) {
      this.$confirm("是否要删除监测对象").then(() => {
        this.instance.post("/app/apppreventionreturntopoverty/delete", null, {
          params: {ids: ids?.toString()}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.back()
          }
        })
      }).catch(() => 0)
    },
    handleAccept(pass) {
      let msg = pass == 1 ? "是否将该人员纳入监测对象?" : "是否驳回纳入监测对象申请?", {id} = this.detail
      this.$confirm(msg).then(() => {
        this.instance.post("/app/apppreventionreturntopoverty/examine", null, {
          params: {pass, id}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("审批提交成功!")
            this.back()
          }
        })
      }).catch(() => 0)
    },
    handleRelieve(pass) {
      let {id} = this.detail, msg = pass == 0 ? `是否继续监测对象` : `是否${isNaN(pass) ? '(申请)' : ''}解除监测对象`
      this.$confirm(msg).then(() => {
        this.instance.post("/app/apppreventionreturntopoverty/relieve", null, {
          params: {pass, id}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("审批提交成功!")
            this.back()
          }
        })
      }).catch(() => 0)
    },
    getHelpLogs() {
      let {id: pid} = this.detail
      this.instance.post("/app/apppreventionreturntopovertylog/list", null, {
        params: {size: 9999, pid}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records || []
        }
      })
    },
    submitHelpLog() {
      this.$refs.DialogForm.validate(v => {
        if (v) {
          let {id: pid} = this.detail
          this.instance.post(`/app/apppreventionreturntopovertylog/addOrUpdate`, {...this.form, pid}).then(res => {
            if (res.code == 0) {
              this.$message.success('提交成功!');
              this.dialog = false
              this.getHelpLogs()
            }
          })
        }
      })
    },
    handleEditLog(row) {
      this.dialog = true
      this.form = JSON.parse(JSON.stringify(row))
    },
    handleDeleteLog(ids) {
      this.$confirm("是否要删除帮扶日志").then(() => {
        this.instance.post("/app/apppreventionreturntopovertylog/delete", null, {
          params: {ids: ids?.toString()}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getHelpLogs()
          }
        })
      }).catch(() => 0)
    },
  },
  created() {
    this.getDetail()
  }
}
</script>

<style lang="scss" scoped>
.hrDetail {
  height: 100%;

  .flex-start {
    align-items: flex-start;

    .AiAvatar {
      margin-left: 20px;
    }
  }

  .half {
    align-items: flex-start;

    & > .el-form-item, & > div {
      width: 50%;

      .el-form-item {
        width: 100%;
      }
    }
  }

  .el-date-editor {
    width: 100%;
  }

  .logItem {
    position: relative;
    padding: 8px 16px;
    border-radius: 4px;

    &:hover {
      background: #eee;

      .btns {
        display: block;
      }
    }

    .content {
      margin: 8px 0;
    }

    .btns {
      display: none;
      position: absolute;
      right: 8px;
      bottom: 8px;
    }
  }
}
</style>
