<template>
  <div id="detail">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="top">
          <detailTopInfo :topTitle="topTitle" :display="display" @goBack="goBack"></detailTopInfo>
        </el-col>
        <el-col :span="24" class="main">
          <tNewAssignForm :ruleForm="ruleForm" :originalData="originalData" :jgList="jgList" :jgproList="jgproList" @submitForm="submitForm" @resetForm="resetForm" @jgproListSelect="jgproListSelect"></tNewAssignForm>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import detailTopInfo from '@/layout/custom/detailTopInfo.vue';
  import tNewAssignForm from '@/layout/financingTNewAssign/tNewAssignForm.vue';
  import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
  const { mapActions: tNewAssign } = createNamespacedHelpers('tNewAssign');
  const { mapActions: dictionary } = createNamespacedHelpers('dictionary');
  const { mapActions: institution } = createNamespacedHelpers('institution');
  const { mapActions: financeclaims } = createNamespacedHelpers('financeclaims');
  const { mapActions: claimneed } = createNamespacedHelpers('claimneed');
  const { mapActions: tUrgeHandle } = createNamespacedHelpers('tUrgeHandle');
  const { mapActions: follow } = createNamespacedHelpers('follow');
  const { mapActions: intelligentDocking } = createNamespacedHelpers('intelligentDocking');
  const { mapActions: intelligentFollow } = createNamespacedHelpers('intelligentFollow');

  export default {
    name: 'detail',
    props: {},
    components: {
      detailTopInfo, //头部导航
      tNewAssignForm,
    },
    data: () => ({
      display: 'block',
      topTitle: '我要融资指派',
      ruleForm: {},
      originalData:{},
      jgList:[],
      jgproList:[],
      loading: true,
      subject_classification_list: [],
      subject_headings_list: [],
      company_description_list: [],
    }),
    created() {
      console.log(this.demand_id);
      this.select();
    },
    computed: {
      demand_id() {
        return this.$route.query.demand_id;
      },
      ...mapState(['user']),
    },
    methods: {
      ...tNewAssign({create:'create',delete:'delete',update:'update',fetch:'fetch',query:'financingSelect'}),
      ...dictionary({dictQuery:'query'}),
      ...institution({institutionQuery:'query'}),
      ...financeclaims({ financeclaimsSearchpro: 'searchpro'}),
      ...claimneed({ claimneedFetch: 'fetch',claimneedUpdate: 'update'}),
      ...tUrgeHandle([ 'originalQuery']),
      ...follow({ followQuery: 'query'}),
      ...intelligentDocking({ intelligentDockingFetch: 'fetch',intelligentDockingUpdate: 'update'}),
      ...intelligentFollow({ intelligentFollowQuery: 'query'}),
      async jgListSelect() {
        const res = await this.institutionQuery({ skip:0, limit:100});
        this.$set(this, `jgList`, res.data);
        console.log(this.jgList);

      },
      async jgproListSelect({jg_id}) {
        const res = await this.financeclaimsSearchpro({uid:jg_id});
        this.$set(this, `jgproList`, res.data);
        console.log(this.jgproList);
      },
      // 返回
      goBack() {
        this.$router.go(-1);
      },
      async select() {
        if (this.demand_id) {
          const res = await this.query({ skip:0, limit:1, demand_id: this.demand_id});
          this.$set(this, `ruleForm`, res.data[0]);
          this.$set(this, `originalData`, JSON.parse(JSON.stringify(res.data[0])));

          this.jgListSelect();
          this.jgproListSelect(this.originalData);
        }
      },
      filterDict(dict){
        return dict.filter((val) => {
          return val.name !== '不限';
        });
      },
      async selectDict(arr) {
        for (let value of arr) {
          let res = await this.dictQuery({skip:0,limit:100,type:value});
          this.$set(this, `${value}_list`, this.filterDict(res.data));
        }
      },
      // 提交
      async submitForm({ data }) {

        let resOriginalQuery = await this.originalQuery({skip:0,limit:1,type:'2',demand_id:data.demand_id});
        if(resOriginalQuery.total === 1){
          this.$message.error('已催办,无法指派！');
          this.resetForm();
          return ;
        }

        let resFollowQuery = await this.intelligentFollowQuery({skip:0,limit:1,intelligentId:data.demand_id});
        if(resFollowQuery.total === 1){
          this.$message.error('金融机构客户经理已接单,无法指派！');
          this.resetForm();
          return ;
        }

        let resFetch = await this.intelligentDockingFetch(data.demand_id);
        if(!resFetch.data){
          this.$message.error('指派失败');
          this.resetForm();
          return ;
        }
        if(resFetch.data.refuse_times > 4){
          this.$message.error('金融机构已拒绝5次,无法指派！');
          this.resetForm();
          return ;
        }

        let dataUpdate = resFetch.data;
        dataUpdate.jg_id = data.jg_id;
        dataUpdate.cid =data.jg_pro_id;
        dataUpdate.refuse_times = dataUpdate.refuse_times + 1;
        dataUpdate.ids.push({jr_id:data.jg_id,reason:'指派操作'});
        let resUpdate = await this.intelligentDockingUpdate(dataUpdate);
        if(!resUpdate.data){
          this.$message.error('指派失败');
          this.resetForm();
          return ;
        }

        data.type = '2';
        data.government_id = this.user.uid;
        data.government_name = this.user.name;
        data.company_type = '1';
        console.log(data);
        let res = await this.create(data);
        this.$checkRes(res, '指派成功', '指派失败');
        this.resetForm();

      },
      // 取消
      resetForm() {
        this.$router.push({ path: '/financingTNewAssign/index' });
      },
    },
  };
</script>

<style lang="less" scoped>
  .top {
    height: 50px;
    margin: 0 0 10px 0;
  }
  .main {
    min-height: 765px;
    background: #ffffff;
    padding: 20px;
  }
</style>
