<template>
  <section class="Add">
    <ai-detail v-if="!id || isEdit==true">
      <template #title>
        <ai-title :title="params.id ? '编辑集体经济股权登记' : '新增集体经济股权登记'" :isShowBack="true" :isShowBottomBorder="true" @onBackClick="cancel(false)"></ai-title>
      </template>
      <template #content>
        <ai-card title="基本信息">
          <template #right v-if="params.id">
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" v-if="isEdit==true" @click="isEdit=false">取消</span>
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" v-if="isEdit==true" @click="confirm()">保存</span>
          </template>
          <template #content>
            <el-form ref="form" :model="form" :rules="rules" size="small"  label-width="150px">
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="集体经济组织" prop="usePerson">
                    <el-input v-model="form.usePerson" placeholder="请输入集体经济组织名称"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="统一信用社会代码" prop="houseNumber">
                    <el-input v-model="form.houseNumber" placeholder="请输入统一社会信用代码"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="所在地区" prop="areaName">
                <ai-area-select clearable always-show :instance="instance" v-model="form.areaId" :disabled-level="disabledLevel"></ai-area-select>
              </el-form-item>
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="股权证编号" prop="useCertificate">
                    <el-input v-model="form.useCertificate" placeholder="请输入股权证编号"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="权证照片" prop="fileList">
                <ai-uploader v-model="form.fileList" :instance="instance" :limit="9" isShowTip/>
              </el-form-item>
            </el-form>
          </template>
        </ai-card>

        <ai-card title="家庭持股情况">
          <template #right>
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" @click="isEdit=false">+添加股权人</span>
          </template>
          <template #content>
            <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                      @getList="getTableData" :col-configs="colConfigs" :dict="dict">
              <el-table-column slot="options" label="操作"  align="center">
                <template slot-scope="{ row }">
                  <el-button type="text" @click="toAdd(row.id)">详情</el-button>
                  <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </ai-table>
          </template>
        </ai-card>
      </template>
      <template slot="footer" class="footer">
        <el-button class="delete-btn footer-btn" @click="cancel(false)">取消</el-button>
        <el-button class="footer-btn" type="primary" @click="confirm()">保存</el-button>
      </template>
    </ai-detail>
    <ai-detail class="add" v-if="params.id && !isEdit">
      <template slot="title">
        <ai-title title="详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
      </template>
      <template slot="content">
        <ai-card title="合同信息">
          <template #right>
              <span style="color:#2266FF;cursor: pointer;font-size: 12px;" class="iconfont iconEdit" v-if="isEdit==false" @click="isEdit=true,form=data">修改</span>
            </template>
          <template #content>
            <ai-wrapper>
              <ai-info-item label="使用证书" :value="data.useCertificate"></ai-info-item>
              <ai-info-item label="所在地区" :value="data.areaName"></ai-info-item>
              <ai-info-item label="使用人" :value="data.usePerson"></ai-info-item>
              <ai-info-item label="家庭人口" :value="data.houseNumber"></ai-info-item>
              <ai-info-item label="身份证号" :value="data.idNumber"></ai-info-item>
              <ai-info-item label="土地所有权" :value="data.landOwnership">{{ dict.getLabel('landOwnership', data.landOwnership) }}</ai-info-item>
              <ai-info-item label="土地面积/㎡" :value="data.landArea"></ai-info-item>
              <ai-info-item label="土地四至" :value="data.landFourTo"></ai-info-item>
              <ai-info-item label="使用情况" :value="data.useSituation"></ai-info-item>
              <ai-info-item label="权证照片" :value="data.fileList"></ai-info-item>
            </ai-wrapper>
          </template>
        </ai-card>
      </template>
    </ai-detail>
  </section>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "Add",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
    params: Object,
  },
  data() {
    return { 
      id: '',
      form: {
        useCertificate: '',
        areaId: '',
        areaName: '',
        usePerson: '',
        houseNumber: '',
        idNumber: '',
        landOwnership: '',
        landArea: '',
        landFourTo: '',
        useSituation: '',
        fileList: '',

      },
      isEdit: false,
      radioFrom: '',
      page: {current: 1, size: 10, total: 0},
      tableData: [],
    }
  }, 
  computed: {
    rules() {
      return {
        useCertificate: [{required: true, message: '请输入使用证书', trigger: 'blur'}],
        areaName: [{required: true, message: '请选择所在地区', trigger: 'blur'}],
        usePerson: [{required: true, message: '请输入使用人', trigger: 'blur'}],
        houseNumber: [{required: true, message: '请输入家庭人口', trigger: 'blur'}],
        idNumber: [{required: true, message: '请输入身份证号', trigger: 'blur'}],
        landOwnership: [{required: true, message: '请输入土地所有权', trigger: 'blur'}],
        landArea: [{required: true, message: '请输入土地面积', trigger: 'blur'}],
        landFourTo: [{required: true, message: '请输入土地四至', trigger: 'blur'}],
        useSituation: [{required: true, message: '请输入使用情况', trigger: 'blur'}],
        fileList: [{required: true, message: '请上传图片', trigger: 'blur'}],
      }
    },
    ...mapState(['user'])
  },
  created() {
    if (this.params && this.params.id) {
      this.id = this.params.id
      this.getDetail(this.params.id)
    }
    this.form.areaId = this.user.info.areaId
    this.getTableData()
  },
  methods: {
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: !!isRefresh,
      })
    },
    getTableData() {
      this.instance.post("/appcollectiveeconomyequity/list", null, {
        params: {...this.page }
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records.shareholderList
          this.page.total = res.data.total
        }
      })
    },
    getDetail(id) {
      this.instance.post(`/appcollectiveeconomyequity/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.data = res.data
        }
      })
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.instance.post(`/appcollectiveeconomyequity/addOrUpdate`, {
            ...this.form,
            id: this.params.id || '',
          }).then(res => {
            if (res.code == 0) {
              this.$message.success('提交成功')
              setTimeout(() => {
                this.cancel(true)
              }, 600)
            }
          })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.Add {
  height: 100%;
  background: #f3f4f5;

  ::v-deep .ai-detail .ai-detail__footer {
    background: #FFF;
  }
}
</style>