<template>
  <el-dialog
    title="新建"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="NCC-dialog NCC-dialog_center"
    lock-scroll
    width="60%"
    v-loading="loading"
    :modal="false"
  >
    <el-row :gutter="15" class="NCC-dialog-content" v-show="!isDetail">
      <el-form
        ref="elForm"
        :model="dataForm"
        size="small"
        label-width="180px"
        label-position="right"
        :disabled="!!isDetail"
        :rules="rules"
      >
        <el-col :span="24" v-if="false">
          <el-form-item label="主键" prop="id">
            <el-input v-model="dataForm.id" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="来源登记" prop="sourceRegistration">
            <el-input v-model="dataForm.sourceRegistration" placeholder="请输入来源登记" clearable :disabled="formType != 0"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="运营主体" prop="registeredEntity">
            <el-input v-model="dataForm.registeredEntityStr" placeholder="" clearable disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="案件名称" prop="systemName">
            <el-input v-model="dataForm.systemNameStr" placeholder="" clearable disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="应用类别" prop="category">
            <el-radio-group v-model="dataForm.category" disabled>
              <el-radio v-for="(item, index) in platformTypeOptions" :key="index" :label="item.Id">{{ item.FullName }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备案号" prop="recordNumber">
            <el-input v-model="dataForm.recordNumber" placeholder="请输入备案号" clearable :disabled="formType == 2"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="isSHILevel">
          <el-form-item label="所属区县" prop="districtCounty">
              <el-radio-group
                v-model="dataForm.districtCounty"
                placeholder="请选择所属区县"
                style="line-height: 22px"
                :disabled="formType == 2"
              >
                <el-radio v-for="v in areaOptions" :key="v.id" :label="v.id">{{
                  v.fullName
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="违法原因" prop="illegalReasons">
            <el-input type="textarea" v-model="dataForm.illegalReasons" placeholder="请输入违法原因" clearable maxlength="200" :disabled="formType == 2"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="处罚要求" prop="punishmentRequirements">
            <el-input type="textarea" v-model="dataForm.punishmentRequirements" placeholder="请输入处罚要求" clearable maxlength="200" :disabled="formType == 2"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="处罚内容" prop="punishmentContent">
            <el-input type="textarea" v-model="dataForm.punishmentContent" placeholder="请输入处罚内容" clearable maxlength="200" :disabled="formType == 2"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="isSHILevel">
          <el-form-item label="执法方式" prop="enforceType">
            <el-radio-group v-model="dataForm.enforceType">
              <el-radio label="柔性执法">柔性执法</el-radio>
              <el-radio label="行政执法">行政执法</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="!isSHILevel && formType == 2">
          <el-form-item label="执法说明" prop="enforcecontent">
            <el-input type="textarea" v-model="dataForm.enforcecontent" placeholder="请输入执法说明" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <!-- 立案审批、调查（询问）笔录、案件处理意见、执法约谈、行政处罚意见告知、行政处罚决定、行政处罚结案报告、其他（每个流程都为填空+上传附件，非必填） -->
        <el-col :span="24">
          <el-form-item label="立案审批" prop="registerApproval">
            <el-input type="textarea" v-model="dataForm.registerApproval" placeholder="请输入立案审批" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="立案审批附件" prop="registerApprovalFileModel">
            <NCC-UploadFz v-model="dataForm.registerApprovalFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调查（询问）笔录" prop="record">
            <el-input type="textarea" v-model="dataForm.record" placeholder="请输入调查（询问）笔录" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调查（询问）笔录附件" prop="recordFileModel">
            <NCC-UploadFz v-model="dataForm.recordFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="案件处理意见" prop="caseHandlingOpinions">
            <el-input type="textarea" v-model="dataForm.caseHandlingOpinions" placeholder="请输入案件处理意见示例" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="案件处理意见附件" prop="caseHandlingOpinionsFileModel">
            <NCC-UploadFz v-model="dataForm.caseHandlingOpinionsFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="执法约谈" prop="enforcementRecords">
            <el-input type="textarea" v-model="dataForm.enforcementRecords" placeholder="请输入执法约谈" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="执法约谈附件" prop="enforcementRecordsFileModel">
            <NCC-UploadFz v-model="dataForm.enforcementRecordsFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行政处罚意见告知" prop="noticeLetter">
            <el-input type="textarea" v-model="dataForm.noticeLetter" placeholder="请输入行政处罚意见告知" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行政处罚意见告知附件" prop="noticeLetterFileModel">
            <NCC-UploadFz v-model="dataForm.noticeLetterFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行政处罚决定" prop="punishmentDecision">
            <el-input type="textarea" v-model="dataForm.punishmentDecision" placeholder="请输入行政处罚决定" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行政处罚决定附件" prop="punishmentDecisionFileModel">
            <NCC-UploadFz v-model="dataForm.punishmentDecisionFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行政处罚结案报告" prop="closingReport">
            <el-input type="textarea" v-model="dataForm.closingReport" placeholder="请输入行政处罚结案报告" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行政处罚结案报告附件" prop="closingReportFileModel">
            <NCC-UploadFz v-model="dataForm.closingReportFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他" prop="other">
            <el-input type="textarea" v-model="dataForm.other" placeholder="请输入其他" clearable maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他附件" prop="otherFileModel">
            <NCC-UploadFz v-model="dataForm.otherFileModel" :fileSize="5" sizeUnit="MB" :limit="9" buttonText="点击上传" />
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <template>
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" v-if="!isDetail && formType != '2'" @click="submit(1)" :loading="btnLoading">{{isSHILevel ? '下 发' : '上 报'}}</el-button>
        <el-button type="info" v-if="!isDetail && formType != '2'" @click="submit(0)" :loading="btnLoading">保 存</el-button>
      </template>
    </span>
  </el-dialog>
</template>
<script>
import infoMixin from "@/mixins/info";
import request from "@/utils/request";
import { getSystemDetail } from "@/api/baseData/info";
import { getCompanyInfoList, getCompanyInfoById } from "@/api/baseData/company";
import { dynamicTextUP } from "@/filters/index";

export default {
  mixins: [infoMixin],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      formType: false, //  0 新增 1 编辑 2 处理
      dataForm: {
		    id: undefined,
        sourceRegistration: undefined,
		    registrationTime: undefined,
		    systemName: undefined,
		    registeredEntity: undefined,
		    category: undefined,
		    recordNumber: undefined,
        registeredEntityStr: undefined,
		    districtCounty: undefined,
        systemNameStr: undefined,
		    illegalReasons: undefined,
		    punishmentRequirements: undefined,
		    punishmentContent: undefined,
		    state: undefined,
        enforceType: undefined,
        enforcecontent: undefined,
        isHaveSystem: undefined,
        registerApproval: undefined,
        registerApprovalFileModel: [],
        record: undefined,
        recordFileModel: [],
        caseHandlingOpinions: undefined,
        caseHandlingOpinionsFileModel: [],
        enforcementRecords: undefined,
        enforcementRecordsFileModel: [],
        noticeLetter: undefined,
        noticeLetterFileModel: [],
        punishmentDecision: undefined,
        punishmentDecisionFileModel: [],
        closingReport: undefined,
        closingReportFileModel: [],
        other: undefined,
        otherFileModel: [],
	    },
      rules: {
        sourceRegistration: [
          { required: true, message: "请输入来源登记", trigger: "bulr" }
        ],
        systemName: [
          { required: true, message: "请选择应用名称", trigger: "bulr" }
        ],
        registeredEntity: [
          { required: true, message: "请选择运营主体", trigger: "bulr" }
        ],
        districtCounty: [
          { required: true, message: "请选择所属区域", trigger: "change" }
        ],
        enforceType: [
          { required: true, message: "请选择执法方式", trigger: "bulr" }
        ],
        enforcecontent: [
          { required: true, message: "请填写执法说明", trigger: "bulr" }
        ],
      },
      systemOption: [],
      system_loading: false,
      areaOptions: [],
      btnLoading: false,
      companyOptions: [],
      platformTypeOptions: [],
    };
  },
  computed: {
    isSHILevel() {
      // 判断角色是否为‘市级办公室’
      return this.$store.state.user.islader;
    },
  },
  watch: {},
  created() {
    this.getplatformTypeOptions();
    this.initSystemTypeList();
    this.initAreaTypeList();
    // this.initCompanyList();
  },
  mounted() {},
  methods: {
    async getplatformTypeOptions() {
      let list = this.$store.state.meta.system;
      !list && (list = await this.$store.dispatch("getTypeListByCode", "system"));
      this.platformTypeOptions = list;
    },
      // 请求公司列表
    async initCompanyList() {
      request({
        url: `/Extend/basecomapnyinfo/GetNoPagingList`,
        method: "GET",
      }).then(({data}) => {
        this.companyOptions = data;
      })
    },
    // 获取系统列表
    getSystemSelect(companyId) {
      this.system_loading = true;
      request({
        url: "/Extend/basesysteminfo/GetNoPagingList",
        method: "get",
        params: { companyId }
      }).then(({data}) => {
        this.systemOption = data;
        this.system_loading = false;
      })
    },
    selectBlur(e) {
      let value = e.target.value;
      if(!value) return;
      this.dataForm.systemName = e.target.value;
      this.platformNameChange(value, 'blur');
    },
    platformNameChange(val, type) {
      let obj = this.systemOption.find(v => type == 'blur' ? val == v.fullName : val == v.id);
      console.log(obj);
      this.dataForm.isHaveSystem = obj ? true : false;
      this.dataForm.systemName = obj ? obj.id : val;
      this.dataForm.category = obj ? this.platformTypeOptions.find(v => v.Id == obj.systemType || v.FullName == obj.systemType).Id : '';
      // this.dataForm.recordNumber = obj ? obj.platform : '';
    },
    // 系统名称改变
    systemChange(val) {
      getSystemDetail(val).then(({data}) => {
        this.dataForm.registeredEntity = data.companyId;
        this.dataForm.category = dynamicTextUP(data.systemType, this.systemTypeOptions);
        this.dataForm.recordNumber = data.specialRecordNumber || '--';
        this.dataForm.districtCounty = data.areaId;
      })
    },
    visibleNameChange(val) {
      if (val) return
      let input = this.$refs.select.$children[0].$refs.input;
      input.blur();
    },
    companyChange(val, type) {
      let obj = this.companyOptions.find(v => val == v.id);
      obj ? this.getSystemSelect(val) : (this.systemOption == []);
      this.dataForm.systemName = '';
      this.dataForm.category = '';
    },
    // 运营主体改变
    selectCompanyBlur(e) {
      let value = e.target.value;
      if(!value) return;
      this.dataForm.registeredEntity = e.target.value;
    },
    visibleNameCompanyChange(val) {
      if (val) return
      let input = this.$refs.companySelect.$children[0].$refs.input;
      input.blur();
    },
    goBack() {
      this.$emit("refresh");
    },
    init(row, isDetail, type) {
      // type 0 上报/下派 1 处理
      this.formType = type || '0';
      this.isDetail = isDetail || false;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        this.dataForm.registeredEntity = row.companyId;
        this.dataForm.registeredEntityStr = row.companyName;
        this.dataForm.systemName = row.id;
        this.dataForm.systemNameStr = row.systemName;
        this.dataForm.category = row.systemType;
      });
    },
    // 上报/下派
    submit(type) {
      // type: 1 上报 0 保存
       // status 状态 0保存/1办结
      let obj = {
        ...this.dataForm,
        isReport: type,
      }
			this.$refs['elForm'].validate((valid) => {
        if(valid) {
          !this.dataForm.id ? this.toRequest('/Extend/BaseCaseHandling', 'POST', obj) : this.toRequest(`/Extend/BaseCaseHandling/${this.dataForm.id}`, 'PUT', obj);
        }
      })
    },
    // 处理
    handle(status) {
      this.$refs['elForm'].validate((valid) => {
        if(valid) {
          this.btnLoading = true;
          this.dataForm.status = status;
          request({
            url: `/Extend/basecasehandling/HandlingCase/${this.dataForm.id}`,
            method: "POST",
            data: this.dataForm,
          }).then((res) => {
            this.visible = false;
            this.$emit("refresh", true);
            this.$message({
              type: "success",
              message: res.msg,
            })
          }).catch(() => {
            this.btnLoading = false;
          })
        }
      })
    },
    toRequest(url, method, data) {
      this.btnLoading = true;
      request({
        url,
        method,
        data,
      }).then((res) => {
        this.$message({
          type: "success",
          message: res.msg,
          onClose: () => {
            this.visible = false;
            this.$emit("refresh", true);
          },
        });
      }).catch(() => {
        this.btnLoading = false;
      });
    }
  },
};
</script>
