<template>
  <div class="app-container">
    <div class="tab-header webtop">
      <div>{{ viewTitle }}</div>
      <div>
        <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate()">保存</el-button>
        <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
      </div>
    </div>
    <div class="listconadd">
      <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
        <!--   <el-row style="border-top: 1px solid #e0e3eb">
          <el-col :span="8">
            <div class="span-sty">申请部门</div>
            <el-form-item><span class="addinputInfo">{{ formobj.deptName }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">申请人</div>
            <el-form-item><span class="addinputInfo">{{ formobj.createByName }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">申请日期</div>
            <el-form-item><span class="addinputInfo">{{ formobj.createStartDate }}</span></el-form-item>
          </el-col>
        </el-row> -->
        <el-row style="border-top: 1px solid #e0e3eb">
          <el-col :span="8">
            <div class="span-sty"><span class="must">*</span> 分公司</div>
            <el-form-item prop="useOrgName">
              <el-select class="addinputInfo addinputw" v-model="formobj.useOrgName" placeholder="请选择"
                @change="seleOrgName">
                <el-option v-for="item in orgList" :key="item.sid" :label="item.name" :value="item.sid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty"><span class="must">*</span> 审计人</div>
            <el-form-item prop="auditor"><el-input v-model="formobj.auditor" clearable placeholder=""
                class="addinputInfo addinputw" /></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty"><span class="must">*</span> 审计事项</div>
            <el-form-item prop="auditMatters"><el-input v-model="formobj.auditMatters" clearable placeholder=""
                class="addinputInfo addinputw" /></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="span-sty">备注</div>
            <el-form-item><el-input v-model="formobj.remarks" clearable placeholder=""
                class="addinputInfo addinputw" /></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="span-sty">附件</div>
            <el-form-item>
              <upload-img ref="uploadImg" class="addinputw addinputInfo" v-model="formobj.urls" :limit="1" bucket="map"
                :upload-data="{ type: '0001' }" @change="backData" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
  import req from '@/api/auditSupervision/auditSupervision'
  import uploadImg from '@/components/uploadFile/uploadImg'
  import {
    selectOrgSidList,
    getOrgSidByPath,
    fetchBySid
  } from '@/api/Common/dictcommons'
  import {
    getCurrentDate,
  } from '@/utils/index'

  export default {
    name: 'AuditSupervisionAdd',
    components: {
      uploadImg
    },
    data() {
      return {
        viewTitle: '',
        submitdisabled: false,
        formobj: {
          "sid": "",
          "useOrgName": "",
          "useOrgSid": "",
          "auditMatters": "",
          "createStartDate": getCurrentDate(),
          "createEndDate": "",
          "auditor": "",
          "auditResults": "",
          "remarks": "",
          "urls": [],
          "createBySid": window.sessionStorage.getItem('userSid'),
          "createByName": window.sessionStorage.getItem('name'),
          "createOrgSid": "",
          "createOrgName": '',
        },
        rules: {
          orgName: [{
            required: true,
            message: '请选择分公司',
            trigger: 'change'
          }],
          auditor: [{
            required: true,
            message: '审计人不能为空',
            trigger: 'blur'
          }],
          auditMatters: [{
            required: true,
            message: '审计项目不能为空',
            trigger: 'blur'
          }],
        },
        orgList: [],
      }
    },
    methods: {

      init() {
        selectOrgSidList().then((res) => {
          if (res.success) {
            console.log("selectOrgSidList", res);
            this.orgList = res.data
          }
        })

        getOrgSidByPath({
          orgPath: window.sessionStorage.getItem('defaultOrgPath')
        }).then((resp) => {
          if (resp.success) {
            this.formobj.createOrgSid = resp.data
            fetchBySid(resp.data).then((respsone) => {
              if (respsone.success) {
                this.formobj.createOrgName = respsone.data.name
              }
            })
          }
        })
      },
      backData(value) {

        console.log('backData》》》》', value)

        this.formobj.urls = value

      },
      showAdd() {
        this.viewTitle = '【新增】审计监管'
        this.$nextTick(() => {
          this.$refs['form_obj'].clearValidate()
        })
        this.init()
      },
      showEdit(sid) {
        this.viewTitle = '【编辑】审计监管'
        this.$nextTick(() => {
          this.$refs['form_obj'].clearValidate()
        })
        this.init()
        req.init(sid).then((res) => {
          if (res.success) {
            this.formobj = res.data
          }
        })
      },

      saveOrUpdate() {
        this.$refs['form_obj'].validate((valid) => {
          if (valid) {
            this.submitdisabled = true
            req.saveOrUpdate(this.formobj).then((res) => {
              if (res.success) {
                this.$message({
                  showClose: true,
                  type: 'success',
                  message: '保存成功'
                })
                this.handleReturn('true')
              } else {
                this.submitdisabled = false
              }
            }).catch(() => {
              this.submitdisabled = false
            })
          }
        })
      },

      handleReturn(isreload) {
        if (isreload === 'true') this.$emit('reloadlist')
        this.formobj = {
          "sid": "",
          "useOrgName": "",
          "useOrgSid": "",
          "auditMatters": "",
          "createStartDate": getCurrentDate(),
          "createEndDate": "",
          "auditor": "",
          "auditResults": "",
          "remarks": "",
          "urls": [],
          "createBySid": window.sessionStorage.getItem('userSid'),
          "createByName": window.sessionStorage.getItem('name'),
          "createOrgSid": "",
          "createOrgName": '',
        }
        this.submitdisabled = false
        this.$emit('doback')
      },
      seleOrgName(val) {
        console.log("seleOrgName", val);
        const choose = this.orgList.filter((item) => item.sid == val)
        console.log('>>>>>>>>>seleOrgName', choose)
        this.formobj.useOrgSid = choose[0].sid
        this.formobj.useOrgName = choose[0].name
      }
    }
  }
</script>

<style scoped>
  /deep/ .el-form-item__error {
    margin-left: 120px !important;
  }

  .must {
    color: #f00;
  }

  .span-sty {
    width: 130px !important;
    align-items: center;
  }

  .addinputInfo {
    margin-left: 120px !important;
  }

  .formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
    margin-left: 120px !important;
    width: calc(100% - 115px);
  }

  .first_row {
    border-top: 1px solid #E0E3EB;
  }

  .titleOne {
    padding: 7px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .formaddcopy02 .el-row .el-col /deep/ .el-form-item .el-radio-group {
    display: inline;
    line-height: 1px;
    vertical-align: middle;
  }
</style>
