<template>
  <ts-form :data="data" :rules="rules" @submit="submitEvent" ref="xForm">
    <div slot="formCon">
      <ts-form-item field="appType" v-if="saveType=='form'">
        <ts-radio
          v-model="data.appType"
          title="类别"
          :isDefaultFirst="true"
          :data="appType"
          :emphasize="true"
          :disabled="oldData.appType == '3'||this.biModel == 'pivot'"
        ></ts-radio>
      </ts-form-item>
      <template v-if="data.appType == 2&&(data.versionCode&&data.versionCode!='草稿'||isPublish)">
        <ts-form-item field="roleId">
          <ts-select
            ref="roleSelect"
            v-model="data.roleId"
            title="角色"
            :source="roledata"
            :key="oldData.id+'4444'"
            @selected="selectedRole"
          ></ts-select>
        </ts-form-item>
        <ts-form-item field="add">
          <ts-select
            ref="systemSelect"
            v-model="data.sysCode"
            title="应用"
            :source="systemData"
            :key="oldData.id+'2444'"
          ></ts-select>
        </ts-form-item>
      </template>
      <ts-form-item field="themeClassifyValue">
        <ts-select
          :emphasize="true"
          :source="themeClassifyValueSource"
          v-model="data.themeClassifyValue"
          title="主题分类"
          name="themeClassifyValue"
          :key="oldData.id+'3333'"
          @selected="selectedThemeClassify"
        ></ts-select>
      </ts-form-item>
      <ts-form-item field="queryName" v-if="saveType == 'dataSource'">
        <ts-input
          v-model="data.queryName"
          title="服务名称"
          :emphasize="true"
          placeholder
          name="queryName"
        ></ts-input>
      </ts-form-item>
      <ts-form-item field="designName" v-else>
        <ts-input
          v-model="data.designName"
          title="报表名称"
          :emphasize="true"
          placeholder
          name="designName"
        ></ts-input>
      </ts-form-item>
      <ts-form-item v-if="saveType == 'dataSource'">
        <ts-switch
          title="是否分页"
          v-model="data.page"
          on-label="是"
          off-label="否"
          :onValue="1"
          :offValue="0"
        ></ts-switch>
      </ts-form-item>
      <template
        v-if="sysRoleCode.indexOf(platSysRoleColde)>-1||sysRoleCode.indexOf(superSysRoleColde)>-1"
      >
        <ts-form-item field="systemService" v-if="saveType == 'dataSource'">
          <ts-switch
            title="系统服务"
            v-model="data.systemService"
            on-label="是"
            off-label="否"
            on-value="01"
            off-value="02"
            :key="'$$$$'+oldData.id"
          >
            <ts-popover slot="title">
              只有管理员具备此功能，
              <br />设置后，此记录对非管理员身份不可见
            </ts-popover>
          </ts-switch>
        </ts-form-item>
        <ts-form-item field="systemReport" v-else>
          <ts-switch
            title="系统报表"
            v-model="data.systemReport"
            on-label="是"
            off-label="否"
            on-value="01"
            off-value="02"
            :key="'4444$$$$'+oldData.id"
          >
            <ts-popover slot="title">
              只有管理员具备此功能，
              <br />设置后，此记录对非管理员身份不可见
            </ts-popover>
          </ts-switch>
        </ts-form-item>
      </template>

      <ts-form-item v-if="isPublish">
        <ts-textarea title="版本说明" v-model="data.versionNoteInfo"></ts-textarea>
      </ts-form-item>
      <template v-if="saveType=='form'&&data.appType == 1">
        <ts-form-item field="busType">
          <ts-select
            :source="themeClass"
            placeholder="请选择业务分类"
            v-model="data.busType"
            title="业务分类"
            :emphasize="true"
            @selected="refresh($event,'busType')"
          ></ts-select>
        </ts-form-item>
        <ts-form-item field="demandDept">
          <ts-select
            :source="demandDeptSource"
            placeholder="请选择需求部门"
            v-model="data.demandDept"
            title="需求部门"
            :emphasize="true"
          ></ts-select>
        </ts-form-item>
        <ts-form-item field="dimension">
          <ts-radio
            title="统计维度"
            v-model="data.dimension"
            :data="dimensionSource"
            type="group"
            :isDefaultFirst="true"
            :emphasize="true"
          ></ts-radio>
        </ts-form-item>
        <ts-form-item field="designerDesc">
          <ts-textarea
            title="描述"
            v-model="data.designerDesc"
            placeholder="简要描述报表的功能/用途/目的等内容"
            style="height:100px"
          ></ts-textarea>
        </ts-form-item>
      </template>
    </div>
  </ts-form>
</template>

<script>
//防止重复提交的指令
export default {
  name: 'biSave',
  props: {
    biModel: {
      type: String,
      default: ''
    },
    isPublish: {
      type: Boolean,
      default: false
    },
    oldData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    saveType: {
      type: String,
      default: 'form'
    }
  },
  data() {
    return {
      sysRoleCode:  global.userInfo?global.userInfo.sysRoleCode: '',
      platSysRoleColde:  window.platSysRoleColde,
      superSysRoleColde:  window.superSysRoleColde,
      data: { ...this.oldData },
      themeClassCodes: [
        'PLAT_USER_EDITOR_REPORT',
        'PLAT_USER_EDITOR_INDEX',
        'PLAT_USER_EDITOR_BUSINESS',
        'PLAT_USER_EDITOR_FORM'
      ],
      rules: {
        appType: [{ required: false, message: '请选择类别' }],
        roleName: [{ required: true, message: '请输入角色' }],
        designName: [{ required: true, message: '请输入报表名称' }],
        themeClassifyValue: [{ required: true, message: '请输入主题分类' }],
        queryName: [{ required: true, message: '请输入服务名称' }]
      },
      appType_: this.oldData.appType,
      appType: [
        { value: '1', text: '报表' },
        { value: '2', text: '门户' },
        { value: '4', text: '表单' }
      ],
      themeClass: this.getDictSource('BI_BUSTYPE'),
      demandDeptSource: this.getDictSource('BI_DEMANDDEPT'),
      roledata: {
        dataType: 'json',
        url: '/ts-system/api/role/tree',
        method: 'get',
        data: [],
        code: 'code',
        text: 'name',
        children: 'children',
        tree: true,
        treeType: 'checkbox',
        isLoading: false,
        isClose: true,
        noParent: true
        // value: {
        //   name: this.oldData.roleName,
        //   code: this.oldData.roleId
        // }
      },
      systemData: {
        dataType: 'json',
        url: '/ts-system/api/ThpsAppModel/list',
        method: 'get',
        code: 'sysCode',
        text: 'sysName',
        choice: true,
        isReq: true,
        value: {
          // name: this.oldData.roleName,
          // code: this.oldData.roleId
        }
      },
      dimensionSource: [
        {
          value: '00',
          text: '临床'
        },
        {
          value: '01',
          text: '经管'
        }
      ]
    }
  },
  computed: {
    themeClassifyValueSource() {
      let url = '/ts-system/api/dict/item/list/'
      if (this.saveType == 'dataSource') {
        // 自定义服务
        url = url + 'PLAT_USER_SERVICE_TOPIC_CATEGORY'
      } else {
        url = this.appType_
          ? url + this.themeClassCodes[Number(this.appType_ || '1') - 1]
          : url + 'PLAT_USER_EDITOR_REPORT'
      }
      return {
        dataType: 'json',
        url: url,
        method: 'get',
        data: {},
        page: 'pageNo',
        code: 'itemCode',
        text: 'itemName',
        condition: 'condition',
        value: {
          name: this.oldData.themeClassifyName || this.oldData.topicCateName,
          code: this.oldData.themeClassifyValue || this.oldData.topicCateCode
        }
      }
    }
  },
  created() {},
  mounted() {
    this.data.sysCode = this.data.sysCode || ''
    if (this.biModel == 'custom'||this.biModel == 'pivot') {
      this.appType.push({ value: '3', text: '业务组件' })
    }
    if (
      this.saveType == 'form' &&
      this.data.appType == 1 &&
      !this.data.dimension
    ) {
      this.data.dimension = '00'
    }
  },
  methods: {
    // 表单提交验证
    submitEvent() {
      this.addSave()
    },
    addSave() {
      this.$emit('saveClick', { formData: this.data, type: 'add' })
    },
    selectedThemeClassify(row) {
      this.data.themeClassifyName = row.itemName
    },
    // 得到字段源
    getDictSource(type, choice) {
      return {
        dataType: 'json',
        url: '/ts-system/api/dict/item/list/' + type,
        method: 'get',
        data: {},
        page: 'pageNo',
        code: 'itemCode',
        text: 'itemName',
        condition: 'condition',
        choice: choice || false
      }
    },
    selectedRole(row) {
      this.$nextTick(() => {
        this.data.roleName = this.$refs.roleSelect.textValue // 赋值当前名称
      })
    },
    refresh(ev, key) {
      this.$nextTick(() => {
        switch (key) {
          case 'busType':
            this.data.busTypeName = ev.itemName
            break
        }
      })
    }
  },
  watch: {
    oldData: {
      handler(n) {
        this.data = { ...n }
        if (
          this.saveType == 'form' &&
          this.data.appType == 1 &&
          !this.data.dimension
        ) {
          this.data.dimension = '00'
        }
      },
      deep: true
    },
    'data.appType': {
      handler(n) {
        this.appType_ = n
      },
      deep: true
    }
  }
}
</script>
<style lang="less" scoped>
</style>
