<template>
  <div>
    <div
      class="dialog-h-content scroll-h"
      :class="{ 'visiual-form': source === 'visiual' }"
    >
      <el-form
        ref="ref_add_form"
        :inline="true"
        :model="formData"
        :rules="dataRule"
        class="div_form"
      >
        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">姓名: </span>
              <span>{{ formData.name || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="姓名" prop="name" v-else>
              <el-input
                v-model="formData.name"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">联系电话: </span>
              <span>{{ formData.mobile || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="联系电话" v-else>
              <el-input
                v-model="formData.mobile"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">证件类型: </span>
              <span>{{ formData.idTypeName || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="证件类型"
              prop="idType"
              clearable
              v-else
            >
              <el-select v-model="formData.idType" clearable>
                <el-option
                  v-for="item in dicts.idCard_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">证件号: </span>
              <span>{{ formData.idNum || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="证件号"
              prop="idNum"
              v-else
            >
              <el-input
                v-model="formData.idNum"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">出生日期: </span>
              <span>{{ formData.birthday || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="出生日期" v-else>
              <el-date-picker
                v-model="formData.birthday"
                type="date"
                placeholder="选择日期"
                style="width: 202px"
                clearable
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">性别: </span>
              <span>{{
                formData.gender
                  ? formData.gender == "1"
                    ? "男"
                    : formData.gender == "2"
                    ? "女"
                    : "--"
                  : "--"
              }}</span>
            </div>
            <el-form-item label-width="100px" label="性别" v-else>
              <el-select v-model="formData.gender" clearable>
                <el-option
                  v-for="item in genderList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">民族: </span>
              <span>{{ formData.nationName || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="民族" class="wd-wr" v-else>
              <template>
                <el-select v-model="formData.nation" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in dicts.nationList"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">政治面貌: </span>
              <span>{{ formData.politicsStatusName || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="政治面貌"
              class="wd-wr"
              v-else
            >
              <template>
                <el-select
                  v-model="formData.politicsStatus"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in politicsStatusList"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">文化程度: </span>
              <span>{{ formData.cultureLevelName || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="文化程度"
              class="wd-wr"
              v-else
            >
              <template>
                <el-select v-model="formData.cultureLevel" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in dicts.educationList"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">婚姻状况: </span>
              <span>{{ formData.marriageName || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="婚姻状况"
              class="wd-wr"
              v-else
            >
              <template>
                <el-select v-model="formData.marriage" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in dicts.marriageList"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">本地户籍: </span>
              <span>{{ binaryText(formData.localResidenceFlag) }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="本地户籍"
              class="wd-wr"
              v-else
            >
              <template>
                <el-select
                  v-model="formData.localResidenceFlag"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in binaryOptionList"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">户籍地址: </span>
              <span>{{ formData.placeOfDomicile || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="户籍地址"
              class="wd-wr"
              v-else
            >
              <el-input
                v-model="formData.placeOfDomicile"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">现居地址: </span>
              <span>{{ formData.currentResidence || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="现居地址"
              class="wd-wr"
              v-else
            >
              <el-input
                v-model="formData.currentResidence"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">入伍日期: </span>
              <span>{{ formData.joinArmyTime || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="入伍日期" v-else>
              <el-date-picker
                v-model="formData.joinArmyTime"
                type="date"
                placeholder="选择日期"
                style="width: 202px"
                clearable
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">退伍日期: </span>
              <span>{{ formData.leaveArmyTime || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="退伍日期" v-else>
              <el-date-picker
                v-model="formData.leaveArmyTime"
                type="date"
                placeholder="选择日期"
                style="width: 202px"
                clearable
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">服役单位: </span>
              <span>{{ formData.serviceUnit || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="服役单位" v-else>
              <el-input
                v-model="formData.serviceUnit"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">接收单位: </span>
              <span>{{ formData.receiveUnit || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="接收单位" v-else>
              <el-input
                v-model="formData.receiveUnit"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">参战日期: </span>
              <span
                >{{ formData.joinWarStartTime || "--" }} 至
                {{ formData.joinWarEndTime || "--" }}</span
              >
            </div>
            <el-form-item label-width="100px" label="参战日期" v-else>
              <el-date-picker
                v-model="formData.joinWarStartTime"
                type="date"
                class="u-item-width-daterange"
                value-format="yyyy-MM-dd"
                placeholder="开始日期"
                :picker-options="startPickerOptions"
                style="width: 259px"
                clearable
              >
              </el-date-picker>
              <span class="u-data-tag">至</span>
              <el-date-picker
                v-model="formData.joinWarEndTime"
                type="date"
                value-format="yyyy-MM-dd"
                class="u-item-width-daterange u-data-tag"
                placeholder="结束日期"
                :picker-options="endPickerOptionsJoinWar"
                style="width: 259px"
                clearable
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">现就业情况: </span>
              <span>{{ formData.employmentSituation || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="现就业情况" v-else>
              <el-input
                v-model="formData.employmentSituation"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">待安置补助金: </span>
              <span>{{ formData.settlementAmount || "--" }}</span>
            </div>
            <el-form-item label-width="105px" label="待安置补助金" v-else>
              <el-input
                v-model="formData.settlementAmount"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">培训状况: </span>
              <span>{{ formData.trainDesc || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="培训状况" v-else>
              <el-input
                v-model="formData.trainDesc"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">是否办理公益性岗位: </span>
              <span>{{ binaryText(formData.pubWelfareJobFlag) }}</span>
            </div>
            <el-form-item label-width="100px" label="是否办理公益性岗位" v-else>
              <template>
                <el-select
                  v-model="formData.pubWelfareJobFlag"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in binaryOptionList"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">现工作单位: </span>
              <span>{{ formData.workUnit || "--" }}</span>
            </div>
            <el-form-item
              label-width="100px"
              label="现工作单位"
              class="wd-wr"
              v-else
            >
              <el-input
                v-model="formData.workUnit"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="info-prop" v-if="formType == 'detail'">
              <span class="info-title-2">备注: </span>
              <span>{{ formData.remark || "--" }}</span>
            </div>
            <el-form-item label-width="100px" label="备注" class="wd-wr" v-else>
              <el-input
                v-model="formData.remark"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="div_btn">
      <el-button size="small" @click="handleCancle">取 消</el-button>
      <el-button
        size="small"
        type="primary"
        :disabled="btnDisable"
        @click="handleComfirm"
        >确 定</el-button
      >
    </div>
  </div>
</template>
      
        <script>
import { Loading } from "element-ui"; // 引入Loading服务
import { requestPost, requestGet } from "@/js/dai/request";
import nextTick from "dai-js/tools/nextTick";
import {
  genderList,
  binaryOptionList,
  politicsStatusList,
} from "@/js/columns/constants";

let loading; // 加载动画

export default {
  props: {
    formType: {
      type: String,
      default: "add",
    },
    source: {
      //展示来源：manage  管理平台    visiual  可视化平台
      type: String,
      default: "manage",
    },
  },

  data() {
    let self = this;
    function endDisabledDate(time, key) {
      let nowData = Date.now();
      let startTime = self.formData[key];
      if (startTime) {
        let referenceTime = new Date(startTime);
        return (
          time.getTime() < referenceTime - 24 * 60 * 60 * 1000 ||
          time.getTime() > nowData
        );
      } else {
        return time.getTime() > nowData || time.getTime() === nowData;
      }
    }
    let startDisabledDate = (time) => {
      let nowData = Date.now();
      return time.getTime() > nowData;
    };
    return {
      btnDisable: false,

      formData: {
        name: "张三",
        mobile: "176777777777",
        idType: "1",
        idNum: "370215151515154551",
        birthday: "2021-04-06",
        gender: -2827257,
        nation: "0",
        politicsStatus: -54855012,
        cultureLevel: "0",
        marriage: "0",
        localResidenceFlag: 0,
        placeOfDomicile: "十大国际广场",
        currentResidence: "李沧区",
        joinArmyTime: "2021-05-07",
        leaveArmyTime: "2021-05-05",
        serviceUnit: "火箭军",
        receiveUnit: "测测",
        joinWarStartTime: "2021-05-07",
        joinWarEndTime: "2021-05-07",
        employmentSituation: "失业",
        settlementAmount: 0,
        trainDesc: "汽车维修",
        pubWelfareJobFlag: 73704271,
        workUnit: "时代国际广场",
        remark: "备注",
      },
      endPickerOptionsJoinWar: {
        disabledDate: (time) => endDisabledDate(time, "joinWarStartTime"),
      },
      startPickerOptions: {
        disabledDate: startDisabledDate,
      },
      genderList,
      binaryOptionList,
      politicsStatusList,
      dataRule: {
        name: [{ required: true, message: "姓名不能为空", trigger: "bulr" }],
        idNum: [{ required: true, message: "证件号不能为空", trigger: "bulr" }],

        idType: [
          { required: true, message: "证件类型不能为空", trigger: "bulr" },
        ],
      },
      scopeId: "",

      dicts: {
        nationList: [],
        educationList: [],
        marriageList: [],
      }, //字典对象
    };
  },

  watch: {},

  components: {},
  async created() {
    await this.getDicts();
  },
  async mounted() {
    this.startLoading();
    await this.endLoading();
  },

  methods: {
    async initForm(type, row) {
      this.formType = type;
      this.scopeId = row.categorizedResiId;
      await this.getDetail();
    },
    binaryText(value) {
      let text = "";
      text = this.binaryOptionList.filter((item) => item.value == value)[0]
        .label;
      return text || "--";
    },
    async getDetail(categorizedResiId) {
      const url = `/governance/commonDemand/detail/${categorizedResiId}`;
      const { data, code, msg } = await requestGet(url);
      if (code === 0) {
        console.log(data);
        //   let obj = {
        //     name: "测试",
        //     mobile: "17555555555",
        //     idType: "1",
        //     idNum: "370254111111111111",
        //     gender: "1",
        //     localResidenceFlag: 1,
        //     placeOfDomicile: "青岛市市北区时代国际广场",
        //     currentResidence: "青岛市李沧区建安小区",
        //     disabilityCategoryCode: "tingli",
        //     disabilityLevel: "1",
        //     disabilityNum: "545461564512167",
        //     subsistenceAllowanceFlag: 1,
        //     retirementAmount: 1,
        //     monthIncome: 200.111,
        //     contactName: "刘备",
        //     contactMobile: "15655555454",
        //     remark: "备注",
        //   };
        this.formData = { ...obj };
      } else {
        this.$message.error(msg);
      }
    },
    async handleComfirm() {
      this.$refs["ref_add_form"].validate(async (valid) => {
        if (!valid) {
          return false;
        }
        const url =
          this.formType === "edit"
            ? "/governance/commonDemand/update"
            : "/governance/commonDemand/save";

        const { data, code, msg } = await requestPost(url, this.formData);
        if (code === 0) {
          this.$message.success("添加成功");
          this.resetData();
          this.$emit("handleComfirm");
        } else if (code >= 8000) {
          this.$message.error(msg);
        }
      });
    },
    async getDicts() {
      try {
        const requests = [
          this.$http.post("sys/dict/data/nation", {
            formCode: "resi_base_info",
          }),
          this.$http.post("sys/dict/data/education", {
            formCode: "resi_base_info",
          }),
          this.$http.post("sys/dict/data/dictlist", { dictType: "marriage" }),
        ];
        const dictKeys = ["nationList", "educationList", "marriageList"];
        const results = await Promise.all(requests);
        results.forEach((result, index) => {
          if (result.data.code === 0) {
            this.dicts[dictKeys[index]].push(...result.data.data);
          } else {
            console.log(`获取${dictKeys[index]}失败: ${result.data.msg}`);
          }
        });
      } catch (error) {
        console.log("获取字典失败: ", error);
      }
    },
    handleCancle() {
      this.resetData();
      this.$emit("handleClose");
    },

    resetData() {
      this.formData = {};
    },
    // 开启加载动画
    startLoading() {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: "正在加载……", // 加载中需要显示的文字
        background: "rgba(0,0,0,.7)", // 背景颜色
      });
    },
    // 结束加载动画
    endLoading() {
      if (loading) {
        loading.close();
      }
    },
  },
};
</script>
      
        <style lang="scss" scoped>
@import "@/assets/scss/modules/management/form-main.scss";
@import "@/assets/scss/modules/visual/a_customize.scss";
@import "@/assets/scss/modules/management/detail-main.scss";

.form-item::v-deep .el-form-item__label {
  color: #fff;
}
.form-item {
  .el-radio {
    color: #fff;
  }
  .el-checkbox {
    color: #fff;
  }
}

.wd-wr {
  width: 100%;
  /deep/ .el-form-item__content {
    width: calc(100% - 148px);
  }
}
.info-prop {
  margin-top: 29px;
  .info-title-2 {
    width: 100px;
    text-align: right;
    display: inline-block;
    padding-right: 10px;
  }
}
</style>
      
    
    