<template>
  <div id="form-1">
    <van-row>
      <van-col span="24" class="main">
        <van-form @submit="onSubmit" label-width="4em">
          <van-field v-model="form.send_id" name="send_id" label="发送人id" style="display: none" />
          <van-field v-model="form.send_name" name="send_name" label="发送人" readonly />
          <van-field name="to_type" label="接收用户">
            <template #input>
              <van-radio-group v-model="form.to_type" direction="horizontal">
                <van-radio name="0">所有用户</van-radio>
                <van-radio name="1">机构用户</van-radio>
                <van-radio name="2">平台用户</van-radio>
                <van-radio name="4">指定用户</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field v-model="form.to_name" name="to_name" center clearable label="用戶姓名" readonly v-if="form.to_type == '4'">
            <template #button>
              <van-button size="small" type="primary" @click="toSelect">选择用户</van-button>
            </template>
          </van-field>
          <van-field v-model="form.to_id" name="to_id" style="display: none" v-if="form.to_type == '4'" />
          <van-field v-model="form.content" name="content" rows="2" autosize label="发送内容" type="textarea" placeholder="发送内容" />
          <van-field name="notice_file" label="通知文件" :rules="[{ required: true, message: '通知文件' }]">
            <template #input>
              <van-uploader
                :fileList="form.notice_file"
                :max-count="1"
                :after-read="(file) => toUpload(file, 'notice_file')"
                @delete="(file) => toDelete(file, 'notice_file')"
                accept="file"
              />
            </template>
          </van-field>
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit">提交</van-button>
          </div>
        </van-form>
      </van-col>
    </van-row>
    <van-dialog class="dialog" v-model="dialog.show" :title="dialog.title" :show-confirm-button="false" show-cancel-button cancel-button-text="返回">
      <van-col span="24" class="one" v-if="dialog.type == '1'">
        <van-col span="24" class="one_1">
          <van-search v-model="phone" show-action placeholder="请输入账号">
            <template #action>
              <div @click="searchUser()">搜索</div>
            </template>
          </van-search>
        </van-col>
        <van-col span="24" class="one_2">
          <van-col span="24" class="list" v-for="(item, index) in list" :key="index">
            <van-col span="24" class="name"> {{ item.phone }} </van-col>
            <van-col span="24" class="other">
              <van-col span="24" class="otherInfo">
                联系电话:<span>{{ item.name }}</span>
              </van-col>
              <van-col span="24" class="otherInfo">
                机构代码:<span>{{ item.code }}</span>
              </van-col>
            </van-col>
            <van-col span="24" class="btn">
              <van-button type="info" size="mini" @click.prevent="conFirm(item)">确认选择</van-button>
            </van-col>
          </van-col>
          <van-col span="24" class="page">
            <page @search="searchUser" :limit="limit" :total="total"></page>
          </van-col>
        </van-col>
      </van-col>
    </van-dialog>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: upload } = createNamespacedHelpers('upload');
const { mapActions: personal } = createNamespacedHelpers('personal');
export default {
  name: 'form-1',
  props: {
    form: { type: Object },
  },
  components: {},
  data: function () {
    return {
      dialog: { show: false, titla: '查询用户', type: '1' },
      // 查询
      phone: '',
      list: [],
      limit: 3,
      total: 0,
    };
  },
  created() {},
  methods: {
    ...upload(['upload']),
    ...personal({ personalQuery: 'query' }),
    onSubmit(values) {
      this.$emit('onSubmit', values);
    },
    async toUpload({ file }, model) {
      // 上传,赋值
      const res = await this.upload({ file, dir: 'analysis' });
      if (this.$checkRes(res)) {
        this.$set(this.form, model, [{ name: res.name, url: res.uri }]);
      }
    },
    toDelete(file, model) {
      const index = this.form[model].findIndex((f) => _.isEqual(f, file));
      this.form[model].splice(index, 1);
    },
    // 选择用户
    toSelect() {
      this.dialog = { show: true, title: '查询用户', type: '1' };
    },
    // 查询用户
    async searchUser({ skip = 0, limit = this.limit, ...info } = {}) {
      let res = await this.personalQuery({ skip, limit, phone: this.phone, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 确认选择
    conFirm(value) {
      this.$set(this.form, `to_id`, value.id);
      this.$set(this.form, `to_name`, value.phone);
      this.dialog = { show: false, title: '查询用户', type: '1' };
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.dialog {
  /deep/.van-dialog__content {
    max-height: 350px;
    overflow-y: auto;
  }
  .one {
    .one_2 {
      .list {
        border-bottom: 1px dashed #ccc;
        margin: 0 0 8px 0;
        padding: 0 8px 5px 8px;
        .name {
          font-size: 16px;
          margin: 0 0 5px 0;
        }
        .other {
          margin: 0 0 5px 0;
          .otherInfo {
            font-size: 14px;
            color: #666;
            margin: 0 0 5px 0;
            span {
              color: #000;
            }
          }
        }
        .btn {
          text-align: center;
        }
      }
    }
  }
  .two {
    .btn {
      margin: 8px 0;
      text-align: center;
    }
  }
}
</style>
