<template>
  <div class="app-container">
    <Form ref="formRef" :formList="formList" :ruleFormData="ruleForm" :config="config" @fnClick="callback"></Form>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import {addBusDeploy, getBusDeploy, updateBusDeploy, listAllBusinessList, getAllBusScriptName, reviewBusiness} from "@/api/disRevenue/earnManage"
  export default {
    name: 'BusinessIssuedDetails',
    components: {Form},
    dicts: ['eps_business_review_status'],
    props: {
      open: {
        type: String,
        default: () => {}
      },
      dialogRowData: {
        type: Object,
        default: (() => {})
      }
    },
    watch: {
      open: {
        handler(val) {
          if (val === 'type_false') {
            // 清空form
            this.$refs['formRef'].$refs.ruleForm.resetFields();
          }
        },
        deep: true,
        immediate: true
      },
    },
    data() {
      return {
        loading: true,
        ruleForm: {},
        formList: [],
        scriptList: {},
        paramsData: {},
        config: {},
        busNameList: {},
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.open) {
        this.paramsData = {};
        this.$set(this.ruleForm, 'deployDevice', this.dialogRowData.clientId);
      }
      if (this.paramsData && this.paramsData.readonly) {
        this.config = {
          buttonGroup: [{title: '返回', fnCode: 'goBack'}]
        };
      } else if (this.paramsData && this.paramsData.reviewStatus) {
        this.config = {
          buttonGroup: [
            {title: '返回', fnCode: 'goBack'},
            {title: '审核驳回', fnCode: 'rejection'},
            {title: '审核通过', fnCode: 'byMeans', type: 'primary'},
          ]
        };
      }
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      this.getBusinessNames();
      this.getBusScriptNames();
      this.fnFormList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        if (this.paramsData && this.paramsData.reviewStatus) {
          this.formList = [{
            config: {title: '基本信息', colSpan: 'disBlock', readonly: true},
            controls: {
              id: {label: 'ID',hidden: true},
              taskName: {label: '任务名称', span: 12, type: 'input', required: true},
              scriptName: {label: '脚本名称', span: 12, type: 'select', options: [], eventName: 'change', required: true},
              scriptPath: {label: '脚本文件地址', span: 12, type: 'input', required: true},
              scriptParams: {label: '脚本参数', span: 12, type: 'input'},
              businessName: {label: '业务名称', span: 12, type: 'select', options: [], required: true},
              submitBy: {label: '提交人', span: 12, type: 'input'},
              // reviewBy: {label: '审核人', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
              // reviewTime: {label: '审核时间', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
              updateTime: {label: '修改时间', span: 12, type: 'datetime'},
              createTime: {label: '创建时间', span: 12, type: 'datetime'},
              reviewStatus: {label: '审核状态', span: 12, type: 'select', options: this.dict.type.eps_business_review_status,},
              deployDevice: {label: '部署设备', span: 18, type: 'textarea', rows: 15, required: true},
            }
          },{
            config: {},
            controls: {
              reviewComment: {label: '审核意见', span: 18, type: 'textarea'},
            }
          }];
        } else {
          this.formList = [{
            config: {title: '基本信息', colSpan: this.paramsData && this.paramsData.readonly ? '' : 'disBlock', readonly: this.paramsData && this.paramsData.readonly},
            controls: {
              id: {label: 'ID',hidden: true},
              taskName: {label: '任务名称', span: 12, type: 'input', required: true},
              businessName: {label: '业务名称', span: 12, type: 'select', options: [], required: true},
              scriptName: {label: '脚本名称', span: 12, type: 'select', options: [], eventName: 'change', required: true},
              scriptPath: {label: '脚本文件地址', span: 12, type: 'input', required: true, disabled: true},
              scriptParams: {label: '脚本参数', span: 12, type: 'input'},
              submitBy: {label: '提交人', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
              reviewStatus: {label: '审核状态', span: 12, type: 'select', options: this.dict.type.eps_business_review_status, hidden: !(this.paramsData && this.paramsData.readonly)},
              reviewBy: {label: '审核人', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
              reviewTime: {label: '审核时间', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
              reviewComment: {label: '审核意见', span: 12, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
              createTime: {label: '创建时间', span: 12, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
              updateTime: {label: '修改时间', span: 12, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
              deployDevice: {label: '部署设备', span: 18, type: 'textarea', rows: 15, required: true},
            }
          }];
        }
      },
      // 获取详情
      getFormDataList(id) {
        getBusDeploy(id).then(val => {
          if (val && val.data) {
            val.data['scriptName'] = Number(val.data['scriptName']);
            this.ruleForm = val.data;
          }
        }).catch(() => {
          // this.$modal.msgError("操作失败")
        });
      },
      // 业务名称
      getBusinessNames() {
        listAllBusinessList().then(val => {
          this.formList[0].controls.businessName['options'] = val && val.data.map(item => {
            this.busNameList[item.businessName] = item;
            return Object.assign({label: item.businessName, value: item.businessName});
          });
        });
      },
      // 脚本名称
      getBusScriptNames() {
        getAllBusScriptName().then(val => {
          this.formList[0].controls.scriptName['options'] = val && val.data.map(item => {
            this.scriptList[item.scriptName] = item;
            return Object.assign({label: item.scriptName, value: item.scriptName});
          });
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'scriptName':
              if (dataVal) {
                let dataListVal = this.scriptList[dataVal];
                this.$set(this.ruleForm, 'scriptPath', dataListVal['scriptPath']);
                this.$set(this.ruleForm, 'scriptParams', dataListVal['defaultParams']);
              }
              break;
            case 'submit':
              this.loading = true;
              dataVal['businessCode'] = this.busNameList[dataVal.businessName].id;
              let fnType = addBusDeploy;
              if (dataVal && dataVal.id) {
                fnType = updateBusDeploy;
              }
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                if (this.open) {
                  this.$emit("dialogResult", {open: false});
                } else {
                  this.$router.push("/earnManage/businessIssued");
                }
                this.loading = false;
              }).catch(() => {
                this.$modal.msgError("操作失败");
                this.loading = false;
              });
              break;
            case 'rejection': // 审核驳回
                let params = {
                  id: dataVal.id,
                  reviewStatus: '3',
                  reviewComment: dataVal.reviewComment
                };
                reviewBusiness(params).then(res => {
                  this.$modal.msgSuccess(res.msg);
                  this.$router.push("/earnManage/businessIssued");
                }).catch(() => {
                  this.$modal.msgError("操作失败");
                });
                break;
            case 'byMeans': // 审核通过
              let paramsBy = {
                id: dataVal.id,
                reviewStatus: '2',
                reviewComment: dataVal.reviewComment
              };
              reviewBusiness(paramsBy).then(res => {
                this.$modal.msgSuccess(res.msg);
                this.$router.push("/earnManage/businessIssued");
              }).catch(() => {
                this.$modal.msgError("操作失败");
              });
                break;
            case 'cancel':
              if (this.open) {
                this.$emit("dialogResult", {open: false});
              } else {
                this.$router.push("/earnManage/businessIssued");
              }
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
