<template>
  <app-dialog v-model="visible" :loading="loading" append-to-body class="detail-dialog" title="资源复制" width="70%" @cancel="onCancel" @confirm="onConfirm">
    <!--    <div style="padding-bottom: 10px">-->
    <!--      <el-button icon="el-icon-plus" size="mini" type="primary" @click="resourceSelection">选择资源</el-button>-->
    <!--      <span>（注：点击选择资源进行复制）</span>-->
    <!--    </div>-->
    <el-form ref="form" :model="form" :rules="rules" class="demo-form ml-150 mr-150" label-width="120px">
      <el-divider content-position="left"
        ><i class="el-icon-document" style="color: rgb(24, 144, 255)" /><span class="divider-top">数据资源项</span>
      </el-divider>
      <div class="form-content ml-120">
        <el-row :gutter="20">
          <app-filter-item :span="13" label="表资源名称" prop="name">
            <el-input v-model="form.name" clearable placeholder="请输入" />
          </app-filter-item>
          <app-filter-item :span="13" label="表资源编码" prop="code">
            <el-input v-model="form.code" clearable placeholder="请输入" />
          </app-filter-item>
          <app-filter-item :span="13" label="数据库表名" prop="tableNameEn">
            <el-input v-model="form.tableNameEn" clearable placeholder="请输入" />
          </app-filter-item>
          <app-filter-item :span="24" label="资源提供方式" prop="source">
            <el-radio-group v-model="form.source">
              <el-radio-button v-for="radio in DICT.SOURCE_PROVIDED_TYPE" :key="radio.value" :label="radio.value">
                {{ radio.label }}
              </el-radio-button>
            </el-radio-group>
          </app-filter-item>
        </el-row>
        <!--        <el-row :gutter="20">-->
        <!--          <app-filter-item :span="12" label="实体表中文名" prop="tableNameCh">-->
        <!--            <el-input v-model="form.tableNameCh" clearable placeholder="请输入" />-->
        <!--          </app-filter-item>-->

        <!--          <app-filter-item :span="12" label="实体表英文名" prop="tableNameEn">-->
        <!--            <el-input v-model="form.tableNameEn" clearable placeholder="请输入">-->
        <!--              <template #append>-->
        <!--                <el-button size="mini" type="primary" @click="checkAccount">验证</el-button>-->
        <!--              </template>-->
        <!--            </el-input>-->
        <!--          </app-filter-item>-->
        <!--        </el-row>-->
        <el-row :gutter="20">
          <app-filter-item :span="12" label="实体表类型" prop="tableType">
            <el-radio-group v-model="form.tableType">
              <el-radio-button v-for="radio in DICT.TABLE_TYPE" :key="radio.value" :label="radio.value">{{ radio.label }} </el-radio-button>
            </el-radio-group>
          </app-filter-item>
          <app-filter-item :span="20" label="实体表描述" prop="tableDesc">
            <el-input v-model="form.tableDesc" :rows="4" clearable placeholder="请输入" type="textarea" />
          </app-filter-item>
        </el-row>
      </div>

      <copy-resource-dialog v-model="resourceVisable" @change="currentChange" />
    </el-form>
  </app-dialog>
</template>

<script>
import dialogMixin from '@/mixins/dialog.js';
import CopyResourceDialog from './CopyResourceDialog.vue';
import companyApi from '@/api/business/dataCollectCompany';
import DICT from '@/utils/dict';

export default {
  name: 'CopyDialog',
  components: { CopyResourceDialog },
  mixins: [dialogMixin],
  props: {
    clusterId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      DICT,
      form: {
        source: '1',
        tableType: '1',
        sourceType: '1',
      },
      rules: {
        name: [{ required: true, message: '单位名称不能为空' }],
        code: [{ required: true, message: '单位类型不能为空' }],
        source: [{ required: true, message: '资源提供方式不能为空' }],
        sourceType: [{ required: true, message: '系统编码不能为空' }],
        tablePrefixCode: [{ required: true, message: '提供方表前缀名不能为空' }],
        tableNameCh: [
          { required: true, message: '实体中文名表不能为空' },
          { pattern: /[\u4e00-\u9fa5]/g, message: '必须为汉字' },
        ],
        tableNameEn: [
          { required: true, message: '实体表英文名不能为空' },
          { pattern: /^[A-Za-z_]+$/g, message: '必须为英文' },
        ],
        tableCode: [
          { required: true, message: '数据库表名不能为空' },
          { pattern: /^[A-Za-z]+$/g, message: '必须为英文' },
        ],
        tableType: [{ required: true, message: '请选择实体表类型' }],
      },
      loading: false,
      resourceVisable: false,
    };
  },
  methods: {
    async checkAccount() {
      const { tableNameEn } = this.form;
      if (!tableNameEn) {
        this.$message.error('请输入实体表英文名');
        return;
      }
      try {
        this.loading = true;
        await companyApi.checkSurfaceName({ tableNameEn: tableNameEn, clusterId: this.clusterId });
        this.$message.success('验证通过');
      } catch (error) {
        console.log(error);
      } finally {
        this.loading = false;
      }
    },
    resourceSelection() {
      this.resourceVisable = true;
    },
    initData(data) {
      console.log('-------', data);
      this.form = {
        sourceTableId: data.id,
        name: data.name,
        code: data.code,
        source: data.source,
        tableNameCh: data.tableNameCh,
        tableNameEn: data.tableNameEn,
        tableCode: data.tableCode,
        tableType: data.tableType,
        tableDesc: data.tableDesc,
      };
      // const orgForm = this.$options.data().form;
      // this.form = { ...orgForm, ...data };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    onCancel() {
      this.visible = false;
    },
    currentChange(row) {
      this.form = {
        sourceTableId: row.id,
        name: row.name,
        code: row.code,
        source: row.source,
        tableNameCh: row.tableNameCh,
        tableNameEn: row.tableNameEn,
        tableCode: row.tableCode,
        tableType: row.tableType,
        tableDesc: row.tableDesc,
      };
    },
    async onConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            const params = { ...this.form, clusterId: this.clusterId };
            this.loading = true;
            await companyApi.addSurfaceSource(params);
            this.$message.success('操作成功');
            this.onCancel();
            this.$emit('confirm', { ...params });
          } catch (e) {
            console.log(e);
          } finally {
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form-content {
  width: 96%;
  //margin: 0 auto;
}

.demo-form {
  border: solid 1px #eff0f5;
  height: 98%;
}
</style>
